TimeAlarms Library alarms not calling functions

Hey guys!

Having trouble with getting the Alarm.alarmRepeat in my void setup to call the function dailyReset ()
I have simulated the RTC running past the 1 am time and it never calls the function.

I am also wondering how the alarm once alarms in the setAlarms() functions should be called. I am calling the setAlarms() in the setup, like the example i am using. However none of the alarms seem to be working.

The code below is as much of the relevant sections as I can fit into this post. I am leaving out all of the operational functions (doorOpen, doorClosed, ect) as those are all working.

// Date and time functions using a DS3231 RTC connected via I2C and Wire Lib

#include <Wire.h>
#include <RTClib.h>
#include <Time.h>
#include <TimeAlarms.h>
  #define dtNBR_ALARMS 20
#include <Adafruit_MCP23017.h>
#include <Adafruit_RGBLCDShield.h>
#include <NewPing.h>
#include <DHT.h>
#include <TimeLord.h>
#define DHTTYPE DHT11




RTC_DS1307 RTC;
Adafruit_RGBLCDShield lcd = Adafruit_RGBLCDShield();

const boolean SERIAL_DEBUG = true;

//Set Pins
const int doorRelay = 7; //DPDT K7 and 8 relays 
const int pumpRelay = 8; //K6 sprayer pump on 
const int lightRelay = 9; // K5 Light Relay
const int foodLevelTrig = 36; //Trigger for the food level sensor
const int foodLevelEcho = 37; //Echo for the food level sensor
const int feederMaxDist = 42; //Max dist for library
const int dhtcoopPin = 22; //Read Coop DHT 11 

 float hourMinuteToHour(int hour, int minute){
return hour + minute/60.0f;
}
//define vars
long timer = millis(); //A timer 

//Variables for monitoring, printing, tweeting, various indications
int pumpState = 0;
int doorState = 0;
int feederState = 0;
int lightState = 0;
float coopTemp = 0.0;
int DOOR_SHUT_AFTER_SUNSET_MIN = 20;
int LIGHT_PER_DAY_HRS = 16;
int LIGHT_ON_BEFORE_SUNSET_HRS = 1;
boolean isdoorOpen = false;
int initBit = 0;


// Location constants
// Location constants
const float LATITUDE = 31.729389,
    LONGITUDE = -97.106217;
const int TIMEZONE = -5;

//time control variables
TimeLord coopTimeLord;

byte openDoorHr, openDoorMin, closeDoorHr, closeDoorMin,
  lightOnHr, lightOnMin, lightOffHr, lightOffMin,
   waterHr, waterMin, feedMorningHr, feedMorningMin,
   feedAfternoonHr, feedAfternoonMin,
   feedEveningHr, feedEveningMin;


NewPing sonar(foodLevelTrig, foodLevelEcho, feederMaxDist);
DHT dht(dhtcoopPin, DHTTYPE);

void setup() {
  // Set the relays to off immediately
  digitalWrite(doorRelay, HIGH);
  digitalWrite(pumpRelay, HIGH);
  

  //begin libraries
  Serial.begin(9600);
  Wire.begin();
  RTC.begin();
  dht.begin();
  lcd.begin(16, 2);

   lcd.print("Twin Flame Acres");
  lcd.setBacklight(WHITE);

  // Set the pinmode
  pinMode(pumpRelay, OUTPUT);
  pinMode(doorRelay, OUTPUT);
  pinMode(lightRelay, OUTPUT);
 // pinMode(doorButton, INPUT_PULLUP); //Input pullup becuse button sinks to DC return


  // Notify if the RTC isn't running
  if (! RTC.isrunning()) {
    Serial.println("RTC is NOT running");
  }

  // Get time from RTC
  DateTime current = RTC.now();
  DateTime compiled = DateTime(__DATE__, __TIME__);
  if (current.unixtime() != compiled.unixtime()) {
    Serial.println("RTC is older than compile time! Updating");
    RTC.adjust(DateTime(__DATE__, __TIME__));
  }

  // Use RTC to set time now 
  setTime(current.hour(), current.minute(), current.second(), current.day(), current.month(), current.year());
  time_t n = now();
  
  coopTimeLord.TimeZone(TIMEZONE * 60);
  coopTimeLord.Position(LATITUDE, LONGITUDE);
  
 initState();
 setAlarms();

 Alarm.alarmRepeat( 1, 0, 0, dailyReset); 
}


