Problem with "if else" statement

const int buttonPin = 2;  
                            
const int output1 = 9;
const int output2 = 10;
const int output3 = 11;
//initialize pins
int buttonPushCounter = 0;
int buttonState = 0;
int lastButtonState = 0;

//initialize numbers
void setup() {
pinMode(buttonPin,INPUT);
pinMode(output1,OUTPUT);
pinMode(output2,OUTPUT);
pinMode(output3,OUTPUT);
//pin set up
}

void loop() {
 int buttonState = digitalRead(buttonPin);        //sub in value (HIGH or LOW) for "buttonState"
  if (buttonState != lastButtonState)             //compare the two button states, continue if it change
  {
     if(buttonState ==HIGH)                       //check if the button is on high
      {
       buttonPushCounter++;                       //add a count to the amount of pushes, used for next step
       if (buttonPushCounter=1)                   //turn on all led if it is the first time button is pressed
       {
         digitalWrite(output1, HIGH);
         digitalWrite(output2, HIGH);
         digitalWrite(output3, HIGH);
       }
       else if (buttonPushCounter=2)              //turn on two led if it is the second time button is pressed
       {
         digitalWrite(output1, LOW);
         digitalWrite(output2,HIGH);
         digitalWrite(output3,HIGH);
       }
       else if (buttonPushCounter=3)              //turn on one led if it is the third time button is pressed 
       {
         digitalWrite(output1,LOW);
         digitalWrite(output2,LOW);
         digitalWrite(output3,HIGH);
       }
       else if (buttonPushCounter=4)              //turn all led off if it is the fourth time buttons is pressed
       {
          digitalWrite(output1,LOW);
          digitalWrite(output2,LOW);
         digitalWrite(output3,LOW);
          int buttonPushCounter = 0;              //clear all count back to 0 (original)
          int buttonState = 0;
          int lastButtonState = 0;
        }
    }
    delay(50);                                    //delay to avoid fast track
  }
  lastButtonState=buttonState;                    //set the lastButtonState to buttonState for the next loop
}

For this code I am trying to turn on 3 LED on first push of button, two on second push, one on third push and none on the forth, and recycle this process over and over again. I wrote this code and how ever does not work. What is currently happening is that it only turns on all three of the LEDs on first push, and stops responding later. Please tell me what I am doing wrong here.

The problem is usually rookie error on your part. heheh....only kidding. Anyway, it sometimes happens with experienced programmers too.

One thing I can see is.... the 'if statement'....when you're doing comparisons, such as:

if (counter == 1)

.... the symbol == is a "code" or instruction for telling the processor to provide a RESULT for this question "is counter equal to 1?". So, if counter happens to be '0' at the time, then the RESULT would be zero. In that case, the processor would then interpret the line as:

if (0)

..... which would mean.... don't execute whatever follows the 'if' statement.

So...... when using if statements, make sure to use the correct symbols.... eg. if (counter == 1)

not if (counter = 1)

The '=' equal sign, ie. a single equal symbol is a code for an assignment.... eg. cnt = 1 means, assign the value 1 to a place in processor memory linked to the variable label 'cnt'.

(deleted)

In the last if you are creating a new buttonPushCounter variable that only exists inside that if block. The one you’re using to figure which led to turn on isn’t affected. Lose the “int” there. Same on the other variables you’re trying to reset.

Same issue on the first line of loop. You want that global variable to be the one you use not create another new one there in a different scope.