RGB LED

Ligação de um LED RGB (Vermelho +  Verde + Azul) ao Arduino.

O LED usado é de Ânode (+) comum:

O esquema geral de ligação é o seguinte:

Ligações ao Arduino:

Led Vermelho -> Pino 9 (PWM) c/ resistência de 150R

Led Verde -> Pino 10 (PWM) c/ resistência de 100R

Led Azul  -> Pino 11 (PWM) c/ resistência de 100R

Os códigos utilizados para testar:

FADING

int red = 9; //this sets the red led pin

int green = 10; //this sets the green led pin

int blue = 11; //this sets the blue led pin

int redNow;

int blueNow;

int greenNow;

int redNew;

int blueNew;

int greenNew;

void setup()

{ //this sets the output pins

 pinMode(red, OUTPUT);

 pinMode(green, OUTPUT);

 pinMode(blue, OUTPUT);

 redNow = random(255);

 blueNow = random(255);

 greenNow = random(255);

 redNew = redNow;

 blueNew = blueNow;

 greenNew = greenNow;

}

#define fade(x,y) if (x>y) x--; else if (x<y) x++;

void loop()

{

 analogWrite(blue, blueNow);

 analogWrite(red, redNow);

 analogWrite(green, greenNow);

 redNew = random(255);

 blueNew = random(255);

 greenNew = random(255);

// fade to new colors

 while ((redNow != redNew) ||

  (blueNow != blueNew) ||

  (greenNow != greenNew))

 {

  fade(redNow,redNew)

  fade(blueNow,blueNew)

  fade(greenNow,greenNew)

  analogWrite(blue, blueNow);

  analogWrite(red, redNow);

  analogWrite(green, greenNow);

  delay(20);

 }

}

MULTICOLOR

//

int ledcolor = 0;

int a = 1000; //this sets how long the stays one color for

int red = 9; //this sets the red led pin

int green = 10; //this sets the green led pin

int blue = 11; //this sets the blue led pin

void setup() { //this sets the output pins

pinMode(red, OUTPUT);

pinMode(green, OUTPUT);

pinMode(blue, OUTPUT);

}

void loop() {

int ledcolor = random(7); //this randomly selects a number between 0 and 6

switch (ledcolor) {

case 0: //if ledcolor equals 0 then the led will turn red

analogWrite(red, 51);

delay(a);

analogWrite(red, 255);

break;

case 1: //if ledcolor equals 1 then the led will turn green

digitalWrite(green, LOW);

delay(a);

digitalWrite(green, HIGH);

break;

case 2: //if ledcolor equals 2 then the led will turn blue

digitalWrite(blue, LOW);

delay(a);

digitalWrite(blue, HIGH);

break;

case 3: //if ledcolor equals 3 then the led will turn yellow

analogWrite(red, 95);

digitalWrite(green, LOW);

delay(a);

analogWrite(red, 255);

digitalWrite(green, HIGH);

break;

case 4: //if ledcolor equals 4 then the led will turn cyan

analogWrite(red, 168);

digitalWrite(blue, LOW);

delay(a);

analogWrite(red, 255);

digitalWrite(blue, HIGH);

break;

case 5: //if ledcolor equals 5 then the led will turn magenta

digitalWrite(green, LOW);

digitalWrite(blue, LOW);

delay(a);

digitalWrite(green, HIGH);

digitalWrite(blue, HIGH);

break;

case 6: //if ledcolor equals 6 then the led will turn white

analogWrite(red, 155);

digitalWrite(green, LOW);

digitalWrite(blue, LOW);

delay(a);

analogWrite(red, 255);

digitalWrite(green, HIGH);

digitalWrite(blue, HIGH);

break;

}

}

RANDOM

int a = 1000; //this sets how long the LED stays one color for

int red = 9; //this sets the red led pin

int green = 10; //this sets the green led pin

int blue = 11; //this sets the blue led pin

void setup()

{ //this sets the output pins

  pinMode(red, OUTPUT);

  pinMode(green, OUTPUT);

  pinMode(blue, OUTPUT);

}

void loop()

{

  analogWrite(blue, random(255));

  analogWrite(red, random(255));

  analogWrite(green, random(255));

  delay(a);

}

Adicionando 3 potênciometros (um potênciometro para cada cor) e seguindo este esquema:

VERMELHO= Potênciometro ligado ao pino analógico 0

VERDE= Potênciometro ligado ao pino analógico 1

AZUL= Potênciometro ligado ao pino analógico 2

Podemos controlar cada uma das cores com valores de 0 a 255, o código utilizado é:

int a = 1000; //this sets how long the LED stays one color for

int red = 9; //this sets the red led pin

int green = 10; //this sets the green led pin

int blue = 11; //this sets the blue led pin

void setup()

{ //this sets the output pins

  pinMode(red, OUTPUT);

  pinMode(green, OUTPUT);

  pinMode(blue, OUTPUT);

}

void loop()

{

  

  int val1 = analogRead(0);

  int val2 = analogRead(1);

  int val3 = analogRead(2);

  val1 = map(val1, 0, 1023, 0, 255);

  val2 = map(val2, 0, 1023, 0, 255);

  val3 = map(val3, 0, 1023, 0, 255);

  analogWrite(red, val1);

  analogWrite(green, val2);

  analogWrite(blue, val3);

}