# Store values in array

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 = winR;
g = winR;
b = winR;
} else if (mySwitch == 'g') {
r = winG;
g = winG;
b = winG;
} else if (mySwitch == 'b') {
r = winB;
g = winB;
b = winB;
} else if (mySwitch == 'y') {
r = winY;
g = winY;
b = winY;
}

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!

You could first of all replace int num = 50 with Color[] col = new Color; and just take it‘s length. And then just set the respective Color value for each element. And then just iterate over the array and each Time a new Color is added you can just set each Element to be one higher.

You could also use Java.util.Queue. Might be a more convenient.

Hey @b_and_z, before you overwrite r, you need to shift all the values. Try to make a for loop which shifts all the values one higher, so you move r to r, r to r, etc. and the last element will be dropped. Hint: start at the end of the array.

Then values are shifted and you can write a new value to r.

Thanks for your answer! I tried this, but now the array is filled as long as the key is pressed

``````  if (keyPressed) {
for (int i = num-1; i > 0; i--) {
r[i] = r[i-1];
g[i] = g[i-1];
b[i] = b[i-1];
}
}
``````
1 Like

Your array goes the wrong way round. You should have the value i+1, not -1. And as for why it‘s filled only while mouse is pressed, you probably added in draw something that resets the array…

Fixed-sized array cycling queue sketches: Good job @b_and_z, the code looks good!

Now if you want this to happen only once, you have to put the code inside `keyPressed()`. Otherwise it happens every frame. I think the code which sets `r` should go there as well.

Thanks all for your help, I found another way which I also understand ``````
boolean initChange = false;

void draw() {
if (initChange) {
for (int i = num-1; i > 0; i--) {
r[i] = r[i-1];
g[i] = g[i-1];
b[i] = b[i-1];
}

initChange=false;
}
}
void mouseClicked() {
initChange = true;
}
``````
1 Like