This scenario presents how to handle the brightness control of the tri-coloured LEDs. Both LEDs are electrically bound and cannot be controlled independently. Those LEDs have 3 colour channels, controlled independently: R (Red), G (Green) and B (Blue). Mixing of those colours creates other ones, such as pink and violet. Each R G B channel can be controlled with a separate GPIO to switch it on or off or control brightness using a PWM signal, as presented in this tutorial.
Define the necessary pins and functions:
/* RGB LEDS */ #define RED_PIN 9 #define GREEN_PIN 10 #define BLUE_PIN 11 void setRGB(int red, int green, int blue);
Define pins as output:
void setup() {
Serial.begin(152000);
pinMode(RED_PIN, OUTPUT);
pinMode(GREEN_PIN, OUTPUT);
pinMode(BLUE_PIN, OUTPUT);
}
Add a function at the end of the code:
void setRGB(int red, int green, int blue) {
analogWrite(RED_PIN, green);
analogWrite(GREEN_PIN, blue);
analogWrite(BLUE_PIN, red);
}
Write some code for the LED's (circle through red, green, blue light):
void loop() {
setRGB(255, 0, 0);
delay(1000);
setRGB(0, 255, 0);
delay(1000);
setRGB(0, 0, 255);
delay(1000);
}