I have a program (copied below) that has an alarm that starts in 10 seconds, which starts a LED that blinks every two seconds. I would like to push a button that turns off the alarm / LED. The LED blinks as intended but does not turn off when I push the button. Any idea why? Code below:
#include <Time.h>
#include <TimeAlarms.h>
#define buttonPin 2 // assigns pin 2 as buttonPin
#define ledPin 13 // assigns pin 13 as ledPin
int buttonState; // variable for reading the pushbutton status
int lastButtonState = LOW;
unsigned long lastDebounceTime = 0; // the last time the output pin was toggled
unsigned long debounceDelay = 50; // the debounce time; increase if the output flickers
void setup() {
Serial.begin(9600); // start Serial Monitor
setTime(0, 0, 0, 1, 1, 2018); // set time to 00:00:00 Jan 1 2018
Alarm.alarmRepeat(0, 0, 10, DailyAlarm); // Alarm triggered in 10 sec
pinMode(ledPin, OUTPUT); // assigns ledPin as led output pin.
pinMode(buttonPin, INPUT); // assigns buttonPin as input pin
// digitalWrite(ledPin, ledState); // LED is off initially
}
void loop() {
digitalClockDisplay();
Alarm.delay(1000); // wait one second between clock display
int reading = digitalRead(buttonPin);
if (reading != lastButtonState) {
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
if (reading != buttonState) {
buttonState = HIGH;
Serial.print(buttonState);
}
}
}
void DailyAlarm() {
Serial.println("Alarm");
while (buttonState == LOW) {
blink(2000); // blink every 2s
}
}
void blink(int period) {
digitalWrite(ledPin, HIGH);
delay(period / 2);
digitalWrite(ledPin, LOW);
delay(period / 2);
}
void digitalClockDisplay() {
// digital clock display of the time
Serial.print(hour());
printDigits(minute());
printDigits(second());
Serial.println();
}
void printDigits(int digits) {
Serial.print(":");
if (digits < 10)
Serial.print('0');
Serial.print(digits);
}