ask how to use Push button to turn on and off

hello i'm newbie using an arduino (), i've found some tutorial or guide to use push-button to turn on / off or to start and stop program using push button but i still have a problem to solve my code

i want to try to activate my code (just like Grade Crossing Signals but with 4 leds ). when i push the button once, the program will active and turn it off when i push the button again and so forth

can anyone help to solve my problem.. Any advice would be appreciated :slight_smile:

here's my code

 int buttonPin = 3;
int LED1 = 13;
int LED2 = 12;
int LED3 = 11;
int LED4 = 10;
int LED5 = 9;
int LED6 = 8;
int LED7 = 7;
int LED8 = 6;

 int x = 1;
// variables will change:
int buttonState = 0;         
int oldButtonState = LOW;
int newButtonState = digitalRead(buttonPin);

void setup() {
  // initialize the LED pin as an output:
  //pinMode(motorPin, OUTPUT);      
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);   
  pinMode(LED1, OUTPUT);
   pinMode(LED2, OUTPUT);
   pinMode(LED3, OUTPUT);
   pinMode(LED4, OUTPUT);
   pinMode(LED5, OUTPUT);
   pinMode(LED6, OUTPUT);
   pinMode(LED7, OUTPUT);
   pinMode(LED8, OUTPUT);  
}




void loop()
{
digitalWrite(LED1, HIGH);                      
  digitalWrite(LED2, HIGH);
  digitalWrite(LED3, HIGH);      
 digitalWrite(LED4, HIGH);
 digitalWrite(LED5, LOW);  
   digitalWrite(LED6, LOW);  
 digitalWrite(LED7, LOW); 
 digitalWrite(LED8 , LOW);
   delay(1000); 
   
  digitalWrite(LED1, LOW);                      
  digitalWrite(LED2, LOW);
  digitalWrite(LED3, LOW);      
 digitalWrite(LED4, LOW);
 digitalWrite(LED5, HIGH);  
   digitalWrite(LED6, HIGH);  
 digitalWrite(LED7, HIGH); 
 digitalWrite(LED8 , HIGH);
delay(1000); 

int newButtonState = digitalRead(buttonPin);
  if (newButtonState == HIGH && oldButtonState == LOW) {
buttonState = digitalRead(buttonPin);
      if(x == 1){
       digitalWrite(LED1, HIGH);
      digitalWrite(LED2, HIGH);
      digitalWrite(LED3, HIGH);
      digitalWrite(LED4, HIGH);
    
      digitalWrite(LED5, HIGH);
      digitalWrite(LED6, HIGH);
      digitalWrite(LED7, HIGH);
      digitalWrite(LED8, HIGH);
      x = 0;
    }
  }
}

You need to think about your logic a bit...

The "button state" is almost always 'off' (LOW, I assume?) so there's no point in comparing to the "old button state". (I'm assuming you're using a momentary push-button switch.)

However, there is an LED state or system on-off state, etc.

Then you want to read the button state and whenever the button is pushed toggle (invert) the on/off state.

And, get that basic logic working with one LED before you add a bunch of LEDs or otherwise complicate your code. :wink: ...And of course, make sure you can read the switch before that.

You will need some delay to "debounce" the switch, and right now you've got a 1 second delay. That may be OK but remember, nothing happens during the delay period so you can't read the button during the delay so you may have to hold the button for 1 second before anything happens. If that's not acceptable, or if you'll be adding more delays you'll need to use the timing method from the Blink Without Delay example.