I am building a project that triggers relays high or low based on RTC time or DHT22 sensor values. My sketch sets the clock based on the current time that it is compiled and uploaded. After that is where I run into trouble. The "if" statements used by the DHT22 work perfectly, but for some reason, I can't wrap my brain around an efficient way for the sketch to query the clock and execute a command. Below is the except of code dealing with the timed events. My notes are commented out. The only "time" library I'm using currently is RTClib.
RTC.adjust(DateTime(__DATE__, __TIME__)); //this captures the time from the computer that is uploading sketch to Arduino
// so ensure that the uploading computer has the correct time.
delay(1000);
}
void loop()
{
DateTime now = RTC.now();
//
// Because time is a direction in motion, >= will usually apply for testing as forward in time is upwards in count.
//
//int twelveHour = now.hour() - 12; // Variable used to display 13+ hours in 12 hour format
//int zeroHour = 12; // Variable use to convert "0" zero hour to display it as 12:00+
Serial.print('-');
Serial.print(' ');
//if (now.hour() == 0){ // First we test if the hour reads "0"
//Serial.print(zeroHour); // if yes, serial print a "12" instead
//}
//else if (now.hour() >= 13){ // if no, Second we test if the hour reads "13 or more"
//Serial.print(twelveHour); // if yes, serial print that current hour minus 12
//}
//else
{
Serial.print(now.hour(), DEC); // if no, Third we conclude that the am hours are being displayed correctly.
Serial.print(':');
Serial.print(now.minute(), DEC);
Serial.print(':');
Serial.print(now.second(), DEC);
Serial.print(' ');
Serial.print('-');
Serial.print(' ');
Serial.print(now.month(), DEC);
Serial.print('/');
Serial.print(now.day(), DEC);
Serial.print('/');
Serial.print(now.year(), DEC);
Serial.print(' ');
Serial.print('-');
Serial.println();
// Coded alarm triggers for Relays A-D. Adjust hours and minutes in accordance with 24 hour time format.
//
//****************** 18/6 Light Cycle - RELAY_A ****************************//
if (now.hour() >= 6 && now.minute() >= 0) { // Turn on time for Veg Lights, ON at 6am
digitalWrite(Relay_A, RELAY_ON);
Serial.print("\t");
Serial.print(F("Lights On")); // Text printed to serial monitor
Serial.print("\t");
}
else if (now.hour() >= 0 && now.minute() >= 0) // "else if" was needed to create opposition to lights on
{ // Turn off time for Veg Lights, OFF at 12am
digitalWrite(Relay_A, RELAY_OFF);
Serial.print("\t");
Serial.print(F("Lights OFf")); // Text printed to serial monitor
Serial.print("\t");
}
delay(500);
//******************** FEED TIMES - RELAY_B ****************************************//
if (now.hour() == 9 && now.minute() >= 0) { // Turn on time for FeedPump1, ON 9am
digitalWrite(Relay_B, RELAY_ON);
Serial.print("\t");
Serial.print(F("First 10 Minute Feeding")); // Text printed to serial monitor
Serial.print("\t");
}
else if (now.hour() == 9 && now.minute() >= 10) { // Turn off time for FeedPump1, OFF 9:10am
digitalWrite(Relay_B, RELAY_OFF);
}
if (now.hour() == 18 && now.minute() >= 0) { // Turn on time for FeedPump1, ON 6pm
digitalWrite(Relay_B, RELAY_ON);
Serial.print("\t");
Serial.print(F("Second 10 Minute Feeding")); // Text printed to serial monitor
Serial.print("\t");
}
else if (now.hour() == 18 && now.minute() >= 10) { // Turn off time for FeedPump1, OFF 6:10pm
digitalWrite(Relay_B, RELAY_OFF);
}
delay(500);
//********************** Heat Mat for Clones - RELAY_C *******************************//
if (now.hour() >= 2 && now.minute() == 0) { // Turn on time for HeatMat, ON at 2am
digitalWrite(Relay_C, RELAY_ON);
}
else if (now.hour() == 2 && now.minute() == 50) { // Turn off time for HeatMat, OFF at 2:50am, will soon be reassigned
digitalWrite(Relay_C, RELAY_OFF); // trigger based on DHT values
}
delay(500);
As far as I can tell, the events trigger on, but the weakness of my code is that nothing ever triggers off again because everything is always true and I don't know how to make the oppositions false. I thought the inclusion of "else if" would give me the needed opposition, but I was mistaken. Is it possible to do what I want to do in code, or does this NEED to be ironed out in a library?
TYIA