void loop() {
 
   lcd.setCursor(0, 1);
  //read various states 
  pumpState = digitalRead(pumpRelay);
  doorState = digitalRead(doorRelay);


  float h = dht.readHumidity();
  // Read temperature as Celsius (the default)
  float t = dht.readTemperature();
  // Read temperature as Fahrenheit (isFahrenheit = true)
  float f = dht.readTemperature(true);


Alarm.delay(1000);
if (initBit == 1){ 
 setAlarms();
}

}
//Subroutines
void dailyReset(){
 int initBit = 1;
 Serial.println();
 Serial.print("Daily Reset:");
 Serial.print(initBit);
 
}
 
void CalculateAlarms() {
  
int  nowHour = hour(),
   nowMinute = minute(),
   nowDay = day(),
   nowMonth = month(),
   nowYear = year();
if (SERIAL_DEBUG){
  Serial.println();
   Serial.print("nowHour: ");
  Serial.println(nowHour);
  Serial.print("nowMinute: ");
  Serial.println(nowMinute);  
  Serial.print("nowDay: ");
  Serial.println(nowDay);
  Serial.print("nowMonth: ");
  Serial.println(nowMonth);
  Serial.print("nowYear: ");
  Serial.println(nowYear);
}
   
  byte timeLordSunRise[]  = {0, 0, 0, nowDay, nowMonth, nowYear};
  byte timeLordSunSet[]  = {0, 0, 0, nowDay, nowMonth, nowYear};

  coopTimeLord.SunRise(timeLordSunRise);
  coopTimeLord.SunSet(timeLordSunSet);
  
  if(SERIAL_DEBUG){
    Serial.print("sunrise: ");
    Serial.print(timeLordSunRise[2]);
    Serial.print(":");
    Serial.println(timeLordSunRise[1]);
    Serial.print("sunset: ");
    Serial.print(timeLordSunSet[2]);
    Serial.print(":");
    Serial.println(timeLordSunSet[1]);
  }
  
  openDoorHr = timeLordSunRise[2];
  openDoorMin = timeLordSunRise[1];
  closeDoorHr = timeLordSunSet[2];
  closeDoorMin = timeLordSunSet[1];
  
  if(closeDoorMin + DOOR_SHUT_AFTER_SUNSET_MIN >= 60){
    closeDoorHr += (closeDoorMin + DOOR_SHUT_AFTER_SUNSET_MIN) / 60;
    closeDoorMin = (closeDoorMin + DOOR_SHUT_AFTER_SUNSET_MIN) % 60;
  }else{
    closeDoorMin += DOOR_SHUT_AFTER_SUNSET_MIN;
  }
  
  if(SERIAL_DEBUG){
    Serial.print("open door: ");
    Serial.print(openDoorHr);
    Serial.print(":");
    Serial.println(openDoorMin);
    Serial.print("close door: ");
    Serial.print(closeDoorHr);
    Serial.print(":");
    Serial.println(closeDoorMin);
  }

  float naturalDaylightHr;
  naturalDaylightHr = 12.0f-(timeLordSunRise[2] + timeLordSunRise[1]/60.0f);
  naturalDaylightHr += (timeLordSunSet[2] + timeLordSunSet[1]/60.0f)-12.0f;
  
  if(SERIAL_DEBUG){
    Serial.print("natural daylight (hrs): ");
    Serial.println(naturalDaylightHr);
  }
  
  lightOnHr = timeLordSunSet[2];
  lightOnMin = timeLordSunSet[1];
  lightOffHr = timeLordSunSet[2];
  lightOffMin = timeLordSunSet[1];
  
  float lightNeededHrs = LIGHT_PER_DAY_HRS - naturalDaylightHr;
  int lightNeededMins = lightNeededHrs * 60;
  
  Serial.print("light needed (mins): ");
  Serial.println(lightNeededMins);

  lightOnHr -= LIGHT_ON_BEFORE_SUNSET_HRS;

  if(lightOffMin + lightNeededMins >= 60){
    lightOffHr += (lightOffMin + lightNeededMins) / 60;
    lightOffMin = (lightOffMin + lightNeededMins) % 60;
  }else{
    lightOffMin += lightNeededMins;
  }
  
  if(SERIAL_DEBUG){
    Serial.print("light on: ");
    Serial.print(lightOnHr);
    Serial.print(":");
    Serial.println(lightOnMin);
    Serial.print("light off: ");
    Serial.print(lightOffHr);
    Serial.print(":");
    Serial.println(lightOffMin);
  }
}
  
  void setAlarms(){
    CalculateAlarms();
    
  int nowHr = hourMinuteToHour(hour(), minute());
  
  if(nowHr < hourMinuteToHour(openDoorHr, openDoorMin)){
    Alarm.alarmOnce(openDoorHr, openDoorMin, 0, doorOpen);
  } 

  if(nowHr < hourMinuteToHour(closeDoorHr, closeDoorMin)){
    Alarm.alarmOnce(closeDoorHr, closeDoorMin, 0, doorClose);
  } 
  if(nowHr < hourMinuteToHour(lightOnHr, lightOnMin)){
    Alarm.alarmOnce(lightOnHr, lightOnMin, 0, lightOn); 
  }
  
  if(nowHr < hourMinuteToHour(lightOffHr, lightOffMin)){
    Alarm.alarmOnce(lightOffHr, lightOffMin, 0, lightOff);
  }
 int initBit = 0;
}
  
