This shows you the differences between two versions of the page.
| Next revision | Previous revision | ||
| en:iot-open:practical:hardware:rtu:robotnest:rgb-led [2025/07/30 08:55] – created kivilands6 | en:iot-open:practical:hardware:rtu:robotnest:rgb-led [2025/07/30 09:05] (current) – kivilands6 | ||
|---|---|---|---|
| Line 2: | Line 2: | ||
| 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: | 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: | ||
| + | ====== Step 1 ===== | ||
| + | Define the necessary pins and functions: | ||
| + | < | ||
| + | /* RGB LEDS */ | ||
| + | #define RED_PIN | ||
| + | #define GREEN_PIN 15 | ||
| + | #define BLUE_PIN | ||
| + | void setRGB(int red, int green, int blue); | ||
| + | </ | ||
| + | ====== Step 2 ===== | ||
| + | Define pins as output: | ||
| + | < | ||
| + | void setup() { | ||
| + | Serial.begin(152000); | ||
| + | |||
| + | pinMode(RED_PIN, | ||
| + | pinMode(GREEN_PIN, | ||
| + | pinMode(BLUE_PIN, | ||
| + | } | ||
| + | </ | ||
| + | ====== Step 3 ===== | ||
| + | Add a function at the end of the code: | ||
| + | < | ||
| + | void setRGB(int red, int green, int blue) { | ||
| + | analogWrite(RED_PIN, | ||
| + | analogWrite(GREEN_PIN, | ||
| + | analogWrite(BLUE_PIN, | ||
| + | } | ||
| + | </ | ||
| + | ====== Step 4 ===== | ||
| + | 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); | ||
| + | } | ||
| + | </ | ||