Saturday, October 22, 2011

Controlling 3 RGB LEDs from arduino

basically i hooked up each pin(except for GND) of a common cathode RGB LED to the arduino with resistors, and then wrote a quick program to control all three color of each LEDs easily.

//light
//light(1,1,ps); light(LEDx,colorz,Time);
//
//LED: 1 or 2 or 3
//
//colorz: 1 -Red
//colorz: 2 -Green
//colorz: 3 -Blue
//
//time in ms


//blend
//blend(1,1,ps); blend(LEDx,colorz,Time);
//
//LED: 1 or 2 or 3
//
//colorz: 1 -RG yellow
//colorz: 2 -GB light blue
//colorz: 3 -RB purple/pink
//colorz: 4 -RGB white
//
//time in ms
int colorz;
int LEDx;
int delayx;
int pinx;
int pinz;
int piny;
int blnd;

void setup()
{
//LED1
pinMode(13, OUTPUT);
pinMode(12, OUTPUT);
pinMode(11, OUTPUT);
//LED2
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(8, OUTPUT);
//LED3
pinMode(7, OUTPUT);
pinMode(6, OUTPUT);
pinMode(5, OUTPUT);
}

void loop()
{
int ps = 75;
light(1,1,ps); //LED1-Red
light(2,2,ps); //LED2-green
light(3,3,ps); //LED3-blue

blend(1,1,ps); //LED1-purple RG
blend(2,2,ps); //LED2-light blue GB
blend(3,3,ps); //LED3-yellow RB
blend(1,4,ps); //LED1-White RGB
}

void light(int LEDx,int colorz,int delayx)
{
if(LEDx == 1) //if LEDx is 1(which is the first LED(digital pins 13-11))
{
if(colorz == 1) //if colorz is 1 then turn on pin 13 with delay in on/off,
{ // which is red of first RGB LED
blinkz(13, delayx);
}

if(colorz == 2) //if colorz is 2 then turn on pin 12 with delay in on/off,
{ //which is green of first RGB LED
blinkz(12, delayx);
}

if(colorz == 3) //if colorz is 3 then turn on pin 11 with delay in on/off,
{ //which is blue of first RGB LED
blinkz(11, delayx);
} //and so on for the rest of the if statements
}

if(LEDx == 2)
{
if(colorz == 1)
{
blinkz(3, delayx); //uses digital pin 3
}

if(colorz == 2)
{
blinkz(4, delayx); //uses digital pin 4
}

if(colorz == 3)
{
blinkz(8, delayx); //uses digital pin 8
}
}

if(LEDx == 3)
{
if(colorz == 1)
{
blinkz(7, delayx);
}

if(colorz == 2)
{
blinkz(6, delayx);
}

if(colorz == 3)
{
blinkz(5, delayx);
}
}
}

void blinkz(int pinx, int delayx) //blinks one LED at a time on/off with an established time delay(delayx)
{
digitalWrite(pinx, HIGH);
delay(delayx);
digitalWrite(pinx, LOW);
delay(delayx);
}

void blend(int LEDx, int colorz, int delayx)
{
if(LEDx == 1)
{
if(colorz == 1)
{
//red+green, purple
mixblend(13,12,delayx); //two pins at once to mix colors, or random two pins of any LED at a time.
}

if(colorz == 2)
{
//Green+blue, light blue
mixblend(12,11,delayx);
}

if(colorz == 3)
{
//red+blue,
mixblend(13,11,delayx);
}

if(colorz == 4)
{
whiteblend(13,12,11,delayx); //makes white(if you do three pins from same RGB LED, or random three pins of any LED at a time.
}
}

if(LEDx == 2)
{
if(colorz == 1)
{
mixblend(3,4,delayx);
}

if(colorz == 2)
{
mixblend(4,8,delayx);
}

if(colorz == 3)
{
mixblend(3,8,delayx);
}

if(colorz == 4)
{
whiteblend(3,4,8,delayx);
}
}

if(LEDx == 3)
{
if(colorz == 1)
{
mixblend(7,6,delayx);
}

if(colorz == 2)
{
mixblend(6,5,delayx);
}

if(colorz == 3)
{
mixblend(7,5,delayx);
}

if(colorz == 4)
{
whiteblend(7,6,5,delayx);
}
}
}

//pinx, pinz, are any two LED pins
//you want to light at one time
void mixblend(int pinx, int pinz, int delayx) //tells which two LEDs to blend
{
digitalWrite(pinx, HIGH);
digitalWrite(pinz, HIGH);
delay(delayx);
digitalWrite(pinx, LOW);
digitalWrite(pinz, LOW);
delay(delayx);
}

//pinx, pinz, piny are any three LED pins
//you want to light at one time, do three from same LED for white
void whiteblend(int pinx, int pinz, int piny, int delayx)
{
digitalWrite(pinx, HIGH);
digitalWrite(pinz, HIGH);
digitalWrite(piny, HIGH);
delay(delayx);
digitalWrite(pinx, LOW);
digitalWrite(pinz, LOW);
digitalWrite(piny, LOW);
delay(delayx);
}
*use the "Auto Format"(Cntrl + T) option under the tool menu in the Arduino IDE to fix the formatting. Pics:

No comments:

Post a Comment