BLYNK
HOME       📲 GETTING STARTED       📗 DOCS       ❓HELP CENTER       👉 SKETCH BUILDER

Comparing stored time to RTC time

I have the following code:


#include <TimeLib.h>
#include <LiquidCrystal.h>
#include <Blynk.h>
#include <SoftwareSerial.h>
#include <BlynkSimpleStream.h>
#include <WidgetRTC.h>
SoftwareSerial SwSerial(10, 11); // RX, TX
#define BLYNK_PRINT SwSerial
char auth[] = "OxtgJkMzg6b5u1jSQ1og-aP69__JxHxz";
WidgetRTC rtc;
BlynkTimer timer;
BlynkTimer alarmTimer;
LiquidCrystal lcd(13, 12, 11, 10, 9, 8);
boolean alarmConfirm = false;
boolean alarmDisplayed = false;
String savedAlarmString = "";
int alarmmins, alarmhours = 0;
String aMpM() {
  //return string for am or pm
  if (isAM()) {
    return "A.M.";
  } else {
    return "P.M.";
  }

}

String digitMinutes(int input) {
  //create empty string
  String minutes = String(input);
  //check if current minute is less than 10. (0,1,2,3 etc)
  if (input < 10) {
    //add 0 to beginning of the string version of current minute (00, 01, 02, 03 etc)
    minutes = "0" + String(input);
    if (input < 1) {
      return "00";
    } else {
      return minutes;
    }
  } else {
    return minutes;
  }


  return minutes;
}


String alarmFormat(TimeInputParam t) {
  int houroutput = 0;
  String ampm = "";
  if (t.getStartHour() >= 12) {
    if (t.getStartHour() == 12) {
      houroutput = 12;
    } else {
      houroutput = t.getStartHour() - 12;
    }
    ampm = " P.M.";
  } else {
    if (t.getStartHour() == 0) {
      houroutput = 12;
    } else {
      houroutput = t.getStartHour();
    }
    ampm = " A.M.";
  }
  return String(houroutput) + ":" + digitMinutes(t.getStartMinute()) + ampm;
}

void clockDisplay() {
  Blynk.sendInternal("rtc", "sync");
  if (isAlarmReached()) {
    alarmDisplay();
    return;
  }
  if (alarmConfirm || alarmDisplayed) {
    return;
  }
  lcd.clear();
  lcd.setCursor(3, 0);
  if (hourFormat12() < 10) {
    lcd.print(" " + String(hourFormat12()));
  } else {
    lcd.print(hourFormat12()); // display 12 hour time
  }
  lcd.setCursor(5, 0);
  lcd.print(":" + digitMinutes(minute()));
  lcd.setCursor(9, 0);
  lcd.print(aMpM());
}

BLYNK_CONNECTED() {
  rtc.begin();
  clockDisplay();
}

BLYNK_WRITE(V1) {
  TimeInputParam t(param);
  if (alarmDisplayed) {
    return;
  }
  if (t.hasStartTime()) {
    alarmConfirm = true;
    //save the alarmtime as seconds in localAlarm
    lcd.clear();
    lcd.setCursor(3, 0);
    lcd.print(alarmFormat(t));
    lcd.setCursor(3, 1);
    lcd.print("Alarm Set.");
    savedAlarmString = alarmFormat(t);
    alarmhours = t.getStartHour();
    alarmmins = t.getStartMinute();
    delay(3000);
    alarmConfirm = false;
    clockDisplay();
  }

}

boolean isAlarmReached() {
  if (hour() == alarmhours && minute() == alarmmins) {
    return true;
  } else {
    return false;
  }
}
boolean alarmShutoff() {
  /* while(analogRead(A0) >= 900) {
     return true;
    }
    return false;
  */
  return false;
}

void alarmDisplay() {
  if (!alarmDisplayed) {
    while (!alarmShutoff) {
      alarmDisplayed = true;
      lcd.clear();
      lcd.setCursor(3, 0);
      lcd.print(savedAlarmString);
      lcd.setCursor(3, 1);
    }
    alarmDisplayed = false;
  }
}



void setup() {
  // put your setup code here, to run once:
  lcd.begin(16, 2);
  SwSerial.begin(9600);
  Serial.begin(9600);
  Blynk.begin(Serial, auth);
  setSyncInterval(10);
  timer.setInterval(5000L, clockDisplay);
  pinMode(7, INPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
  Blynk.run();
  timer.run();
  alarmTimer.run();
}

And I’m trying to make it so that when the time fetched from TimeLib/WidgetRTC is the same as the time stored after using the TimeInputWidget are the same, the device will display the alarm clock time.

The functions of note are:

isAlarmReached() is a boolean that returns whether the current time is equivalent to the stored time.

BLYNK_WRITE(V1) stores the time that the user inputs.

alarmDisplay() displays the set message and will also do some other stuff, basically needs to be called when the alarm time is reached.

clockDisplay() displays the time normally, but can be interrupted by the alarm setting confirmation screen and the alarm reached screen.

But as it stands right now, when the time I entered is reached, the device behaves as if nothing happened. When I enter my alarm time it works, interrupts the clock etc, but when it reaches the time I set it just ignores it. Any help?