vendredi 3 avril 2020

Control RGB LEDs ON/OFF - A smarter way than using "if"? (Arduino, C++)

I want to control three RGB LEDs in color (eff. 9 LEDs in total) and whether to be on or off. I am using Arduino and the FastLED library to turn them on using FastLED.show();.

I have six buttons on my display: LED1, LED2, LED3, RED, GREEN, BLUE (dual state buttons)

I came up with this one:

if(BLUE == true){BLUEV = 255;} else { BLUEV = 0;}
if(RED == true){REDV = 255;} else { REDV = 0;}
if(GREEN == true){GREENV = 255;} else { GREENV = 0;}

if(LED1 == true){leds[0] = CRGB(REDV, GREENV, BLUEV); } else { leds[0] = CRGB(0, 0, 0); }
if(LED2 == true){leds[1] = CRGB(REDV, GREENV, BLUEV); } else { leds[1] = CRGB(0, 0, 0); }
if(LED3 == true){leds[2] = CRGB(REDV, GREENV, BLUEV); } else { leds[2] = CRGB(0, 0, 0); }
FastLED.show();

but it's obviously imperfect, I reckon.

Anyone has suggestions how to code it better? Maybe using a switch case?

Cheers!

Aucun commentaire:

Enregistrer un commentaire