void initState(){
  pumpOff();
 
 CalculateAlarms();
  
  int nowHr = hourMinuteToHour(hour(), minute());

  if(nowHr > hourMinuteToHour(lightOnHr, lightOnMin) && nowHr < hourMinuteToHour(lightOffHr, lightOffMin)){
    Serial.println("light init on");
    lightOn();
  }else{
    Serial.println("light init off");
    lightOff();
  }
  if(nowHr > hourMinuteToHour(openDoorHr, openDoorMin) && nowHr < hourMinuteToHour(closeDoorHr, closeDoorMin)){
    Serial.println("door init open");
    doorOpen();
  }else{
    Serial.println("door init close");
    doorClose();
  }  
}
TimeLord coopTimeLord;

What is a TimeLord? You didn't deem that important?

You need to try the TimeAlarms class WITHOUT extra stuff like this class possibly interfering with time keeping.

Why, when you have an RTC attached, do you need the TimeAlarms function? Is it that difficult to determine that it is O'dark-thirty and, therefore, time to do something?

The dailyReset() function doesn't reset anything, so why is it called that?

TimeLord is the library that calculates sunrise and sunset times each day.

I use time alarms in the current, running version of the sketch along with TimeLord, However, instead of having timelord actually setting the sunrise / sunset times its just calculating, and timeAlarms are static times each day.

I guess its not that hard and may be my next method, to just call functions using RTC time compares to other variables

The dailyReset sets a bit to 1. If that bit == 1; the loop calls the setAlarms() function, which at the end of its run, sets the bit back to 0. Its supposed to be that at 01:00 the initBit is set to 1, causing the loop to run the setAlarms() function. This in turn calculates the alarms for today, using the timeLord to set the sunrise and sunset times. Then after calculating the alarms, the setAlarms() function then sets several Alarm.alarmOnce alarms for times the door should open and close and feeder should go off, then the day progresses, these alarms make stuff happen, then again at 1 am, the alarms are recalculated from the new day sunrise / sunset times.

The dailyReset sets a bit to 1. If that bit == 1; the loop calls the setAlarms() function

