Using RTC (DS1307) to trigger 4 events throughout the day

Hi all,

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..

Any help would be greatly appreciated.

have you tried anything yet? any bit of code to start from, even if it is your basic LED sketch would help.

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.

great... don't feel pressure to over-comment your code, most folks understand what you are writing without excessive comments.

now, meaningful variable names, that's a different story!! that really helps! :grin:

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.

#define RED1 5
#define GREEN1 6
#define BLUE1 3
#define RED2 10
#define GREEN2 11
#define BLUE2 9
#define FADESPEED 5
#define TOWER 13


long blink_interval;
long next_blink;

bool pin_state;

void setup() {
 
  // put your setup code here, to run once:
  pinMode(RED1, OUTPUT);
  pinMode(GREEN1, OUTPUT);
  pinMode(BLUE1, OUTPUT);
  pinMode(RED2, OUTPUT);
  pinMode(GREEN2, OUTPUT);
  pinMode(BLUE2, OUTPUT);
  
  pinMode(TOWER, OUTPUT);
  
  pin_state = 0;
  digitalWrite(TOWER, pin_state);
  blink_interval = 1200;
  next_blink =  millis() + blink_interval;
}

void loop() {
 
 LED_morning();
 LED_day();
 LED_evening();
 LED_night();
 beacon_lx_cycle();

}



void beacon_lx_cycle() {
     if (millis() >= next_blink)
  {
     pin_state = !pin_state;
     digitalWrite(TOWER, pin_state);
    next_blink  = millis() + blink_interval;
  }

}

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.

You have a clock, right? And you want to do something in the morning, the afternoon, the evening, etc.?

Well, what would you do at home? Look at the clock? And depending on what it says, do something, or something else? It's the same basic idea in code.

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;
    }
  if (tm.Minute==15)
  {
  } 

  if (tm.Minute!=15)
  {

Are there other choices, besides equal to 15 or not equal to 15 that you might add later? If not, a simple else would be easier to understand.

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.