Debounce button to toggle blinking LED

Hello,

I am trying to write a simple set of instructions to monitor a debounced button. When the button is pressed, it want the output led to begin blinking. When pressed again, stop the blinking. Continue looping to check button status.

Have little experience with programming, trying to learn and understand the Arduino code.

Please see my code, I'm sure there are several things wrong with it.

When I run the program, the output led is initially LOW. When the button is pressed, the led starts blinking. When the button is pressed again, nothing happens. It continues to blink.


// constants won't change. They're used here to set pin numbers:
const int buttonPin = 2;    // DB the number of the pushbutton pin
const int ledPin = 13;      // DB the number of the LED pin

// Variables will change:
int ledState = LOW;         // DB the current state of the output pin
int buttonState;             // DB the current reading from the input pin
int lastButtonState = LOW;   // DB the previous reading from the input pin
long previousMillis = 0; //Blink

// the following variables are unsigned longs because the time, measured in
// milliseconds, will quickly become a bigger number than can be stored in an int.
unsigned long lastDebounceTime = 0;  // DB the last time the output pin was toggled
unsigned long debounceDelay = 50;    // DB the debounce time; increase if the output flickers


void setup() {
  pinMode(buttonPin, INPUT); //DB
  pinMode(ledPin, OUTPUT); //DB

  // set initial LED state
  digitalWrite(ledPin, ledState); //DB
}

long interval = 500; //Blink

void loop() {
  // read the state of the switch into a local variable:
  int reading = digitalRead(buttonPin); //DB

  // check to see if you just pressed the button
  // (i.e. the input went from LOW to HIGH), and you've waited long enough
  // since the last press to ignore any noise:

  // If the switch changed, due to noise or pressing:
  if (reading != lastButtonState) { //DB
    // reset the debouncing timer
    lastDebounceTime = millis(); //DB
  } //DB

  if ((millis() - lastDebounceTime) > debounceDelay) { //DB
    // whatever the reading is at, it's been there for longer than the debounce
    // delay, so take it as the actual current state:

    // if the button state has changed:
    if (reading != buttonState) { //DB
      buttonState = reading; //DB

      // only toggle the LED if the new button state is HIGH
      if (buttonState == HIGH) { //DB
        ledState = !ledState; //DB
      } //DB
    } //DB
  } //DB

  // set the LED:
  digitalWrite(ledPin, ledState); //DB

  //Blink code starts

  if (buttonState == HIGH) {

    unsigned long currentMillis = millis(); //Blink

    if (currentMillis - previousMillis > interval) { //Blink

      previousMillis = currentMillis; //Blink
      if (ledState == LOW) //Blink
        ledState = HIGH; //Blink
      else //Blink
        ledState = LOW; //Blink

      digitalWrite(ledPin, ledState); //Blink

    }
  }
  else

    //Blink code stops
    // save the reading. Next time through the loop, it'll be the lastButtonState:
    lastButtonState = reading; //DB
} //DB

Hello
What is going "wrong"?

Debouncing Switches in Arduino has an all in one class that does the debounce for you which will simplify your sketch.
There are other classes out there also, this is a common problem.

Led will not stop blinking after the initial press.

How to write Timers and Delays in Arduino has a simple timer class millisDelay that lets you start/stop/restart timers.
Use this structure to integrate them, and debounce, into your code (
Multi-tasking in Arduino
multitaskingDiagramSmall

@vcholman, your topic has been moved to a more suitable location on the forum.

Hello
your sketch donĀ“t need any debouncing. The button flickering will be "eaten" by the blink task.

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