Really? The dailyReset() function creates a local variable called initBit, and sets its value. It prints the value, and then ends, so initBit goes out of scope (ceases to exist). The global variable of the same name is NOT affected by the actions of dailyReset(), so what dailyReset() does has NO bearing on the rest of the code.

OK, I am not sure I understand that. Or is this because the function is not "returning" anything? I am a little lost with the local / global variable thing. I work as an automation tech programming PLC's where a variable is a variable is a variable, or a tag is a tag, or a bit is a bit. All variables are predefined and all functions manipulate only those variables that are defined. How do I have the function manipulate the global variable? Even if that is the case, the function is still not being called, as the print functions fail to occur. How do I get the time alarms to call the function?

How do I have the function manipulate the global variable?

Do not redeclare it in the function.

void dailyReset(){
 //int initBit = 1;
 initBit = 1;
 Serial.println();
 Serial.print("Daily Reset:");
 Serial.print(initBit); 
}

Even if that is the case, the function is still not being called, as the print functions fail to occur. How do I get the time alarms to call the function?

Your problem is not with Alarm.alarmRepeat( 1, 0, 0, dailyReset); or with the access of time from the RTC. When I load an rtc with 0 hrs, 59 minutes, 0 seconds and run a stripped down version of your sketch I get the dailyReset to run. I think your sketch is hanging up somewhere or the Time Lord is messing with the time.

When I leave out all the non time/rtc related libraries and functions depending on them in your sketch the dailyReset is entered and I see the printout.

#include <Wire.h>
#include <RTClib.h>
#include <Time.h>
#include <TimeAlarms.h>
#define dtNBR_ALARMS 20
//#include <Adafruit_MCP23017.h>
//#include <Adafruit_RGBLCDShield.h>
//#include <NewPing.h>
//#include <DHT.h>
//#include <TimeLord.h>
//#define DHTTYPE DHT11

Too many libraries and probably way too much floating-point arithmetic.

Sometimes better to solve a problem yourself than to just throw more libraries at it.
Remember, it's only an Arduino. It can only handle so much at a time, and that's really not very much at all.

Thanks guys!! I didnt realize that I was re-declaring the variable. I will start commenting out sections and figure out where the timeAlarms is getting hung up!

I may be throwing too much at it.

I got it solved. I think the problem was my time alarms library was fubar. The same issue started happening even with my original program that was running rock solid for months, when I downloaded that program. Created a new sketch stripped down to just alarm.alarmRepeat functions and times set from variables. Even then the time alarms wouldnt call the functions. Removed and reinstalled the libraries for time, timealarms, and timelord. After that I got time alarms to call functions

#include <Wire.h>
#include <RTClib.h>
#include <Time.h>
#include <TimeAlarms.h>
#include <TimeLord.h>
RTC_DS1307 RTC;

// Door Open Time
int sHour = 07;
int sMinute = 0;
int sSecond = 0;

// Door Close Time
int eHour = 19;
int eMinute = 25;
int eSecond = 0;

//Pump Start Time
int lHour = 12;
int lMinute = 30;
int lSecond = 0;

//Pump End Time
int xHour = 17;
int xMinute = 0;
int xSecond = 0;

//Lights On time
int LightsOnHour = 18;
int LightsOnMin = 40;
int LightsOnSec = 0;

//Lights Off Time
int LightsOffHour = 23;
int LightsOffMin = 30;
int LightsOffSec = 0;


//Set Pins
const int doorRelay = 7; //DPDT K7 and K8 relays wireed together for double pole 
const int pumpRelay = 8; //K6 sprayer pump on 
const int lightRelay = 9; // K5 Light Relay

