setTime() is a Time Library function, and the format is different from what you entered through the serial monitor. I have been developing the test code without the RTC, and I find it more convenient to just reset the time in code if necessary.You can start at any time of day and the code will follow.
setTime(int hr,int min,int sec,int day, int month, int yr);
My example was not very complete, and I have been working on implementing the complete timing. I was originally skeptical of the approach without the time alarms, and was concerned about some complicated control logic, but it turned out to be not too difficult to develop the code without any use of the time alarms.
I have changed some of the start times of your timing by a few seconds because I had some bugs with events starting at seconds==0. I think the minutes values could be in the process of changing. I centered the frozen food dispense in the middle of the stir cycle. I hope I interpreted our timing schedule correctly.
Please review this code, check and comment on the timings. We'll put the RTC back later after confirming the cycles. I have speeded up the testing by rescaling Timer0 which controls millis() and micros(). One hour is now about a minute of code test time. You will need to implement the digitalWrite() outputs.
Please review and comment.
#include <Time.h>
#include <TimeLib.h>
byte Hour;
byte Minute;
byte Second;
byte lastHour;
boolean runKalkCycle = false;
boolean runDryFoodCycle = false;
boolean runFrozenFoodCycle = false;
boolean runPumpCycle = false;
boolean runNightPumpCycle = false;
void setup()
{
//accelerate timing for testing purposes
//change Timer0 prescaler from default 64 to 1
//comment out next line to go back to default timing
TCCR0B = B00000001;
//setTime(int hr,int min,int sec,int day, int month, int yr);
setTime(8, 15, 00, 2, 1, 2019);//set time for Time library
Serial.begin(115200);
Serial.println("Starting Reef Tank Test");
Hour = hour();
Minute = minute();
Second = second();
printTime();
lastHour = hour();
}
void loop()
{
Hour = hour();
Minute = minute();
Second = second();
if (Hour != lastHour) //Kalk triggers every hour
{
lastHour = Hour;
printTime();
runKalkCycle = true;
}
if (Hour == 8 or Hour == 11 or Hour == 14 or Hour == 17)//dryFood
{
if (Minute == 30)
{
if (Second == 5) //some problem when triggered at 0 seconds
{
runDryFoodCycle = true;
}
}
}
if (Hour == 10 or Hour == 13 or Hour == 16 or Hour == 19)//frozenFood
{
if (Minute == 5)
{
if (Second == 5)
{
runFrozenFoodCycle = true;
}
}
}
//pump on times
if (Hour == 8 and Minute == 40 and Second == 0) runPumpCycle = true;
if (Hour == 10 and Minute == 10 and Second == 0) runPumpCycle = true;
if (Hour == 11 and Minute == 40 and Second == 0) runPumpCycle = true;
if (Hour == 13 and Minute == 10 and Second == 0) runPumpCycle = true;
if (Hour == 14 and Minute == 40 and Second == 0) runPumpCycle = true;
if (Hour == 16 and Minute == 10 and Second == 0) runPumpCycle = true;
if (Hour == 17 and Minute == 40 and Second == 0) runPumpCycle = true;
if (Hour == 19 and Minute == 10 and Second == 0) runNightPumpCycle = true;
if (runPumpCycle == true)
{
pumpCycle();
}
if (runNightPumpCycle == true)
{
nightPumpCycle();
}
if (runKalkCycle == true)
{
kalkCycle();
}
if (runDryFoodCycle == true)
{
dryFoodCycle();
}
if (runFrozenFoodCycle == true)
{
frozenFoodCycle();
}
}//end loop
void dryFoodCycle()
{
//timing variables
const unsigned long dryFoodFeedTime = 10000;
//logic control variables
static boolean timing = false;
static unsigned long timeStart;
if (!timing)
{
timing = true;
timeStart = millis();
//digitalWrite(dryFoodFeedPin, On)
Serial.print("Start Dry Food Feed ");
printTime();
}
if (millis() - timeStart >= dryFoodFeedTime)
{
//digitalWrite(dryFoodPin, off);
Serial.print("Stop Dry Food Feed ");
printTime();
timing = false;
runDryFoodCycle = false;
}
}
void frozenFoodCycle()
{
//timing values
const unsigned long frozenFoodStirTime = 20000;//
const unsigned long frozenFoodFeedTime = 5000;// 5 second dose
const unsigned long stirFeedDelay = 5000;//time between stir and dose start
//logic control variables
static boolean timing = false;
static boolean startFeed = false;
static boolean stopFeed = false;
static unsigned long timeStart;
if (!timing)
{
timing = true;
timeStart = millis();
//digitalWrite(frozenFoodStirPin, On)
Serial.print("Start Frozen Food Stir ");
printTime();
}
if (millis() - timeStart >= stirFeedDelay and !startFeed)
{
Serial.print("Start Frozen Food Feed ");
printTime();
//digitalWrite(frozenFoodFeedPin, On)
startFeed = true;
}
if (millis() - timeStart >= (stirFeedDelay + frozenFoodFeedTime) and !stopFeed)
{
Serial.print("Stop Frozen Food Feed ");
printTime();
//digitalWrite(frozenFoodFeedPin, Off)
stopFeed = true;
}
if (millis() - timeStart >= frozenFoodStirTime)
{
Serial.print("Stop Frozen Food Stir ");
printTime();
timing = false;
startFeed = false;
stopFeed = false;
runFrozenFoodCycle = false;
}
}
void kalkCycle()
{
//timing values
const unsigned long kalkStirTime = 60000;//1 minute stir
const unsigned long kalkFeedTime = 12000;//12 second dose
const unsigned long stirFeedDelay = 7000;//time between stir and dose start
//logic control variables
static boolean timing = false;
static boolean startFeed = false;
static boolean stopFeed = false;
static unsigned long timeStart;
if (!timing)
{
timing = true;
timeStart = millis();
//digitalWrite(kalkStirPin, On)
Serial.print("Start KalkStir ");
printTime();
}
if (millis() - timeStart >= stirFeedDelay and !startFeed)
{
Serial.print("Start KalkFeed ");
printTime();
//digitalWrite(kalkFeedPin, On)
startFeed = true;
}
if (millis() - timeStart >= (stirFeedDelay + kalkFeedTime) and !stopFeed)
{
Serial.print("Stop KalkFeed ");
printTime();
//digitalWrite(kalkFeedPin, Off)
stopFeed = true;
}
if (millis() - timeStart >= kalkStirTime)
{
Serial.print("Stop KalkStir ");
printTime();
timing = false;
startFeed = false;
stopFeed = false;
runKalkCycle = false;
}
}
void pumpCycle()
{
//timing variables
const unsigned long pumpOnTime = 80 * 60000UL; //1 Hr 20 min = 80 x 60000
//logic control variables
static boolean timing = false;
static unsigned long timeStart;
if (!timing)
{
timing = true;
timeStart = millis();
//digitalWrite(pumpAPin, On)
//digitalWrite(pumpBPin, On)
Serial.print("Start Return Pumps ");
printTime();
}
if (millis() - timeStart >= pumpOnTime)
{
//digitalWrite(pumpAPin, Off)
//digitalWrite(pumpBPin, Off)
Serial.print("Stop Return Pumps ");
printTime();
timing = false;
runPumpCycle = false;
}
}
void nightPumpCycle()
{
//timing variables
const unsigned long pumpOnTime = ((13 * 60) + 20) * 60000UL; //19:10 > 8:30 13 hr 20 min = ((13*60)+20)*60000UL
//logic control variables
static boolean timing = false;
static unsigned long timeStart;
if (!timing)
{
timing = true;
timeStart = millis();
//digitalWrite(pumpAPin, On)
//digitalWrite(pumpBPin, On)
Serial.print("Start Night Return Pumps ");
printTime();
}
if (millis() - timeStart >= pumpOnTime)
{
//digitalWrite(pumpAPin, Off)
//digitalWrite(pumpBPin, Off)
Serial.print("Stop Night Return Pumps ");
printTime();
timing = false;
runNightPumpCycle = false;
}
}
void printTime()
{
if (Hour < 10)
Serial.print('0');
Serial.print(Hour);
Serial.print(':');
if (Minute < 10)
Serial.print('0');
Serial.print(Minute);
Serial.print(':');
if (Second < 10)
Serial.print('0');
Serial.println(Second);
}