I wrote some code to set and RGB LED certain colours. I have made a 2D array of colours. But i had to put it in the setColours() function that i made because it didn't work outside of it.
How do i get array to work in custom functions? And is there a better way to have a colour look-up?
int R;
int G;
int B;
void setup() {
setPins(9, 10, 11);
}
void loop() {
for (int k = 0; k < 16; k++) {
setColour(k);
delay(500);
}
}
void setPins(int R, int G, int B) {
pinMode(R, OUTPUT);
analogWrite(R, 0);
pinMode(G, OUTPUT);
analogWrite(G, 0);
pinMode(B, OUTPUT);
analogWrite(B, 0);
}
void setRGB(int Rval, int Gval, int Bval) {
analogWrite(R, Rval);
analogWrite(G, Gval);
analogWrite(B, Bval);
}
void setColour(int i) {
int COLOURS[16][3] = {
{255,0,0},
{0,255,0},
{0,0,255},
{255,255,0},
{0,255,255},
{255,0,255},
{255,255,128},
{128,255,255},
{255,128,255},
{255,128,0},
{128,255,0},
{128,0,255},
{128,128,0},
{128,0,128},
{0,128,128},
{128,64,0}
};
analogWrite(R, COLOURS[i][0]);
analogWrite(G, COLOURS[i][1]);
analogWrite(B, COLOURS[i][2]);
}
[edit]You have to use a number to lookup the colour from the array, because I couldn't work out how to get it to lookup from an array using colour names e.g. BLUE = 3 or something. That is without bulky ifs and stuff...[/edit]