 # How to do this with a loop?

``````  if (digitalRead(x1) == HIGH && digitalRead(y1) == HIGH) {
EEPROM.write(1, true);}

if (digitalRead(x2) == HIGH && digitalRead(y1) == HIGH) {
EEPROM.write(2, true);}

if (digitalRead(x3) == HIGH && digitalRead(y1) == HIGH) {
EEPROM.write(3, true);}

if (digitalRead(x4) == HIGH && digitalRead(y1) == HIGH) {
EEPROM.write(4, true);}

if (digitalRead(x5) == HIGH && digitalRead(y1) == HIGH) {
EEPROM.write(5, true);}
``````

i am making 5x5x5 led cube, and i want to read what coordinates are pressed to save the led that needs to light up, this is done layer by layer. How can i simplify this code (with a loop)?

i

You need an array of pin numbers. Then your loop will just work down the items in the array.

First read all the buttons and save their values to variables. if you put the button pin numbers and the button states in arrays you can do it like this

``````for (byte n = 0; n < numButtons; n++) {
buttonState[n] = digitalRead(buttonPin[n]);
}
``````

and then you can probably test the combinations of the saved values with a similar FOR loop.

Why are you writing to the EEPROM?

…R

``````  if (digitalRead(x1) == HIGH && digitalRead(y1) == HIGH) {

digitalWrite(51, EEPROM.read(1));

if (digitalRead(x2) == HIGH && digitalRead(y1) == HIGH) {
EEPROM.write(2, true);}

if (digitalRead(x3) == HIGH && digitalRead(y1) == HIGH) {
EEPROM.write(3, true);}

if (digitalRead(x4) == HIGH && digitalRead(y1) == HIGH) {
EEPROM.write(4, true);}

if (digitalRead(x5) == HIGH && digitalRead(y1) == HIGH) {
EEPROM.write(5, true);}
``````

i am making 5x5x5 led cube, with 5x and 5y buttons and work per layer. is there a way i can make this in a loop? can you give an example code?

Why have you posted the same question again ?

UKHeliBob: Why have you posted the same question again ?

Because it's a loop?

@zedpython, stop cross-posting. Threads merged.