SWITCH WITH LED switching

const int buttonPin1 = 2;     // the number of the pushbutton pin
const int buttonPin2 = 3; 
const int buttonPin3 = 4; 
const int buttonPin4 = 5; 
const int ledPin1 =  6;      // the number of the LED pin
const int ledPin2 =  7; 
const int ledPin3 =  8; 
const int ledPin4 =  9; 

// variables will change:
int buttonState1 = 0;         // variable for reading the pushbutton status
int buttonState2 = 0;
int buttonState3 = 0;
int buttonState4 = 0;
void setup() {
  // initialize the LED pin as an output:
  pinMode(ledPin1, OUTPUT);    
  pinMode(ledPin2, OUTPUT); 
  pinMode(ledPin3, OUTPUT); 
  pinMode(ledPin4, OUTPUT);  
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin1, INPUT);  
  pinMode(buttonPin2, INPUT);  
  pinMode(buttonPin3, INPUT);  
  pinMode(buttonPin4, INPUT);  
}

void loop(){
  // read the state of the pushbutton value:
  buttonState1 = digitalRead(buttonPin1);
  buttonState2 = digitalRead(buttonPin2);
  buttonState3 = digitalRead(buttonPin3);
  buttonState4 = digitalRead(buttonPin4);
  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState1 == HIGH) {     
    // turn LED on:    
    digitalWrite(ledPin1, HIGH);
    digitalWrite(ledPin2, LOW);
    digitalWrite(ledPin3, LOW);
    digitalWrite(ledPin4, LOW);
  } 
  else {
     delay(50);
  }
    if (buttonState2 == HIGH) {     
    // turn LED on:    
    digitalWrite(ledPin1, LOW);
    digitalWrite(ledPin2, HIGH);
    digitalWrite(ledPin3, LOW);
    digitalWrite(ledPin4, LOW);
  } 
  else {
     delay(50);
  }
    if (buttonState3 == HIGH) {     
    // turn LED on:    
    digitalWrite(ledPin1, LOW);
    digitalWrite(ledPin2, LOW);
    digitalWrite(ledPin3, HIGH);
    digitalWrite(ledPin4, LOW);
  } 
  else {
    delay(50);
    
  }
    if (buttonState4 == HIGH) {     
    // turn LED on:    
    digitalWrite(ledPin1, LOW);
    digitalWrite(ledPin2, LOW);
    digitalWrite(ledPin3, LOW);
    digitalWrite(ledPin4, HIGH);
  } 
  else {
     delay(50);
  }
}

i has a progam like this
4 push button and 4 led
each button can switch the led
when press a button the led will stay on
while press another button and the button's led will on and other will turn off , until i press the reset and then all led's will thrn off

i have a question
how can i change my progam to make the led turn on 30 seconds and then off without using "delay"commond
because if i use delay then i can't swtich the leds because it is delaying

i aleary read the "blink without delay" but i still not sure how can it combine with my progam.
many thianks :wink:

Think about how you would turn the LEDs on an off, manually.

Each time a button is pressed, you turn some LEDs on, and note when you did that.

Periodically, you would then check what "time" it is, and see if enough time has elapsed. If it has, you turn those LEDs off.

On the Arduino, periodically means on every iteration of loop. "Time" is defined by millis() (both when the lights are turned on and "now").

thank you paul
i think i don't need to turn the leds off manually
err
could u have any sample about millis()
i am not too understand.

check the famous - http://www.arduino.cc/en/Tutorial/BlinkWithoutDelay article in the tutorial section. There is a lot to learn there.

i had aleary read the blick without delay..
but.. i am quite new on the progaming?
could u change my progam using millis?

and i may easier to understand

I have allready written someones program today and 1/ day > enough :slight_smile:

i had aleary read the blick without delay..
but.. i am quite new on the progaming?

Reading is not enough, programming is about understanding. It is not that difficult if you take little steps. Start from the blink without delay example and just add the second LED and if that works the next and so on. Don't try to build a bigger program than you understand, so only expand the functionality if you understand what you made so far. Small steps ...

"Every long journey is made in small steps" variation on Confusius.