RTC Schedule on Nano

Okay, this may be covered somewhere on here that I just missed, but I am having a hell of a time figuring out how to use a DS1307 RTC to schedule events at specific times during the day.

Every time I think I have found something that works, either the library is incompatible with the Nano, or it errors out for some other reason. I've ended up deleting so many test sketches at this point I don't even know what to show.

And before anyone asks, I'm not using a running timer because I need the function to run at two specific times during the day, even if I have to unplug and move the project or if it should lose power for a time.

I have set the time on my RTC and tested it by using a sketch to print the time to the serial, so I know the clock is working and have verified that the battery on the RTC is working and it keeps time when powered off.

I'm fairly certain I've missed something big and am overlooking something that will make this simple, but I cannot find it, can someone point me in the right direction please?

I'm fairly certain I've missed something big and am overlooking something that will make this simple, but I cannot find it, can someone point me in the right direction please?

The DS1307 has the ability to set an alarm which you can read on a digital pin or even using an interrupt (although not necessary for the described task). Try with one of the libraries to implement such an alarm and post your code if you get problems. Don't expect us to write the code.

I suggest to learn how to use the Time library and to set your own alarms using a small data base and simple comparisons. It is much easier to do this than deal with the very rudimentary alarm scheme available in RTCs.

You don't need an RTC to do this, but one can very easily be added later to make the Arduino into an accurate timekeeper.

artanyis: I'm fairly certain I've missed something big and am overlooking something that will make this simple, but I cannot find it, can someone point me in the right direction please?

What is holding you back is your assumption that any solution will involve an additonal library.

You have stated that your RTC is working and keeping time, and that your evidence for this is your ability to display the RTC's time on the Serial monitor. This strongly implies the ability to read the time from the RTC. Just like if you ask a child the time and he correctly tells you, that strongly implies that the child is able to tell time.

If you can read the time from the RTC, then what is the problem? Suppose, for example, that your shift at work ends at five o'clock. If you want to know whether your shift is over, just look at the clock to see whether or not it is five o'clock yet.

That is a wonderfully simple way to put it. I think my problem is less conceptual and more in figuring out how to get the program to read the clock see that it is 07:00 and run the function or see that it is 20:00 and run the other function.

I'm re-reading through the Time and TimeAlarms, I should have some code on here soon for some help in picking it apart to tell me where I am wrong.

Okay, this is what I’m looking at. I feel there is something very wrong but I don’t know where/what.

#include <TimeLib.h>
#include <TimeAlarms.h>
#include <Wire.h>
#include <DS1307RTC.h>

int LEDs = 11;

AlarmId id;

void setup() {
Serial.begin(9600);
time_t t = now();
setTime(t);
pinMode(LEDs, OUTPUT);
Alarm.alarmRepeat(7,00,0, MorningAlarm);
Alarm.alarmRepeat(20,00,0,EveningAlarm);
}

void loop() {
Alarm.delay(1000);
}

void MorningAlarm() {
digitalWrite(LEDs HIGH);
}

void EveningAlarm() {
digitalWrite(LEDs LOW);
}

Alarm.alarmRepeat(7,00,0, MorningAlarm);

Write zero as 0 and not as 00. It doesn't really matter here, but leading zeroes can bite you later.

 time_t t = now();
  setTime(t);

Ok, let's think about this exercise. I want you to go find a clock in the room you're in. I want you to read that clock to get the current time and then set that clock to the current time. See it?

artanyis: Ok, let's think about this exercise. I want you to go find a clock in the room you're in. I want you to read that clock to get the current time and then set that clock to the current time. See it?

Wow, that makes sense. I will admit to being pretty sleep deprived but... I don't know what I was thinking with that.

Okay, so with that removed, I feel like there is something wrong here, like the alarm function has no way to reference the time? Does there need to be something to call the time form the clock for the alarm to reference or is it always reading?

On the Arduino, after program load, now() defaults to midnight of January 1, 1970.

If you want things to make sense, set the time to your current time and print out the time to see that it is correct.

Example:

#include <TimeLib.h>

char buf[30];
unsigned long current_millis = 0;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  // set current time and date to (hours, minutes, seconds, day, month, year)
  setTime(23, 59, 30, 29, 5, 2018);
}

void loop() {
  // put your main code here, to run repeatedly:
  if (millis() - current_millis > 3000UL) { //every three seconds...
    current_millis = millis();
    sprintf(buf, "%02d/%02d/%4d %02d:%02d:%02d", day(), month(), year(), hour(), minute(), second());
    Serial.println(buf);
    Serial.print("minutes since midnight ");
    Serial.println(60 * hour() + minute());
  }
}

The whole point of the RTC is that you set it once and forget it. So you write a quick little sketch to set the time (there are examples with the library) and thenyou load your code and just read the time from it. No need for your code to keep setting it.

artanyis: That is a wonderfully simple way to put it. I think my problem is less conceptual and more in figuring out how to get the program to read the clock see that it is 07:00 and run the function or see that it is 20:00 and run the other function.

I'm re-reading through the Time and TimeAlarms, I should have some code on here soon for some help in picking it apart to tell me where I am wrong.

How did you manage to print the RTC's time to the Serial monitor?

Did you use the Time library for that?

How did you do it? Post your code.

Delta_G: The whole point of the RTC is that you set it once and forget it. So you write a quick little sketch to set the time (there are examples with the library) and thenyou load your code and just read the time from it. No need for your code to keep setting it.

I used the Example sketch from DC1307RTC ReadTest to test that the clock was set and running.

Okay, so last night I added that bit of code into the rest of my code, pushed the entire thing to the Nano and wired my test-bed up, and absolutely nothing worked, even the really simple parts. I checked my wiring and found nothing wrong, and parts of this code I have tested independently before, so something should have been working, but it wasn't.

I'm going to strip everything down into it's parts and test each segment one at a time; I'll update this post once I have actually tested the RTC schedule segment.

Thank you everyone for the help and focusing gestures. :-)