DS1307 RTC with Time and TimeAlarms library

by sfjohnson722 on Mon Jan 21, 2013 6:52 pm

Most likely a very simple request for information - novice Arduino-er here...
Using the Adafruit DS1307, and successfully communicating with it, but am unclear how to write some alarm functions for it other than using if/then statements. I would like to incorporate the TimeAlarms libraries, but I am not clear how format/use the time once queried from the RTC (i.e. it seems as thought the Time/TimeAlarms libraries use the onboard Arduino time - how might I either force the Arduino time to match the RTC, or perhaps more accurate, to use the TimeAlarms to gather the time from the RTC). Found the RTC example in the Time library).
Thanks in advance for any help!
sfjohnson722
 
Posts: 6
Joined: Thu Nov 01, 2012 10:31 am
Location: State College, PA / Baltimore, MD

Re: DS1307 RTC with Time and TimeAlarms library

by adafruit_support_bill on Tue Jan 22, 2013 5:43 am

Post a link to the TimeAlarms library are you using. There are several out there.
User avatar
adafruit_support_bill
 
Posts: 25511
Joined: Sat Feb 07, 2009 9:11 am

Re: DS1307 RTC with Time and TimeAlarms library

by sfjohnson722 on Tue Jan 22, 2013 7:06 am

I'd originally found the following, but am open to other suggestions:
http://playground.arduino.cc/Code/Time
sfjohnson722
 
Posts: 6
Joined: Thu Nov 01, 2012 10:31 am
Location: State College, PA / Baltimore, MD

Re: DS1307 RTC with Time and TimeAlarms library

by adafruit_support_bill on Tue Jan 22, 2013 7:27 am

In the "TimeRTC" example there is a line in the setup:
Code: Select all
  setSyncProvider(RTC.get);   // the function to get the time from the RTC

That tells the Time library to sync the Arduino time to the RTC. The rest of the time & alarm functions continue to work as before.
User avatar
adafruit_support_bill
 
Posts: 25511
Joined: Sat Feb 07, 2009 9:11 am

Re: DS1307 RTC with Time and TimeAlarms library

by adafruit_support_rick on Tue Jan 22, 2013 7:44 am

sfjohnson722 wrote:or perhaps more accurate, to use the TimeAlarms to gather the time from the RTC

That's exactly what the Time/TimeAlarms library does. When you call setSyncProvider, the arduino time is synchronized with the RTC time.

By default, the times are re-synchronized whenever now() is called and it's been more than 5 minutes since the last sync.

So, you can follow the regular adafruit DS1307 example code. After calling RTC.begin(), call setSyncProvider(RTC.get), then follow the TimeAlarms example for setting alarms.
Code: Select all
#include "RTClib.h"

RTC_DS1307 RTC;

