Hi everyone,
I have a setup i'm testing to be used to trigger a relay on and off
currently it is in test with an LED in place of the relay for simplicity
The below sketch works perfect as long as on time and off time are in the same day
however when i set on time to 18:00 and off time to 6:00 (next day) it does not work
I am looking for some input on where to change the code to facilitate an off time after midnight
#include "RTClib.h"
RTC_DS3231 rtc;
int tempC;
int tempF;
char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
const uint8_t ontime = 17; // set the stating hour
const uint8_t offtime = 20; // set the finish hour
const int ledPin = 15;
char dateBuffer[12];
void setup () {
Serial.begin(115200);
Wire.begin();
rtc.begin();
pinMode(15, OUTPUT); // currently an led for testing
}
void loop () {
tempC = rtc.getTemperature();
tempF = (tempC * 1.8) + 32.0; // Convert C to F
DateTime now = rtc.now();
if ((ontime <= now.hour()) && (now.hour() <= offtime))
{
// turn LED on:
digitalWrite(ledPin, HIGH);
Serial.println("currently in time range, led on");
}
else {
// turn LED off:
digitalWrite(ledPin, LOW);
}
Serial.print(now.month(), DEC);
Serial.print('/');
Serial.print(now.day(), DEC);
Serial.print('/');
Serial.print(now.year(), DEC);
Serial.print(" (");
Serial.print(daysOfTheWeek[now.dayOfTheWeek()]);
Serial.print(") ");
Serial.println();
sprintf(dateBuffer, "%02u:%02u:%02u ", now.hour(), now.minute(), now.second());
Serial.println(dateBuffer);
Serial.print("Temperature: ");
Serial.print(tempF);
Serial.println(" F");
Serial.println(" ");
delay(60000);
}
Ok, the logic of this project is to turn on a relay at 6pm and turn off the relay at 6am.
I am using an Arduino Uno Every with a DS 3231 RTC attached.
My coding works as long as it is same day. ie, on at 10am off at 10pm
as below
const uint8_t ontime = 10; // set the stating hour
const uint8_t offtime = 19; // set the finish hour
that works perfect. The problem comes when the off time hour is less than the on time hour
If it set on time to 6pm (18) and off time to 6am (5)
as below
const uint8_t ontime = 18; // set the stating hour
const uint8_t offtime = 5; // set the finish hour
It no longer operates. I have read all the documentation I can find but cannot figure out how to tell it that offtime is the next day
well that was what i thought when i set it up, however in testing it just doesn't work
I'm quite new at this programming Arduino code and this just seems like it should work... but it doesn't. It's not all that important so i'll play around with it, and the suggestions to try to achieve my goal. Thanks for your input
when both times are in the same day the LED lights and goes off as it should between the selected hours
but when end time is the next day at 6am the code below does not turn on my LED at all
It's like the code doesn't know that i'm between the selected times and defaults to the else of LED off
if ((ontime <= now.hour()) && (now.hour() <= offtime))
Yes, because you're saying, "if it is now after 18:00 and before 06:00". Does that seem right to you? Think of a number that is greater than 18 and less than 6.
Yes, because you're saying, "if it is now after 18:00 and before 06:00". Does that seem right to you?
In people time it is perfectly clear, I would know that's between 6pm and 6am without question.
However i see your point that in computer logic there is no match ever because there is no number between those parameters. If I set it ti it is now after 18:00 and before 23:00 it works because there is a match. So any suggestions on how to tell the program that the 6am is the next day? like i said i'm quite new and trying my best to learn this without being a bother to others.
My goal is just a bit of help in coding this so the program understands what i want.
Code in reply #14 has a feature you should adopt. It only sets and clears the LED once per day. That is why it didn't come on immediately. Your code sets it 100 billion times a day.
Hi @mr_jim
I don't understand why it didn't work for you.
Here it worked as expected.
To test here I changed from hours to seconds and changed the delay at the end of the code to 1 second (delay(1000)).
#include "RTClib.h"
RTC_DS3231 rtc;
int tempC;
int tempF;
char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
const uint8_t ontime = 28; // set the stating hour
const uint8_t offtime = 6; // set the finish hour
const int ledPin = 15;
char dateBuffer[12];
bool sequence = true;
//---------------------------------------------------------------
void setup () {
Serial.begin(115200);
Wire.begin();
rtc.begin();
pinMode(ledPin, OUTPUT); // currently an led for testing
}
//---------------------------------------------------------------
void loop () {
tempC = rtc.getTemperature();
tempF = (tempC * 1.8) + 32.0; // Convert C to F
DateTime now = rtc.now();
//if (ontime == now.hour() and digitalRead(ledPin) == LOW)
if (ontime == now.second() and digitalRead(ledPin) == LOW)
{
// turn LED on:
digitalWrite(ledPin, HIGH);
Serial.println("currently in time range, led on");
}
//if (offtime == now.hour() and digitalRead(ledPin) == HIGH)
if (offtime == now.second() and digitalRead(ledPin) == HIGH)
{
// turn LED off:
digitalWrite(ledPin, LOW);
}
Serial.print(now.month(), DEC);
Serial.print('/');
Serial.print(now.day(), DEC);
Serial.print('/');
Serial.print(now.year(), DEC);
Serial.print(" (");
Serial.print(daysOfTheWeek[now.dayOfTheWeek()]);
Serial.print(") ");
Serial.println();
sprintf(dateBuffer, "%02u:%02u:%02u ", now.hour(), now.minute(), now.second());
Serial.println(dateBuffer);
Serial.print("Temperature: ");
Serial.print(tempF);
Serial.println(" F");
Serial.println(" ");
//delay(60000);
delay(1000);
}