Moderators: adafruit_support_bill, adafruit
setSyncProvider(RTC.get); // the function to get the time from the RTC
sfjohnson722 wrote:or perhaps more accurate, to use the TimeAlarms to gather the time from the RTC
#include "RTClib.h"
RTC_DS1307 RTC;
void setup () {
Serial.begin(57600);
Wire.begin();
RTC.begin();
setSyncProvider(RTC.get);
… etc ...
#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)
}#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)
}#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);
}
// 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);
}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// 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
}
*/Return to Arduino Shields from Adafruit
Users browsing this forum: No registered users and 4 guests