Is it possible to read a digital input from an array without using the exact element? Such as
while(digitalRead(myButtonArray[i]))
digitalWrite(myLEDArray[i], HIGH);
Reading the myButtonArray one by one and turning on the led one by one doesnt make for clean written code. The following is just an example code. When button on pin 2 which is element 0 is pressed turn on led on pin 7 and turn off when released. When button on pin 3 is pressede turn on led on pin 8 and turn off when released and continue on in the same fashion with the rest of the buttons and leds as well.
int myButtonArray[] = {2,3,4,5,6};
int myLEDArray[] = {7,8,9,10,11};
void setup() {
for(int i = 0;i<5;i++){
pinMode(myButtonArray[i], INPUT);
pinMode(myLEDArray[i], OUTPUT);
}
}
void loop() {
while(digitalRead(myButtonArray[0]))
digitalWrite(myLEDArray[0], HIGH);
while(digitalRead(myButtonArray[1]))
digitalWrite(myLEDArray[1], HIGH);
while(digitalRead(myButtonArray[2]))
digitalWrite(myLEDArray[2], HIGH);
while(digitalRead(myButtonArray[3]))
digitalWrite(myLEDArray[3], HIGH);
while(digitalRead(myButtonArray[4]))
digitalWrite(myLEDArray[4], HIGH);
}
for
loop.