RTC timer

Hi there,

I need to turn on an LED for half a second every ten minutes between 8 and 18 hrs (8am-6pm). My boss bought a Arduino Uno R3 and a DS1307 RTC from Adafruit Industries and told me to figure it out. I've never used Arduino before and am hoping someone can help me with the sketch so I can get this thing set up quickly.

I've poked around for the last couple hours but the code is a little over my head. I've got the Wire and RTClib in my Arduino IDE, and I have the RTC hooked up to the Arduino w/ an LED in pin 13. Just need the sketch to make it work...

Trying to turn an LED on for .5 seconds every 10 minutes between 8 and 18hrs (8am-6pm). Timing doesn't have to be ultra-precise, just within the minute or so.

Thank you very much, I appreciate any help I can get.

You can start with this. I just wrote this for what you need. You will probably need to change a few things and figure some things out for yourself, but it should work. You need to make sure that you can get a reading from your RTC before this code will work. You will also need to set the time on your RTC first. Once you get that figured out, then you can try to use this code. Right now the LED is set to turn on for 2 seconds, then it will turn off.

#include <Time.h>
#include <DS1307RTC.h>
#include <Wire.h>

#define DS1307_ADDRESS 0x68
const int ledPin = 13;
tmElements_t tm;
int Hour;
int Minute;
int Second;

void setup() {
   delay(5000);
  
  Serial.begin(9600);
  while (!Serial) ; // wait for serial
  delay(200);
  
  pinMode(ledPin,OUTPUT);
  Wire.begin();
  

}

void loop() {
updateTime();

if(Hour >= 8 && Hour <= 18){
  if(Minute == 0 && Second <= 4){
    digitalWrite(ledPin,HIGH);
    delay(2000);
    digitalWrite(ledPin,LOW);
  }
  else if(Minute == 10 && Second <= 4){
    digitalWrite(ledPin,HIGH);
    delay(2000);
    digitalWrite(ledPin,LOW);
  }
  else if(Minute == 20 && Second <= 4){
    digitalWrite(ledPin,HIGH);
    delay(2000);
    digitalWrite(ledPin,LOW);
  }
  else if(Minute == 30 && Second <= 4){
    digitalWrite(ledPin,HIGH);
    delay(2000);
    digitalWrite(ledPin,LOW);
  }
  else if(Minute == 40 && Second <= 4){
    digitalWrite(ledPin,HIGH);
    delay(2000);
    digitalWrite(ledPin,LOW);
  }
  else if(Minute == 50 && Second <= 4){
    digitalWrite(ledPin,HIGH);
    delay(2000);
    digitalWrite(ledPin,LOW);
  }
  else{
    delay(1000);
  }
 }
 else{
   delay(1000);
 }
}


void updateTime(){
  Hour = tm.Hour;
  Minute = tm.Minute;
  Second = tm.Second;
}

Hi!

I think that you need try do instead ask the code done.

A couple of hours isn't too time to learn something.

Anyway here's my suggestion to start.

const byte Start_Hour = 8; // Define hour to start.
const byte Start_Minute = 0; // Define minute to start.
const byte End_Hour = 18; // Define hour to stop.
const byte End_Minute = 0; // Define minute to stop.

void loop () 
{
  DateTime now = RTC.now();

  if((NumMins(now.hour(),now.minute()) >= NumMins(Start_Hour, Start_Minute)) && (NumMins(now.hour(),now.minute()) <= NumMins(End_Hour, End_Minute)))
  {
    // Add here a function using millis()
  } 
}

int NumMins(uint8_t ScheduleHour, uint8_t ScheduleMinute) // This function convert hour and minutes to MINUTES.
{
  return (ScheduleHour*60) + ScheduleMinute;
}
  if(Minute == 0 && Second <= 4){
    digitalWrite(ledPin,HIGH);
    delay(2000);
    digitalWrite(ledPin,LOW);
  }
  else if(Minute == 10 && Second <= 4){
    digitalWrite(ledPin,HIGH);
    delay(2000);
    digitalWrite(ledPin,LOW);
  }
  else if(Minute == 20 && Second <= 4){
    digitalWrite(ledPin,HIGH);
    delay(2000);
    digitalWrite(ledPin,LOW);
  }
  else if(Minute == 30 && Second <= 4){
    digitalWrite(ledPin,HIGH);
    delay(2000);
    digitalWrite(ledPin,LOW);
  }
  else if(Minute == 40 && Second <= 4){
    digitalWrite(ledPin,HIGH);
    delay(2000);
    digitalWrite(ledPin,LOW);
  }
  else if(Minute == 50 && Second <= 4){
    digitalWrite(ledPin,HIGH);
    delay(2000);
    digitalWrite(ledPin,LOW);
  }

