Again, I am a n00b, so I don't fully understand this yet.
I am trying to use an RTC to trigger events throughout the day. I have already programmed my different colors on my LED strips. Now my question is how do I tell it to change color at the same times every day? I am having some difficulty understanding the time library as well..
I can post the code I have been tinkering with. The pain is that my RTC comes tomorrow. So I have been shooting in the dark.But yeah, let me finish commenting on my code. And I'll post it.
Part of the problem with my code is I have four functions that I would like to have run at different times.
Function 1 6:00 AM- LED_morning
Function2 9:30 AM- LED_day
Function3 7:30 PM-LED_sunset
Function4 9:30 PM- LED_night
I have no idea really where to begin when writing that. I currently have all four functions in different sketches.
The DS1307 does not have built in alarms, so you will have to make a choice between two different approaches.
I am having some difficulty understanding the time library as well..
You may want to make the effort to figure that out, because you can synchronize the Arudino's internal clock with the RTC and use the Time and Time Alarms libraries to do what you want. There are examples with both libraries to help.
The alternative is to to read the time directly from the RTC at intervals (every minute, every 5 minutes, every time through the loop, etc.) using a timer following the "blink without delay" example. You can then compare the times you read from the RTC with your desired "change what I am doing" times.
I advocate the use of the alarm library and it is indeed worth understanding, as cattledog says.
But before I embraced that library, I did it old school.... I wanted to write the temperature to eeprom at 15 minutes past every hour. So all you do is look to see if tm.minute==15 in loop(). BUT there's a trick.... since loop runs at something like 150k times a second, tm.minute will be 15 for many many passes, and you only want one eeprom write. So have a flag, say eepromWritten, initially false, and set it true the first time you write the eeprom on a new quarter past the hour. Then no matter how many times you loop(), only one eeprom write will happen for that hour. Further trick is to remember to set it false again, the first time you get a tm.minute of 16 (or strictly, not 15), else it won't write in an hour's time.
Snippet below using flag called tempLogged.... but it's pretty clear the library makes things lots simpler for the coder.
if (tm.Minute==15)
{
if (tempLogged == false) //declare this first of course :)
{
// do stuff
tempLogged = true;
}
}
if (tm.Minute!=15)
{
tempLogged = false;
}
You're right, it should be done in one go unless there's a compelling reason not to.
I always get confused about the last one being an option off the first or is it off the second if, itself already inside the first one. So I did it the long way which I knew would work since I- at the time- didn't feel like investigating the workings of the nest.