2

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);
}
Deltharis
  • 2,320
  • 1
  • 18
  • 29
Ronnie L
  • 123
  • 1
  • 3
  • 9
  • 2
    Is DailyAlarm *supposed* to contain an infinite loop? :} – user2864740 Feb 10 '18 at 02:10
  • I was hoping the Daily Alarm would keep blinking while the buttonState was LOW, and when I pushed the button, the buttonState would change to HIGH, thus stopping the loop. Somehow that isn't happening – Ronnie L Feb 10 '18 at 02:18
  • 1
    A prudent step might be to combine `blink(..)` *into* `loop()` - it can be called from there, doesn't necessarily need to have code moved over - and ditch the infinite loop shown. The reason is this: [*there is only a single "thread" in an Arduino program*](https://arduino.stackexchange.com/questions/286/how-can-i-create-multiple-running-threads), so when the Alarm interrupt is triggered it enters into the blinkloop-that-never-exists and the interrupt cannot complete and thus execution *never returns to the main loop* :} – user2864740 Feb 10 '18 at 02:30

1 Answers1

0
int buttonState; // variable for reading the pushbutton status

You haven't initialized the button state

Try

boolean buttonState = LOW

And Don't mix integers and booleans use this

boolean lastButtonState = LOW;

instead of

int lastButtonState = LOW;
anilkunchalaece
  • 388
  • 2
  • 10