Make button pushes add to variable?

Trying to make it so that every time I push this button, it blinks one more time than it did before. I push it once, one blink. I push it again, two blinks. That’s the idea. Why isn’t this working? Thanks!

const int buttonPin = 2;     // the number of the pushbutton pin
const int ledPin =  13;      // the number of the LED pin

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status
int buttonPushes = 0; // number of button pushes

void setup() {
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);      
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);     

void loop(){
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState == HIGH) {     int buttonPushes = buttonPushes + 1;   
    // turn LED on:    
    for(int c = 0; c <= buttonPushes; c++) {

  digitalWrite(ledPin, HIGH);
  digitalWrite(ledPin, LOW);
  else {
    // turn LED off:
    digitalWrite(ledPin, LOW); 

You have not told us what DOES happen.

You have no debounce on your press button. This may cause the counter to increment many times on one press.


Because you're declaring int buttonPushes as a local variable within your loop.

Every time your loop runs, it will recreate buttonPushes (and initialise it to 0);

YES I know you declare int buttonPushes at the top of your code, but this isn't the variable used within your loop, as you've included int just before it within your loop. (therefore creating a new variable).

So try removing that simple little "int" before the word buttonPushes within your loop function.

You also need to look at the state change detection example. You want to change what is happening when the switch BECOMES pressed, not when the switch IS pressed.

For any given press of the switch, the loop() function could determine that the switch IS pressed millions of times. Not in your case, with the stupid delay(), but that's a story for another (of the countless millions of how do I not use delay()) thread(s).

Thank you, KenF! I am quite new to this and really appreciate people taking time out to address such simple issues. :)