void setup() {
  // put your setup code here, to run once:
 // Set the relays to off immediately
  digitalWrite(doorRelay, HIGH);
  digitalWrite(pumpRelay, HIGH);
  digitalWrite(lightRelay, HIGH);

  //begin libraries
  Serial.begin(57600);
   RTC.begin();

   // Set the pinmode
  pinMode(pumpRelay, OUTPUT);
  pinMode(doorRelay, OUTPUT);
  pinMode(lightRelay, OUTPUT);

// Notify if the RTC isn't running
  if (! RTC.isrunning()) {
    Serial.println("RTC is NOT running");
  }

  // Get time from RTC
  DateTime current = RTC.now();
  DateTime compiled = DateTime(__DATE__, __TIME__);
  if (current.unixtime() < compiled.unixtime()) {
    Serial.println("RTC is older than compile time! Updating");
    RTC.adjust(DateTime(__DATE__, __TIME__));
  }


// Use RTC time to set the Door Open time
  setTime(sHour, sMinute, sSecond, current.day(), current.month(), current.year());
  time_t s = now();
  
  // Use RTC time to set the Door Close time
  setTime(eHour, eMinute, eSecond, current.day(), current.month(), current.year());
  time_t e = now();
  //Set lights on times 
  setTime(LightsOnHour, LightsOnMin, LightsOnSec, current.day(), current.month(), current.year());
  time_t onLight = now();
  //Set lights off time  
  setTime(LightsOffHour, LightsOffMin, LightsOffSec, current.day(), current.month(), current.year());
  time_t offLight = now();
   // Use RTC to set time now 
  setTime(current.hour(), current.minute(), current.second(), current.day(), current.month(), current.year());
  time_t n = now();
 
 
 //Test if various operations should occur to set the door and pump if reset occurs. 
  if (s <= n && n <= e) {
    digitalWrite(doorRelay, LOW);  // Openes the chicken door
  }
  else {
    digitalWrite(doorRelay, HIGH);
  }
 if (onLight <= n && n <= offLight) {
    digitalWrite(lightRelay, LOW);  // Openes the chicken door
  }
  else {
    digitalWrite(lightRelay, HIGH);
  } 

  Alarm.alarmRepeat(sHour, sMinute, sSecond, doorOpen); //sTime opens door
  Alarm.alarmRepeat(eHour, eMinute, eSecond, doorClose);//eTime closes door
  Alarm.alarmRepeat(LightsOnHour, LightsOnMin, LightsOnSec, lightOn);
  Alarm.alarmRepeat(LightsOffHour, LightsOffMin, LightsOffSec, lightOff);

After that was working I started calling the timelord calculate alarms function in the void setup, and have the CalculateAlarms() function set the shour, sminute, LightsOnHour, and LightsOnMin, in the setup BEFORE I run the set_time function.

 coopTimeLord.TimeZone(TIMEZONE * 60);
  coopTimeLord.Position(LATITUDE, LONGITUDE);
  
setTime(current.hour(), current.minute(), current.second(), current.day(), current.month(), current.year());
 
   //Calculate alarm times 
 CalculateAlarms(); 
  

// Use RTC time to set the Door Open time
  setTime(sHour, sMinute, sSecond, current.day(), current.month(), current.year());
  time_t s = now();
  
  // Use RTC time to set the Door Close time
  setTime(eHour, eMinute, eSecond, current.day(), current.month(), current.year());
  time_t e = now();
  //Set lights on times 
  setTime(LightsOnHour, LightsOnMin, LightsOnSec, current.day(), current.month(), current.year());
  time_t onLight = now();
  //Set lights off time  
  setTime(LightsOffHour, LightsOffMin, LightsOffSec, current.day(), current.month(), current.year());
  time_t offLight = now();
   // Use RTC to set time now 
  setTime(current.hour(), current.minute(), current.second(), current.day(), current.month(), current.year());
  time_t n = now();

The calculate alarms function is here, Now I just call void setup() with an alarm.alarmRepeat function at 1 am. This calls the calculate function and resets the set_time variables for repeating alarms. This way the chicken coop door and lights open and close times are reset each day.

void CalculateAlarms() {
  
int  nowHour = hour(),
   nowMinute = minute(),
   nowDay = day(),
   nowMonth = month(),
   nowYear = year();

  Serial.println();
   Serial.print("nowHour: ");
  Serial.println(nowHour);
  Serial.print("nowMinute: ");
  Serial.println(nowMinute);  
  Serial.print("nowDay: ");
  Serial.println(nowDay);
  Serial.print("nowMonth: ");
  Serial.println(nowMonth);
  Serial.print("nowYear: ");
  Serial.println(nowYear);

   
  byte timeLordSunRise[]  = {0, 0, 0, nowDay, nowMonth, nowYear};
  byte timeLordSunSet[]  = {0, 0, 0, nowDay, nowMonth, nowYear};

  coopTimeLord.SunRise(timeLordSunRise);
  coopTimeLord.SunSet(timeLordSunSet);
  
 
    Serial.print("sunrise: ");
    Serial.print(timeLordSunRise[2]);
    Serial.print(":");
    Serial.println(timeLordSunRise[1]);
    Serial.print("sunset: ");
    Serial.print(timeLordSunSet[2]);
    Serial.print(":");
    Serial.println(timeLordSunSet[1]);
  
  
  openDoorHr = timeLordSunRise[2];
  openDoorMin = timeLordSunRise[1];
  closeDoorHr = timeLordSunSet[2];
  closeDoorMin = timeLordSunSet[1];
  
  if(closeDoorMin + DOOR_SHUT_AFTER_SUNSET_MIN >= 60){
    closeDoorHr += (closeDoorMin + DOOR_SHUT_AFTER_SUNSET_MIN) / 60;
    closeDoorMin = (closeDoorMin + DOOR_SHUT_AFTER_SUNSET_MIN) % 60;
  }else{
    closeDoorMin += DOOR_SHUT_AFTER_SUNSET_MIN;
  }
  
  
    Serial.print("open door: ");
    Serial.print(openDoorHr);
    Serial.print(":");
    Serial.println(openDoorMin);
    Serial.print("close door: ");
    Serial.print(closeDoorHr);
    Serial.print(":");
    Serial.println(closeDoorMin);
  

  float naturalDaylightHr;
  naturalDaylightHr = 12.0f-(timeLordSunRise[2] + timeLordSunRise[1]/60.0f);
  naturalDaylightHr += (timeLordSunSet[2] + timeLordSunSet[1]/60.0f)-12.0f;
  
 
    Serial.print("natural daylight (hrs): ");
    Serial.println(naturalDaylightHr);
  
  
  lightOnHr = timeLordSunSet[2];
  lightOnMin = timeLordSunSet[1];
  lightOffHr = timeLordSunSet[2];
  lightOffMin = timeLordSunSet[1];
  
  float lightNeededHrs = LIGHT_PER_DAY_HRS - naturalDaylightHr;
  int lightNeededMins = lightNeededHrs * 60;
  
  Serial.print("light needed (mins): ");
  Serial.println(lightNeededMins);

  lightOnHr -= LIGHT_ON_BEFORE_SUNSET_HRS;

  if(lightOffMin + lightNeededMins >= 60){
    lightOffHr += (lightOffMin + lightNeededMins) / 60;
    lightOffMin = (lightOffMin + lightNeededMins) % 60;
  }else{
    lightOffMin += lightNeededMins;
  }
  
 
    Serial.print("light on: ");
    Serial.print(lightOnHr);
    Serial.print(":");
    Serial.println(lightOnMin);
    Serial.print("light off: ");
    Serial.print(lightOffHr);
    Serial.print(":");
    Serial.println(lightOffMin);

    LightsOnHour = lightOnHr;
    LightsOnMin = lightOnMin;
    LightsOnSec = 0;

    LightsOffHour = lightOffHr;
    LightsOffMin = lightOffMin;
    LightsOffSec = 0;
    //set times for testing
//    LightsOnHour = 17;
//    LightsOnMin = 53;
//    LightsOnSec = 0;
//
//    LightsOffHour = 17;
//    LightsOffMin = 54;
//    LightsOffSec = 0;
    
   sHour = openDoorHr;
   sMinute = openDoorMin;
   sSecond = 0;

   eHour = closeDoorHr;
   eMinute = closeDoorMin;
   eSecond = 0;