How do you make an LED increase in blinks based total number of times a button has been pressed

I am trying to write a program where the led blink 1 time more when a button is pressed. For example, if it was the first time I pushed a button, the led will blink once, if it was my second time pushing the button, it will blink twice, and if it was my third time pushing the button, it will blink thrice. My code can be seen below.

Thanks in advance!

int count = 0;
int buttonState = 0;

void setup()
{
pinMode(13, OUTPUT);
pinMode(1, INPUT);
buttonState = digitalRead(1);
while (count <= 5)
{
if (count == 1 && buttonState == HIGH)
{
digitalWrite(13, HIGH);
delay(1000);
digitalWrite(13,LOW);
}

else if (count == 2 && buttonState == HIGH)
{
digitalWrite(13, HIGH);
delay(1000);
digitalWrite(13,LOW);
delay(1000);
digitalWrite(13, HIGH);
delay(1000);
digitalWrite(13,LOW);
}

else if (count == 3 && buttonState == HIGH)
{
digitalWrite(13, HIGH);
delay(1000);
digitalWrite(13,LOW);
delay(1000);
digitalWrite(13, HIGH);
delay(1000);
digitalWrite(13,LOW);
delay(1000);
digitalWrite(13, HIGH);
delay(1000);
digitalWrite(13,LOW);
}

else if (count == 4 && buttonState == HIGH)
{
digitalWrite(13, HIGH);
delay(1000);
digitalWrite(13,LOW);
delay(1000);
digitalWrite(13, HIGH);
delay(1000);
digitalWrite(13,LOW);
delay(1000);
digitalWrite(13, HIGH);
delay(1000);
digitalWrite(13,LOW);
delay(1000);
digitalWrite(13, HIGH);
delay(1000);
digitalWrite(13,LOW);
}

else if (count == 5 && buttonState == HIGH)
{
digitalWrite(13, HIGH);
delay(1000);
digitalWrite(13,LOW);
delay(1000);
digitalWrite(13, HIGH);
delay(1000);
digitalWrite(13,LOW);
delay(1000);
digitalWrite(13, HIGH);
delay(1000);
digitalWrite(13,LOW);
delay(1000);
digitalWrite(13, HIGH);
delay(1000);
digitalWrite(13,LOW);
delay(1000);
digitalWrite(13, HIGH);
delay(1000);
digitalWrite(13,LOW);
}

else
{
  digitalWrite(13,LOW);
}

count = count + 1;

}
}

Start with the StateChangeDetection example in the IDE. It will show you how to detect when a button becomes pressed rather than when it is pressed and how to increment a button press counter

Use the value of the counter in a function to blink the LED that number of times

Please follow the advice given in the link below when posting code , use code tags and post the code here to make it easier to read and copy for examination

1 Like
  1. Please post your code properly.
  2. Please post your entire sketch. The one you posted is missing the loop() function.
  3. You never re-read the button state during your while loop and therefore buttonState would never change.
  4. Do you want the pattern to keep repeating until you press the button again?
  5. Use of delay() and reading inputs don't work well together.
  6. Review the following tutorials:
    StateChangeDetection
    BlinkWithoutDelay
  7. Get one thing working at a time. First make sure you can detect the button correctly, then add the other code.

You can also create your own function, maybe call it blink() that will do one blink. Then use a for() loop to call that function as many times as necessary. Much better the duplicating all that blinking code over and over...

something like this maybe:
(Compiles, NOT tested!)

/*
  State change detection (edge detection)

   https://www.arduino.cc/en/Tutorial/BuiltInExamples/StateChangeDetection
*/

// this constant won't change:
const int  buttonPin = 2;    // the pin that the pushbutton is attached to
const int ledPin = 13;       // the pin that the LED is attached to
const int BlinkPeriod = 500;       // 500ms ON, 500ms OFF

// Variables will change:
int buttonPushCounter = 0, oldbuttonPushCounter = 0, BlinkCounter = 0;   // counter for the number of button presses and LED binks
int buttonState = 0;         // current state of the button
int lastButtonState = 0;     // previous state of the button
unsigned long oldtime;

void setup() {
  // initialize the button pin as a input:
  pinMode(buttonPin, INPUT);
  // initialize the LED as an output:
  pinMode(ledPin, OUTPUT);
  // initialize serial communication:
  //Serial.begin(9600);
  oldtime = millis();
}


void loop() {
  // read the pushbutton input pin:
  buttonState = digitalRead(buttonPin);

  // compare the buttonState to its previous state
  if (buttonState != lastButtonState) {
    // if the state has changed, increment the counter
    if (buttonState == HIGH) {
      // if the current state is HIGH then the button went from off to on:
      buttonPushCounter++;
      //Serial.println("on");
      //Serial.print("number of button pushes: ");
      //Serial.println(buttonPushCounter);
    } else {
      // if the current state is LOW then the button went from on to off:
      //Serial.println("off");
    }
    // Delay a little bit to avoid bouncing
    delay(50);
  }
  // save the current state as the last state, for next time through the loop
  lastButtonState = buttonState;


  // set number of blinks
  if (buttonPushCounter  != oldbuttonPushCounter) {
    oldbuttonPushCounter = buttonPushCounter;
    if (BlinkCounter == 0) {
      BlinkCounter = 2 * oldbuttonPushCounter; //x2 since will be counting down in ON and OFF timing
    }
    else {
      BlinkCounter += 2; //LED currently blinking. +2 for additional button press detected.
    }
  }

  if ((BlinkCounter > 0) && ((millis() - oldtime) >= BlinkPeriod)) {
    digitalWrite(ledPin, !digitalRead(ledPin)); //toggle pin output to blink LED
    oldtime = millis();
    --BlinkCounter;
  }

}

hope that helps

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