void setup () {
    Serial.begin(57600);
    Wire.begin();
    RTC.begin();
    setSyncProvider(RTC.get);

… etc ...
User avatar
adafruit_support_rick
 
Posts: 8569
Joined: Tue Mar 15, 2011 10:42 am
Location: Buffalo, NY

Re: DS1307 RTC with Time and TimeAlarms library

by sfjohnson722 on Sun Mar 17, 2013 5:38 pm

Still having some trouble with this; I was able to get the TimeRTC example to run when using DSC1307RTC.h ~or~ the adafruit example code when using RTClib.h.

BUT, trying to implementing the code as driverblock has above gives me the error:
sketch_mar17a.ino: In function 'void setup()':
sketch_mar17a:26: error: 'class RTC_DS1307' has no member named 'get'
sketch_mar17a:29: error: expected `;' before 'Serial'

So where I'm as it that I can't seem to get a setsync or the like using the adafruit library; only with the DSC1307RTC library (according to the wiki, ada's is a derivative but is modified; so it makes sense but I'm not clear as to the compatibility).

Driverblock - see you're in Buffalo; I grew up just outside East Aurora! Small world indeed!
sfjohnson722
 
Posts: 6
Joined: Thu Nov 01, 2012 10:31 am
Location: State College, PA / Baltimore, MD

Re: DS1307 RTC with Time and TimeAlarms library

by adafruit_support_rick on Sun Mar 17, 2013 8:10 pm

Post your code - we'll see if we can spot what's wrong.

Been back to East Aurora lately? They've really been doing a lot of work. They re-bricked Main Street, fixed up the Roycroft Campus, etc. The town is looking sharper than ever
User avatar
adafruit_support_rick
 
Posts: 8569
Joined: Tue Mar 15, 2011 10:42 am
Location: Buffalo, NY

Re: DS1307 RTC with Time and TimeAlarms library

by sfjohnson722 on Mon Mar 18, 2013 10:21 pm

Yup - a couple times a year (folks still live in Elma). Heading up this weekend actually!

Thanks in advance for any ideas on the following:

This does not compile for me; get an error "'class RTC_DS1307' has no member 'get'":
Code: Select all
#include <Wire.h>
#include "RTClib.h"
#include "Time.h"
#include "TimeAlarms.h"
// #include <DS1307RTC.h>

RTC_DS1307 RTC;

int RTCgnd = 16; // configure pins to power RTC & PWM
int RTCvcc = 17; // configure pins to power RTC & PWM

void setup () {
    // provide power to the RTC 
    pinMode(RTCgnd,OUTPUT); // provides power for RTC & PWM
    pinMode(RTCvcc,OUTPUT); // provides power for RTC & PWM
    digitalWrite(RTCgnd,LOW);   // provides power for RTC & PWM
    digitalWrite(RTCvcc,HIGH);  // provides power for RTC & PWM
 
    Serial.begin(9600);
    Wire.begin();
    RTC.begin();
    setSyncProvider(RTC.get)
}


If I include the DS1307 library; get an error "conflicting declaration 'RTC_DS1307 RTC' (removing that declaration will get an error "'class DS1307RTC' has no member 'begin'" at RTC.begin)
Code: Select all
#include <Wire.h>
#include "RTClib.h"
#include "Time.h"
#include "TimeAlarms.h"
#include <DS1307RTC.h>

RTC_DS1307 RTC;

int RTCgnd = 16; // configure pins to power RTC & PWM
int RTCvcc = 17; // configure pins to power RTC & PWM

void setup () {
    // provide power to the RTC 
    pinMode(RTCgnd,OUTPUT); // provides power for RTC & PWM
    pinMode(RTCvcc,OUTPUT); // provides power for RTC & PWM
    digitalWrite(RTCgnd,LOW);   // provides power for RTC & PWM
    digitalWrite(RTCvcc,HIGH);  // provides power for RTC & PWM
 
    Serial.begin(9600);
    Wire.begin();
    RTC.begin();
    setSyncProvider(RTC.get)
}


So I gave up on trying to use the RTClib.h and seem to have the following working ok:
Code: Select all
#include <Wire.h>
#include <Time.h>
#include <TimeAlarms.h>
#include <DS1307RTC.h>  // a basic DS1307 library that returns time as a time_t

int RTCgnd = 16; // configure pins to power RTC & PWM
int RTCvcc = 17; // configure pins to power RTC & PWM
int LED = 9; // which pin the LED is on
int LIGHTS = 0; // LED status (0 off, 1 on)

void setup()
{
  // provide power to the RTC 
  pinMode(RTCgnd,OUTPUT); // provides power for RTC & PWM
  pinMode(RTCvcc,OUTPUT); // provides power for RTC & PWM
  digitalWrite(RTCgnd,LOW);   // provides power for RTC & PWM
  digitalWrite(RTCvcc,HIGH);  // provides power for RTC & PWM
  // configure output light
  pinMode(LED,OUTPUT);
  digitalWrite(LED,LOW);
  // start services
  Serial.begin(9600);
  Wire.begin();
  // sync
  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");
  // set alarm times
  Alarm.alarmRepeat(20,03,00,LightsOn );  // turn light on
  Alarm.alarmRepeat(20,03,05,LightsOff);  // turn light off
}

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

void LightsOn()
{
  digitalWrite(LED,HIGH); // turn LED of
  Serial.println("...turning lights on...");
}
void LightsOff()
{
  digitalWrite(LED,LOW); // turn LED off
  Serial.println("...turning lights off...");
}

void digitalClockDisplay()
{
  // digital clock display of the time
  Serial.print(hour());
  printDigits(minute());
  printDigits(second());
  Serial.print(" ");
  Serial.print(day());
  Serial.print(" ");
  Serial.print(month());
  Serial.print(" ");
  Serial.print(year());
  Serial.println();
}
void printDigits(int digits)
{
  // utility function for digital clock display: prints preceding colon and leading 0
  Serial.print(":");
  if(digits < 10)
    Serial.print('0');
  Serial.print(digits);
}
sfjohnson722
 
Posts: 6
Joined: Thu Nov 01, 2012 10:31 am
Location: State College, PA / Baltimore, MD

Re: DS1307 RTC with Time and TimeAlarms library

by adafruit_support_rick on Tue Mar 19, 2013 11:16 am

By golly - you're right! Sorry for the bum steer. :oops:
But, as Spock says, simple problem, easy to correct. You just need to add your own sync provider function in place of the get(), and have it use RTCLib to do the same thing:

Give it a try and see if it works for you:

Code: Select all
// Date and time functions using a DS1307 RTC connected via I2C and Wire lib

#include <Wire.h>
#include "RTClib.h"
#include <Time.h>
#include <TimeAlarms.h>

RTC_DS1307 RTC;

time_t syncProvider()     //this does the same thing as RTC_DS1307::get()
{
  return RTC.now().unixtime();
}

void setup () {
    Serial.begin(57600);
    Wire.begin();
    RTC.begin();
    setSyncProvider(syncProvider);     //reference our syncProvider function instead of RTC_DS1307::get()

  if (! RTC.isrunning()) {
    Serial.println("RTC is NOT running!");
    // following line sets the RTC to the date & time this sketch was compiled
    RTC.adjust(DateTime(__DATE__, __TIME__));
  }
}

void loop () {
    DateTime now = RTC.now();
   
    Serial.print(now.year(), DEC);
    Serial.print('/');
    Serial.print(now.month(), DEC);
    Serial.print('/');
    Serial.print(now.day(), DEC);
    Serial.print(' ');
    Serial.print(now.hour(), DEC);
    Serial.print(':');
    Serial.print(now.minute(), DEC);
    Serial.print(':');
    Serial.print(now.second(), DEC);
    Serial.println();
   
    Serial.print(" since midnight 1/1/1970 = ");
    Serial.print(now.unixtime());
    Serial.print("s = ");
    Serial.print(now.unixtime() / 86400L);
    Serial.println("d");
   
    // calculate a date which is 7 days and 30 seconds into the future
    DateTime future (now.unixtime() + 7 * 86400L + 30);
   
    Serial.print(" now + 7d + 30s: ");
    Serial.print(future.year(), DEC);
    Serial.print('/');
    Serial.print(future.month(), DEC);
    Serial.print('/');
    Serial.print(future.day(), DEC);
    Serial.print(' ');
    Serial.print(future.hour(), DEC);
    Serial.print(':');
    Serial.print(future.minute(), DEC);
    Serial.print(':');
    Serial.print(future.second(), DEC);
    Serial.println();
   
    Serial.println();
    delay(3000);
}
User avatar
adafruit_support_rick
 
Posts: 8569
Joined: Tue Mar 15, 2011 10:42 am
Location: Buffalo, NY

Re: DS1307 RTC with Time and TimeAlarms library

by Ashton1 on Sun Apr 07, 2013 3:07 pm

What I'm trying to do though, without a PC connected, is sync the Arduino clock with the RTC, maybe when one of my scheduled events trigger.

At the moment, I'm testing the setTime(hr,min,sec,day,month,yr); method. I'm setting a variable to each value from the RTC, then trying to do a setTime().
See below code snippet.

Code: Select all
void updateTime(){
    DateTime now = RTC.now();
      h = now.hour();
      m = now.minute();
      s = now.second();
      d = now.month();
      mn = now.day();
      y = now.year();
      setTime(h,m,s,d,mn,y); // Old set time, pre RTC


It compiles fine but doesn't seem to be working. I'm really giving this a chance, because I'm pressed for room on my large application. Using the DS1307RTC.h library things get large. I tried using RTClib.h and DS1307RTC.h together but that causes compile errors.
Ashton1
 
Posts: 13
Joined: Sat Mar 23, 2013 8:59 am

Re: DS1307 RTC with Time and TimeAlarms library

by adafruit_support_rick on Sun Apr 07, 2013 4:19 pm

The syncProvider() function in my sample code does the actual synchronization. You don't need to call setTime at all. And you don't need DS1307RTC either.

All you need is RTCLIB, Time, and TimeAlarms. Copy the syncProvider() function into your sketch, and get rid of your updateTime function.

Call setSyncProvider(syncProvider) in setup(), right after RTC.begin(). It will immediately synchronize the clocks by calling the syncProvider() function. After that, the times will be automatically synchronized whenever now() is called and it's been more than 5 minutes since the last sync.

You never call syncProvider() yourself - the library will call it whenever it thinks it needs to synchronize the clocks
User avatar
adafruit_support_rick
 
Posts: 8569
Joined: Tue Mar 15, 2011 10:42 am
Location: Buffalo, NY

Re: DS1307 RTC with Time and TimeAlarms library

by Ashton1 on Sun Apr 07, 2013 5:02 pm

Ok, here is my code. I think I've followed your instructions exactly, but my alarm event does not fire, telling me the Arduino clock is not being set.
I really appreciate your time db.

I'm going to display the Arduino time on line #2 of my LCD to verify.

Code: Select all
// Daily alarm test using a DS1307 RTC
#include <Wire.h>
#include "RTClib.h"
#include <LiquidCrystal_I2C.h>
#include <Time.h>
#include <TimeAlarms.h>
LiquidCrystal_I2C lcd(0x27,20,4);
RTC_DS1307 RTC;
//int h, m, s, mn, d, y;

time_t syncProvider()     //this does the same thing as RTC_DS1307::get()
{
  return RTC.now().unixtime();
}
 
void setup () {
 // updateTime();  //setTime(10,48,0,7,4,2013);
 RTC.begin();
 setSyncProvider(syncProvider);
 Alarm.alarmRepeat(16,56,0, Alarm1);  // every day 11:50 Hours
 lcd.init();
 lcd.backlight();
 lcd.setCursor(0, 0);
 pinMode(13, OUTPUT);
 
//updateTime();  // Syncs Arduino to RTC using SetTime()
  //if (! RTC.isrunning()) {
   // Serial.println("RTC is NOT running!");
   // // following line sets the RTC to the date & time this sketch was compiled
   // RTC.adjust(DateTime(__DATE__, __TIME__));
  }
 
void loop () {
 clockDisplay();
 Alarm.delay(1000);
}

void clockDisplay()
{
  DateTime now = RTC.now();
  char buf[20];
  //sprintf(buf, "%02d:%02d:%02d %02d/%02d/%4d", now.hour(), now.minute(), now.second(), now.month(), now.day(), now.year());
  sprintf(buf, "%02d:%02d  %02d/%02d/%4d", now.hour(), now.minute(), now.month(), now.day(), now.year());
  lcd.print(buf);
 lcd.setCursor(0,0);


void Alarm1(){
 // DateTime now = RTC.now();
 //updateTime();
  digitalWrite(13, HIGH);  //alarm test output
  delay(4000);
  digitalWrite(13, LOW); }
/* 
  void updateTime(){
    DateTime now = RTC.now();
      h = now.hour();
      m = now.minute();
      s = now.second();
      d = now.month();
      mn = now.day();
      y = now.year();
      setTime(h,m,s,d,mn,y); // Old set time, pre RTC
  }
  */
Ashton1
 
Posts: 13
Joined: Sat Mar 23, 2013 8:59 am

Re: DS1307 RTC with Time and TimeAlarms library

by Ashton1 on Sun Apr 07, 2013 5:39 pm

I have the LCD displaying both Arduino time and RTC time now. The Arduino time is a random 23:XX hr ..../2014 time, line 2 is the current & RTC time expected.
Ashton1
 
Posts: 13
Joined: Sat Mar 23, 2013 8:59 am

Re: DS1307 RTC with Time and TimeAlarms library

by adafruit_support_rick on Sun Apr 07, 2013 5:47 pm

Hmmm... I'll have to set up and try to replicate this here. Give me a while.
User avatar
adafruit_support_rick
 
Posts: 8569
Joined: Tue Mar 15, 2011 10:42 am
Location: Buffalo, NY

Re: DS1307 RTC with Time and TimeAlarms library

by Ashton1 on Sun Apr 07, 2013 5:57 pm

Wow ! It did Sync!

But it took about ~ 5-6 minutes running time. Can I force this sync on startup?

When started, the Ardiono time was a random 23:XX hr ..../2014 time, my alarm test set for 2 minutes after upload failed. But then it suddenly synced.
Ashton1
 
Posts: 13
Joined: Sat Mar 23, 2013 8:59 am