Blinking LED with a debounced pushbutton

Hello I am a beginner in arduino and I am having problems with my code. I am trying to make an LED blink at a rate of 2 seconds by pushing a debounced button. I want the LED to blink when I push the button and stop immediately after when i unpress the button. I do not want to use delay.

My homework is much more complicated, but this helps me a lot.

Please Help!!

So, please explain what it is doing currently; feel free to post your existing code using the “Reply” button and the code tag button “</>”. We’ll work through this together.

/* Blink without Delay

 Turns on and off a light emitting diode (LED) connected to a digital
 pin, without using the delay() function.  This means that other code
 can run at the same time without being interrupted by the LED code.

 The circuit:
 * LED attached from pin 13 to ground.
 * Note: on most Arduinos, there is already an LED on the board
 that's attached to pin 13, so no hardware is needed for this example.

 created 2005
 by David A. Mellis
 modified 8 Feb 2010
 by Paul Stoffregen
 modified 11 Nov 2013
 by Scott Fitzgerald


 This example code is in the public domain.

 http://www.arduino.cc/en/Tutorial/BlinkWithoutDelay
 */

// constants won't change. Used here to set a pin number :
const int ledPin =  13;      // the number of the LED pin

// Variables will change :
int ledState = LOW;             // ledState used to set the LED

// Generally, you should use "unsigned long" for variables that hold time
// The value will quickly become too large for an int to store
unsigned long previousMillis = 0;        // will store last time LED was updated

// constants won't change :
const long interval = 1000;           // interval at which to blink (milliseconds)

void setup() {
  // set the digital pin as output:
  pinMode(ledPin, OUTPUT);
}

void loop() {
  // here is where you'd put code that needs to be running all the time.

  // check to see if it's time to blink the LED; that is, if the
  // difference between the current time and last time you blinked
  // the LED is bigger than the interval at which you want to
  // blink the LED.
  unsigned long currentMillis = millis();

  if (currentMillis - previousMillis >= interval) {
    // save the last time you blinked the LED
    previousMillis = currentMillis;

    // if the LED is off turn it on and vice-versa:
    if (ledState == LOW) {
      ledState = HIGH;
    } else {
      ledState = LOW;
    }

    // set the LED with the ledState of the variable:
    digitalWrite(ledPin, ledState);
  }
}
/*
  Button

 Turns on and off a light emitting diode(LED) connected to digital
 pin 13, when pressing a pushbutton attached to pin 2.


 The circuit:
 * LED attached from pin 13 to ground
 * pushbutton attached to pin 2 from +5V
 * 10K resistor attached to pin 2 from ground

 * Note: on most Arduinos there is already an LED on the board
 attached to pin 13.


 created 2005
 by DojoDave <http://www.0j0.org>
 modified 30 Aug 2011
 by Tom Igoe

 This example code is in the public domain.

 http://www.arduino.cc/en/Tutorial/Button
 */

// constants won't change. They're used here to
// set pin numbers:
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

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) {
    // turn LED on:
    digitalWrite(ledPin, HIGH);
  } else {
    // turn LED off:
    digitalWrite(ledPin, LOW);
  }
}

This is my code. What I want to do is the following:

I have to do an Arduino Sketch using 2 pushbuttons and an LED. Start the LED blinking at 1 Hz. The buttons will be used to represent faster and slower settings. When Button 1 is pressed the LED blinking will get faster, and when the other button is pressed it will get slower. Set the limits to maximum speed = 20 Hz, minimum speed = 0.2 Hz. Do not use the delay function.

// constants won’t change. They’re used here to
// set pin numbers:
const int buttonFast = 7;    // the number of the fast button pin
const int buttonSlow = 8;    // the number of the slow button pin
const int ledPin =  13;      // the number of the LED pin

// Variables will change:
int ledState = LOW;             // ledState used to set the LED
int buttonState;             // the current reading from the input pin
int lastButtonState = LOW;   // the previous reading from the input pin

// Generally, you should use "unsigned long" for variables that hold time
// The value will quickly become too large for an int to store
unsigned long LastTime = 0;        // will store last time LED was updated

// constants won't change:
const long interval = 1000;           // interval at which to blink (milliseconds)
const long fast = 50;
const long slow = 5000;

void setup() {
  pinMode(buttonFast, INPUT);
  pinMode(buttonSlow, INPUT);
  pinMode(ledPin, OUTPUT);
}

