Hi All,
I am new to the Arduino community, so forgive me if I am asking my question in the wrong place. I am trying to toggle a blinking LED by pressing a button. To be clear, I want to press the button to make the LED blink, and then press it again to make it stop blinking. However, when I run my code, I need to hold down the button to make the LED blink. If anyone can help, I would greatly appreciate it.
const int led = 9;
const int button = 8;
boolean lastButton = LOW;
boolean currentButton = LOW;
boolean ledOn = false;
void setup()
{
pinMode(led, OUTPUT);
pinMode(button, INPUT_PULLUP);
}
boolean debounce(boolean lastButton)
{
currentButton = digitalRead(button); // reads the button
if (lastButton != currentButton) // if the current button state differs from the previous...
{
delay(5); //wait 5ms
currentButton = digitalRead(button); //read the button again
}
return currentButton;
}
void loop()
{
boolean currentButton = debounce(lastButton);
if (currentButton == HIGH && lastButton == LOW)
{
//ledOn = !ledOn;
digitalWrite(led, HIGH);
delay(1000);
digitalWrite(led, LOW);
delay(1000);
}
//lastButton = currentButton;
//digitalWrite(led, ledOn);
}