Iterating an array of buttons looking for HIGH

It's been awhile since I've coded on Arduino (been messing with Python lately) and I bet there is a super simple answer here.

I have a variable called buttonPins[] which is equal to {2,3,4,5}.

What kind of code do I need to write that will iterate each pin checking for a value of high?

The end goal is to basically say, "are any of the four buttons on? if so, turn on the LED"

Will this script only run 4 times then terminate?

const int buttonPin[] = {2,3,4,5};
const int ledPin = 13;

void setup() {
  pinMode(ledPin,OUTPUT);
  pinMode(buttonPin, INPUT_PULLUP);
}

void loop() {

  for (int i=2; i<6; i++){
    if(digitalRead(buttonPin[i]) == HIGH){
      digitalWrite(ledPin, HIGH);
    }
    else {
      digitalWrite(ledPin, LOW);
    }
    
  }
  
}

I realize that my setup script is wrong regarding the pinMode and buttonPins[].

  1. Your script run in forever, in the infinitive loop
  2. you need to change for (int i=2; i<6; i++) to for (int i=0; i<4; i++)
  3. you need to add the below code in the setup() function
for (int i=0; i<4; i++){
 pinMode(buttonPin[i], INPUT_PULLUP);
}

IoT_hobbyist:

  1. Your script run in forever, in the infinitive loop
  2. you need to change for (int i=2; i<6; i++) to for (int i=0; i<4; i++)
  3. you need to add the below code in the setup() function
for (int i=0; i<4; i++){

pinMode(buttonPin[i], INPUT_PULLUP);
}

Yikes.. I can't believe I did that. All I was thinking was pin numbers.. not array numbers.

thanks

Aimbit:
array numbers.

Indices (or indexes if you must).

You turn the LED on if a button is down but you turn it off if the next button is NOT down. You said you want to leave it on if any button is pushed. If your loop() has nothing better to do you can use 'return' after turning on the LED to exit loop() (which will then be called again). If you get through the loop without hitting return, none of the buttons is pushed and it's time to turn off the LED.

If your loop() has other stuff to do you can't use 'return' inside the 'for' loop. You can use 'break' to exit the loop early and check the value of 'i' to see if the loop exited early (led is on) or reached the end (led should be turned off).

Two more ways:
In the 'for' loop, count the number of buttons that are pressed. After the 'for' loop, turn on the LED is the count is not 0 and turn it off if the count is 0.

Initialize a boolean variable to 'false'. In the 'for' loop, if a button is pressed, set the boolean variable to 'true'. After the 'for' loop, if the variable is 'true', turn on the LED, otherwise turn off the LED.

pcbbc:
Indices (or indexes if you must).

right. thanks

// recognize multiple button presses; tgl LED when pressed

byte butPins [] = { A1, A2, A3 };
byte ledPins [] = { 10, 11, 12 };

#define N_PINS sizeof(butPins)

byte butLst [N_PINS] = {};


// -----------------------------------------------------------------------------
void setup (void)
{
    for (unsigned n = 0; n < N_PINS; n++)  {
        digitalWrite (ledPins [n], OFF);
        pinMode      (ledPins [n], OUTPUT);

        pinMode      (butPins [n], INPUT_PULLUP);
        butLst [n]  = digitalRead (butPins [n]);
    }
}

// -----------------------------------------------------------------------------
void loop (void)
{
    for (unsigned n = 0; n < N_PINS; n++)  {
        byte but = digitalRead (butPins [n]);

        if (butLst [n] != but)  {
            butLst [n] = but;

            if (LOW == but)     // button pressed
                digitalWrite (ledPins [n], ! digitalRead (ledPins [n]));
        }
    }

    delay (10);         // debounce
}