This can all be reduced to:

 if(Minute%10 == 0 && Second <= 4){
    digitalWrite(ledPin,HIGH);
    delay(2000);
    digitalWrite(ledPin,LOW);
  }

Thanks for the above help, that helped a lot.

I'll tip someone $10 usd via bitcoin if you help me figure this out the rest of the way.

The sketch below is what I've got going right now. The RTC is set to the correct time (used the Adafruit RTC tutorial to look at it in the serial monitor). The Arduino starts, the LED in pin 13 flashes a few times as it starts, then the onboard LED turns on for 5 sec, then nothing else happens... Time goes by. Any ideas?

#include <Time.h>
#include <DS1307RTC.h>
#include <Wire.h>

#define DS1307_ADDRESS 0x68
const int ledPin = 13;
tmElements_t tm;
int Hour;
int Minute;
int Second;

void setup() {
   delay(5000);
  
  Serial.begin(9600);
  while (!Serial) ; // wait for serial
  delay(200);
  
  pinMode(ledPin,OUTPUT);
  Wire.begin();

}

void loop() {
updateTime();

if(Hour >= 8 && Hour <= 18){
  if(Minute == 0 && Second <= 4){
    digitalWrite(ledPin,HIGH);
    delay(2000);
    digitalWrite(ledPin,LOW);
  }
  else if(Minute == 10 && Second <= 4){
    digitalWrite(ledPin,HIGH);
    delay(2000);
    digitalWrite(ledPin,LOW);
  }
  else if(Minute == 20 && Second <= 4){
    digitalWrite(ledPin,HIGH);
    delay(2000);
    digitalWrite(ledPin,LOW);
  }
  else if(Minute == 30 && Second <= 4){
    digitalWrite(ledPin,HIGH);
    delay(2000);
    digitalWrite(ledPin,LOW);
  }
  else if(Minute == 40 && Second <= 4){
    digitalWrite(ledPin,HIGH);
    delay(2000);
    digitalWrite(ledPin,LOW);
  }
  else if(Minute == 50 && Second <= 4){
    digitalWrite(ledPin,HIGH);
    delay(2000);
    digitalWrite(ledPin,LOW);
  }
  else{
    delay(1000);
  }
 }
 else{
   delay(1000);
 }
}


void updateTime(){
  Hour = tm.Hour;
  Minute = tm.Minute;
  Second = tm.Second;
}

You never read the time from the RTC. Add it to the function updateTime.

void updateTime(){
  RTC.read(tm);
  Hour = tm.Hour;
  Minute = tm.Minute;
  Second = tm.Second;

Thank you for all the hand-holding folks... Looks like it doesn't know what "RTC" is though.. Here's the latest error I get:

Timer4.ino: In function 'void updateTime()':
Timer4:68: error: 'RTC' was not declared in this scope

#include <DS1307RTC.h>

Are you sure you have this library properly installed? Can you see the library listed under File- "examples" in the IDE? Can you run the example sketches ReadTest and SetTime shown there?

Thanks for the hint Cattledog, you solved this whole thing for me. I had a library called DS1307.h installed, not DS1307RTC.h

It all works great! Cattledog, give me a bitcoin address and I'll send $10usd your way for helping me out.

Glad to have been of help. Don't worry about the bitcoin. I hope you continue to learn about Arduino and programming and pass on what you learn to others.

You should take a look at johnwassers suggestion on how to simplify your code. Use it to understand the % (modulo) http://arduino.cc/en/Reference/Modulo

With the DS1307RTC.h library you do not need #define DS1307_ADDRESS 0x68 The address is taken car of by the library.

Its been awhile since I have used a modulo. I completely forgot about that. That would be a great way to simplify the code even further. Nice catch on the missing "get time" in the Update function. I forgot to add that in. This thread has been helpful for my own understanding.

A note to the OP:

Take this and use it as a way for yourself to better understand arduino code. Try simple projects and do them yourself. The best way to learn is to figure it out yourself.

That DS1307 library is probably the Henning Karlsen one, which I had problems with too. That's odd since his 1302 version is very good, as are other of his libraries. My problems also went away when I switched to DS1307RTC, ie it started to work :wink: .

There's also a time alarms library, which might simplify things further.