I am trying to make a Timer for a project. Using RTC i have set the time and set the update interval to maintain a true time.
Now i wish to make Variables or switch cases based on variance between time. i need 24h but to be able to overlap between the days
ie have time periods from 1600 to 0345 so if current time is between this range do this else do nothing
i have no idea on where to start and believe i will have an issue with midnight getting in the way of a simple project.
does anyone know if there is a simple way to do this? Basically after a power outage i need what ever state its in to resume so using time alarms in not ideal.
can it be simple or is it going to be a nightmare?
JayWarne:
I am trying to make a Timer for a project. Using RTC i have set the time and set the update interval to maintain a true time.
Now i wish to make Variables or switch cases based on variance between time. i need 24h but to be able to overlap between the days
ie have time periods from 1600 to 0345 so if current time is between this range do this else do nothing
i have no idea on where to start and believe i will have an issue with midnight getting in the way of a simple project.
does anyone know if there is a simple way to do this? Basically after a power outage i need what ever state its in to resume so using time alarms in not ideal.
can it be simple or is it going to be a nightmare?
Simple or nightmare depends on how you approach it. Do you mind answering some questions?
First, what RTC are you using, does it have a battery, and how does it communicate with the microcontroller? Most RTC units use I2C, but I have a DS3234 that uses SPI as well.
Second, what is your circuit doing? Does it turn something on at a certain time, then off at another; or does it turn it on at random times, and run for a specific length of time?
Finally, do you need the precision of an RTC, or can you use 'Arduino timing'?
void setup() {
Serial.begin(9600);
while (!Serial) ; // Needed for Leonardo only
setSyncProvider(RTC.get); // the function to get the time from the RTC
if (timeStatus() != timeSet)
Serial.println("Unable to sync with the RTC");
else
Serial.println("RTC has set the system time");
setSyncInterval(600);
Setting time from the RTC this is an example sketch that im building from.
The Idea of the project is to Turn Lights on between certain frames and Pumps ETC for an Aquaponic system.
Idea of the RTC is from powerouts/resets i need to maintain the time frames rather than actual running time IUKWIM
18/6 Light periods, But want to make sunrise sunsets and work on longer/shorter days over time which i guess i can work out once iv got the basic approach to working with time.
I am not an overly experienced coder but have made a fair few simple projects with arduino.
I think if this was my project I would do all my time calculations using minutes after midnight. For example 1pm would be 780 minutes. That makes tests with IF much simpler.
A possible issue with time-since-midnight arises when the start and end times are on different days.
Just be sure that your approach doesn't try to measure an interval by subtracting one time-since-midnight from another, when the values are on different days.
PaulS:
A possible issue with time-since-midnight arises when the start and end times are on different days.
Just be sure that your approach doesn't try to measure an interval by subtracting one time-since-midnight from another, when the values are on different days.
Unless, in that case, you add one day's worth of minutes to compensate.
odometer:
Unless, in that case, you add one day's worth of minutes to compensate.
I think what @PaulS was pointing out is the risk that you would not realize that the two times were either side of midnight. Once you have that figured out there are many ways to get the correct interval.
If you use the RTCLib library from Adafruit, it can read your RTC and it has a nice little class 'TimeSpan' which will do all the heavy lifting for figuring out the elapsed time between two dates. I have noticed other RTC libraries have this feature, but the Time library you referenced above does not.