Button counter not incrementing

const int ledPin = 3;
const int buttonPin = 2;
int buttonState = 0;
int counterNumber = 0;
int lastButtonState = 0;
void setup() {
  pinMode(buttonPin,INPUT);
  pinMode(ledPin,OUTPUT);
  Serial.begin(9600);
}

void loop() {
  while (counterNumber < 9){
    buttonSmash();
    }
    
 
    
}
void buttonSmash(){
 
    buttonState = digitalRead(buttonPin);
    counterNumber = 0;
    if (buttonState != lastButtonState){
      if (buttonState == HIGH){
        counterNumber++;
        Serial.println("on");
        Serial.print("number of button pushes: ");
        Serial.println(counterNumber);
        }else {
          Serial.println("off");
          
        }
        delay(50); 
        
        }
        lastButtonState = buttonState;
     if (counterNumber == 1){
      analogWrite(ledPin, 255);   
      }
    if (counterNumber == 2){
      analogWrite(ledPin, 150);
      }
    if (counterNumber == 3){
      analogWrite(ledPin, 76);
      }
    if (counterNumber == 4){
      analogWrite(ledPin, 0);
    }
    if (counterNumber == 5){
      analogWrite(ledPin, 255);
    }
    if (counterNumber == 6){
      analogWrite(ledPin, 150);
    }
    if (counterNumber == 7){
      analogWrite(ledPin, 76);
    }
    if (counterNumber == 8){
      analogWrite(ledPin, 0);
    }
    
    }

I am trying to write a program that only uses one while statement in my void loop, to turn an LED on using PWM so for button press #1 LED = 255 (100%), second press is 60% and third is 30% the fourth press should be off, I then want this loop to repeat one more time and then cancel out.

I think I almost have it down, however in the code i provided, my counter only goes from 0 to 1 and does not increment further.

not sure where the issue is in my code but any pointers would be absolutely amazing, thank you in advance.

This is how I have my button hooked up, which is why I did not use the pinMode(buttonPin, INPUT_PULLUP);

void buttonSmash(){
 
    buttonState = digitalRead(buttonPin);
    counterNumber = 0;

Do you think that this could be the reason ?

wow.....I just don't even know what to say lol.

I am very new to programming and arduino. so little stuff like this is what always hangs me up. thank you so much for your quick reply !!!

buttonState is transient; it does not need global scope (but lastBttonState does; or better still, local static scope)

Suggest you get into the habit of connecting your switches between the Arduino input pin and GND.

S3 circuit below is how we recommend wiring switches, then use INPUT_PULLUP.

A switch press on a N.O. switch gives a LOW.

1 Like

thank you for the clarification, this will definitely help me on future projects!!

Hi, @th3rung0
third post on the same project?

" Using a for loop to increment counter to 8 and cancel "

" Using while loops to PWM an led controlled by a button "

Please read carefully
"How to get the best out of this forum "

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.