void loop() {
  unsigned long CurrentTime = millis();
  if ((digitalRead(buttonFast) == LOW) && (digitalRead(buttonSlow)) == LOW) {
    if (CurrentTime - LastTime >= interval) {
      // save the last time you blinked the LED
      LastTime = CurrentTime;

      // if the LED is off turn it on and vice-versa:
      if (ledState == LOW) {
        ledState = HIGH;
      } else {
        ledState = LOW;
      }

      // set the LED with the ledState of the variable:
      digitalWrite(ledPin, ledState);
    }
  }
  if ((digitalRead(buttonFast) == HIGH) && (digitalRead(buttonSlow) == LOW)) {
    if (CurrentTime - LastTime >= fast) {
      // save the last time you blinked the LED
      LastTime = CurrentTime;

      // if the LED is off turn it on and vice-versa:
      if (ledState == LOW) {
        ledState = HIGH;
      } else {
        ledState = LOW;
      }

      // set the LED with the ledState of the variable:
      digitalWrite(ledPin, ledState);
    }
  }
  if ((digitalRead(buttonFast) == LOW) && (digitalRead(buttonSlow)) == HIGH) {
    if (CurrentTime - LastTime >= slow) {
      // save the last time you blinked the LED
      LastTime = CurrentTime;

      // if the LED is off turn it on and vice-versa:
      if (ledState == LOW) {
        ledState = HIGH;
      } else {
        ledState = LOW;
      }

      // set the LED with the ledState of the variable:
      digitalWrite(ledPin, ledState);
    }
  }
}

arduinobeginner8:
This is my code.

...

What is not working?

Why you update the ledPin three time in a loop?
update the ledPin only one time at the end of code.

get a library called "button"

it really helped me with the debouncing, just just call (button.pressed()) , (button.released()) or (button.toggled())
way easier and cleaner

So, let's first explain why the code is not working-
When you press and hold any button, LastTime = CurrentTime. This means

if (CurrentTime - LastTime >= interval) { .. will never be true because the value will always be zero.

When you release the button, the counting begins. However, only
if ((digitalRead(buttonFast) == LOW) && (digitalRead(buttonSlow)) == LOW) {... will be true;

Since we're only interested in the assigned value(s) of interval, the code can be reduced by simply changing the time interval via a button read.

Changes -
1.) removed "constant" from "const long interval" since it will be changing.
2.) add the constant variable "normal" with a value of 1000 since it will not be changing value.
3.) assigned value of "interval" in void setup
4.) reduced code to assign value of "interval" based on button pushed or no buttuns pushed.

// constants won't change. They're used here to
// set pin numbers:
const int buttonFast = 7;    // the number of the fast button pin
const int buttonSlow = 8;    // the number of the slow button pin
const int ledPin =  13;      // the number of the LED pin

// Variables will change:
int ledState = LOW;             // ledState used to set the LED
int buttonState;             // the current reading from the input pin
int lastButtonState = LOW;   // the previous reading from the input pin

// Generally, you should use "unsigned long" for variables that hold time
// The value will quickly become too large for an int to store
unsigned long LastTime = 0;        // will store last time LED was updated

// constants won't change:
const long normal = 1000;           // interval at which to blink (milliseconds)
const long fast = 50;
const long slow = 5000;
long interval;
void setup() {
  pinMode(buttonFast, INPUT);
  pinMode(buttonSlow, INPUT);
  pinMode(ledPin, OUTPUT);

  interval = normal;
}

void loop() {
  unsigned long CurrentTime = millis();
    if (CurrentTime - LastTime >= interval) {
      // save the last time you blinked the LED
      LastTime = CurrentTime;

      // if the LED is off turn it on and vice-versa:
      if (ledState == LOW) {
        ledState = HIGH;
      } else {
        ledState = LOW;
      }

      // set the LED with the ledState of the variable:
      digitalWrite(ledPin, ledState);
    }

  if ( (digitalRead(buttonFast) == HIGH) && (digitalRead(buttonSlow) == LOW)) {
    interval = fast;
  }
  if ((digitalRead(buttonFast) == LOW) && (digitalRead(buttonSlow)) == HIGH) {
    interval = slow;
  }

    if ((digitalRead(buttonFast) == LOW) && (digitalRead(buttonSlow)) == LOW) {
    interval = normal;
  }
}

The above should lay a working foundation so as to enable you to focus on your debounce criteria; and, hopefully, you learned something along the way.