Why is void loop won't looping and button 2 isn't working

i'm trying to make 5 LED alternating. button 1 is used to turn on the circuit (the LEDs will turn on and looping) and button 2 is used to turn off the circuit.

const int LED1 = 13;

const int LED2 = 12;

const int LED3 = 11;

const int LED4 = 10;

const int LED5 = 9;

const int butON = 3;

const int butOFF = 4;

int btnON = 0;

int btnOFF = 0;

void setup() {

// put your setup code here, to run once:

pinMode(LED1,OUTPUT);

pinMode(LED2,OUTPUT);

pinMode(LED3,OUTPUT);

pinMode(LED4,OUTPUT);

pinMode(LED5,OUTPUT);

pinMode(butON,INPUT);

pinMode(butOFF,INPUT);

digitalWrite(butON,HIGH);

digitalWrite(butOFF,HIGH);

digitalWrite(LED1,LOW);

digitalWrite(LED2,LOW);

digitalWrite(LED3,LOW);

digitalWrite(LED4,LOW);

digitalWrite(LED5,LOW);

}

void loop() {

// put your main code here, to run repeatedly:

if(digitalRead(butON)==0 && btnON==0){

digitalWrite(LED1, HIGH);

delay(1000);

digitalWrite(LED1, LOW);

digitalWrite(LED2, HIGH);

delay(1000);

digitalWrite(LED2, LOW);

digitalWrite(LED3, HIGH);

delay(1000);

digitalWrite(LED3, LOW);

digitalWrite(LED4, HIGH);

delay(1000);

digitalWrite(LED4, LOW);

digitalWrite(LED5, HIGH);

delay(1000);

digitalWrite(LED5, LOW);

btnON=1;

}

else if(digitalRead(butOFF)==0 && btnOFF==0){

digitalWrite(LED1,LOW);

digitalWrite(LED2,LOW);

digitalWrite(LED3,LOW);

digitalWrite(LED4,LOW);

digitalWrite(LED5,LOW); 

btnOFF=1;

}

delay(100);

}

Not gonna happen
Edit: idiot me

Please remember to use code tags when posting code.

How are your switches wired ?


FYI

i don't get it, can you explain more?

If butON is set to 3, how can it ever be 0 ?

Same thing with butOFF.

Ignore me. (Apart from the code tags bit)
It's been a long day.
Sorry

image

pinMode(butON,INPUT_PULLUP);

pinMode(butOFF,INPUT_PULLUP);

where should i put it? :cry:

Don’t know exactly what you wanted.

Explain to us what you think this line does ?

if(digitalRead(butON)==0 && btnON==0){

i actually don't know. i saw some examples on google does that. i wanted it to be button 1 is pressed

Maybe you intended:

if(digitalRead(butON)==0 && last_btnON==1){

it shows error

'last_btnON' was not declared in this scope

suggested alternative: 'btnON'

Well, you do need to create the last_btnON variable.

Going to assume what you want to do is, when the button goes from HIGH (not pushed) to LOW (pushed) then you want something to happen.

Is that correct ?

yes it is

const byte LED1   = 13;
const byte LED2   = 12;
const byte LED3   = 11;
const byte LED4   = 10;
const byte LED5   = 9;
const byte butON  = 3;

byte last_btnON   = HIGH;

//***********************************************************
void setup()
{
  pinMode(LED1, OUTPUT);
  pinMode(LED2, OUTPUT);
  pinMode(LED3, OUTPUT);
  pinMode(LED4, OUTPUT);
  pinMode(LED5, OUTPUT);
  pinMode(butON, INPUT_PULLUP);

  digitalWrite(LED1, LOW);
  digitalWrite(LED2, LOW);
  digitalWrite(LED3, LOW);
  digitalWrite(LED4, LOW);
  digitalWrite(LED5, LOW);
  
} //END of setup()


//***********************************************************
void loop()
{
  byte butONState = digitalRead(butON);

  //was there a change in state ?
  if (butONState != last_btnON)
  {
    //update to the new state
    last_btnON = butONState;

    //is the button pushed now ?
    if (butONState == LOW)
    {
      digitalWrite(LED1, HIGH);
      delay(1000);

      digitalWrite(LED1, LOW);
      digitalWrite(LED2, HIGH);
      delay(1000);

      digitalWrite(LED2, LOW);
      digitalWrite(LED3, HIGH);
      delay(1000);

      digitalWrite(LED3, LOW);
      digitalWrite(LED4, HIGH);
      delay(1000);

      digitalWrite(LED4, LOW);
      digitalWrite(LED5, HIGH);
      delay(1000);

      digitalWrite(LED5, LOW);
    }
  }

  delay(100);

} //END of loop()

it is pushed, but it's still not looping. thank you btw

What does this mean ?


Do any LEDs come ON ?

it's on, what i mean is that i want the on led to be 1-2-3-4-5-1-2-3-4-5-... but it's just 1-2-3-4-5

So you want the leads to be off at the start.

When you push the button, the LEDs will increment forever ?