I already posted this on reddit, so here goes:
For an installation, I need to create a code where one can press a key (r, g, b, y) which then creates a rgb code for an rgb lamp. There will be a fixed amount of lamps (e.g. 50 in my code), and after a second key is pressed, the first color switches to the next lamp, so if for example the first user pressed “r”, the first lamp glows red. When the second user presses “b”, the red “travels” from the first to the second lamp and the first lamp gets blue.
I made three arrays, one for the red value, one for the green value, one for the blue value. In theory, the arrays each should store 50 values for the 50 lamps, so that the values change as soon as a key is pressed. The new rgb value should always be on the 0th element of the array, and the old values travel to the next element.
Here is what I got so far:
int mySwitch = 'r';
//lamp count
int num = 50;
int[] r = new int[num];
int[] g = new int[num];
int[] b = new int[num];
int winR[] = {246, 83, 20};
int winG[] = {124, 187, 0};
int winB[] = {0, 161, 241};
int winY[] = {255, 187, 0};
void setup() {
size(800, 600);
rectMode(CENTER);
}
void draw() {
noStroke();
background(0);
// Add the new values to the beginning of the array
if (mySwitch == 'r') {
r[0] = winR[0];
g[0] = winR[1];
b[0] = winR[2];
} else if (mySwitch == 'g') {
r[0] = winG[0];
g[0] = winG[1];
b[0] = winG[2];
} else if (mySwitch == 'b') {
r[0] = winB[0];
g[0] = winB[1];
b[0] = winB[2];
} else if (mySwitch == 'y') {
r[0] = winY[0];
g[0] = winY[1];
b[0] = winY[2];
}
for (int i = 0; i < num; i++) {
float dist = width/num;
fill(r[i], g[i], b[i]);
rect(i*dist, height/2, dist, height);
}
}
void keyPressed() {
if (keyCode == 82) {
mySwitch = 'r';
} else if (keyCode == 71) {
mySwitch = 'g';
} else if (keyCode == 66) {
mySwitch = 'b';
} else if (keyCode == 89) {
mySwitch = 'y';
}
}
Unfortunately, this only works for one lamp at a time, as the other values aren’t stored in the array. Do you have any advice on how I could make the preceding rgb values be stored in the array?
I hope I could make myself clear, but if you have any questions, just ask! Thanks for any help!