Trying to learn more about arrays by controlling 4 LEDs with pushbuttons. But Im struggling to see what's wrong.
I am trying to assign the switches as input pins and leds as output pins with for loops and arrays.
When button one is pressed, I want the matching led to it to light up.
const int LED[4] = {2,3,4,7}; // Creating an array with 4 slots, assigned values of 2,3,4 and 7 for the pins.
const int button[4] = {6,7,8,9}; // same idea with the buttons'
int i;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
for (i=2; i<13; i++){
if(i<6){
pinMode(i,INPUT_PULLUP);
}
else{
pinMode(i, OUTPUT);
}
}
}
void loop() {
// check if the buttons are pressed and light up the corresponding LED
for(i=0; i<4;i++){
if(digitalRead(button[i])==LOW){
digitalWrite(LED[i],HIGH);
delay(10);
}
}
}