Trouble with Arrays to control LEDs

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);
    }
  }
}

Topic moved, it was in the wrong place. It was in a place for installation and troubleshooting the Arduino system . NOT your project. Please read the descriptions of the places you want to post in.

You should describe what your code actually does against what you want it to do.

When do you ever turn off the LEDs?
You need an else clause on that if to turn them off.

Have a look where your LEDs & buttons are connected, your code and this wiring is not refering to the same pins!

Why does pin 7 appear twice?

Why not use the arrays to set the modes?

Hello
How are you?
What is going wrong?
Post a description written in short words what shall happens.

sorry I did this in a rush. My bad.

I have updated the description

I'm pretty sure it's right. the switches are connected to pins 5,4,3 and 2. Hence i<6 for the switches as input pins. Isn't that correct?

where does pin 7 appear twice? And could you provide an example of setting the modes with arrays?

@mattbolty that is a common problem with beginners and advanced programmers alike: staring at a sketch and not seeing the problem that everyone else spots immediately.

Look at the numbers:

const int LED[4] = {2,3,4,7};
const int button[4] = {6,7,8,9};

Do you see that number 7 is used twice ?

Compare the numbers in the array with your project. In the photo you use other pins than those numbers.

Look at the code in the loop():

  if(digitalRead(button[i])==LOW){
    digitalWrite(LED[i],HIGH);
    delay(10);
  }

Do you notice that the led is turned on, but never turned off when the button is released ?

I hope you understand now what the others are saying.

Don't try to set pins that you don't use and keep it as simple as possible. Just two for-loops to initialize the pins is the most simple I think:

const int ButtonPins[4] = { 2, 3, 4, 5};
const int LedPins[4] = { 10, 11, 12, 13};

void setup() 
{
  // Serial.begin(9600);      // Serial output is not used yet.

  // Set the pins for the buttons
  for( int i=0; i<4; i++)
  {
    pinMode( ButtonPins[i], INPUT_PULLUP);
  }

  // Set the pins for the leds
  for( int i=0; i<4; i++)
  {
    pinMode( LedPins[i], OUTPUT);
  }
}


void loop() 
{
  // check if the buttons are pressed and light up the corresponding LED
  for( int i=0; i<4; i++)
  {
    if( digitalRead( ButtonPins[i]) == LOW)
    {
      digitalWrite( LedPins[i], HIGH);
    }
    else
    {
      digitalWrite( LedPins[i], LOW);
    }
  }

  delay( 10);    // slow down the sketch
}

The sketch in Wokwi simulation:

But not the code. As I said you never turn the LEDs off, is that how you want it, when all the LEDs are on the program doesn't change the LEDs any more?

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'

Buttons are on pins 2,3,4 & 5 , LEDS are on 10,11,12,& 13
The arrays are wrong!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.