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();
}
}