Way to streamline my code and use variables for RGB color values? [SOLVED]

Hi everyone,

I have been using Arduino and writing the code for it for a long time, but I am not an expert coder by any means.

I have a midi controller that has a lot of RGB LEDs in it and I control them with incoming MIDI messages. I have all this figured out and it works.

There are 17 RGB LEDs, To change colors I have this line in my code, with four numerical values:

keysRGB.setLED(1, 2, 3, 4);

The first value is the LED number and the 2, 3, 4 values are RGB values. I am going to implement 10 different colors and I would like them to be global and just listed in the code once, in case I want to add colors or change color values.

Would there be a way to have this in the code:

keysRGB.setLED(1, PURPLE);
keysRGB.setLED(1, RED);
keysRGB.setLED(1, BLUE); etc, Where RED would be variable with three values like: 255, 0, 0 ?

If this is possible how would I implement it? i was looking at a switch case, but I don't really know how to use that.

Thanks.

You can use #define e.g.

#define BLUE 255,0,0

The preprocessor will replace all instances of BLUE in your code with the text. BLUE is not a variable and can only be changed in the source code before you compile.

There are 17 RGB LEDs, To change colors I have this line in my code, with four numerical values:

keysRGB.setLED(1, 2, 3, 4);

If you show us the whole code we can advise you better.

Actually that is exactly how I do it in my colorObj library.

//								Red,Grn,blu
#define	LC_BLACK			  0,  0,  0
#define	LC_CHARCOAL		 50, 50, 50
#define	LC_DARK_GREY	140,140,140
#define	LC_GREY			185,185,185		
#define	LC_LIGHT_GREY	250,250,250
#define	LC_WHITE   		255,255,255

#define	LC_RED     		255,  0,  0
#define	LC_PINK   		255,130,208

#define	LC_GREEN   		  0,255,  0
#define	LC_DARK_GREEN    0, 30,  0
#define	LC_OLIVE   		 30, 30,  1

#define	LC_BLUE    		  0,  0,255
#define	LC_LIGHT_BLUE	164,205,255
#define	LC_NAVY   		  0,  0, 30

#define	LC_PURPLE   	140,  0,255
#define	LC_LAVENDER    218,151,255
#define	LC_ORANGE   	255,128,  0

#define	LC_CYAN    		  0,255,255
#define	LC_MAGENTA 		255,  0,255
#define	LC_YELLOW  		255,255,  0

Well, in my editor they are all lined up nicely. This formats them a bit differently.

-jim lee

Thank you everyone for the replies.

Klaus_K I am using your suggestion of #define RED 255,0,0,0. It works great! And I learned something new. Awesome and thank you all for the help.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.