DS1307 RTC with Time and TimeAlarms library

Adafruit Ethernet, Motor, Proto, Wave, Datalogger, GPS Shields - etc!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
User avatar
adafruit_support_rick
 
Posts: 35092
Joined: Tue Mar 15, 2011 11:42 am

Re: DS1307 RTC with Time and TimeAlarms library

Post by adafruit_support_rick »

Thanks for the summary!

User avatar
Tamadite
 
Posts: 1
Joined: Sun Feb 01, 2015 7:11 am

Re: DS1307 RTC with Time and TimeAlarms library

Post by Tamadite »

Just to complete the previous excellent summary, here you go a working case based on the example given with TimeAlarms.h

VERY IMPORTANT: Alarm.delay([ms]) is always needed by TimeAlarms.h to trigger alarms. You can set [ms] to 0 (cero) if no delay is needed.

Code: Select all

/*
 * TimeAlarmExample.pde
 *
 * This example calls alarm functions at 8:30 am and at 5:45 pm (17:45)
 * and simulates turning lights on at night and off in the morning
 * A weekly timer is set for Saturdays at 8:30:30
 *
 * A timer is called every 15 seconds
 * Another timer is called once only after 10 seconds
 *
 * At startup the time is set to Jan 1 2011  8:29 am
 */

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

RTC_DS1307 RTC;

void setup()
{
  Serial.begin(9600);

  Wire.begin();
  RTC.begin();
  setSyncProvider(syncProvider);
  RTC.now();
  
  Alarm.alarmRepeat(8,30,0, MorningAlarm);  // 8:30am every day
  Alarm.alarmRepeat(17,45,0,EveningAlarm);  // 5:45pm every day 
  Alarm.alarmRepeat(dowSaturday,8,30,30,WeeklyAlarm);  // 8:30:30 every Saturday 

 
  Alarm.timerRepeat(15, Repeats);            // timer for every 15 seconds    
  Alarm.timerOnce(10, OnceOnly);             // called once after 10 seconds 
}

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

void  loop(){  
  digitalClockDisplay();
  Alarm.delay(1000); // wait one second between clock display
}

// functions to be called when an alarm triggers:
void MorningAlarm(){
  Serial.println("Alarm: - turn lights off");    
}

void EveningAlarm(){
  Serial.println("Alarm: - turn lights on");           
}

void WeeklyAlarm(){
  Serial.println("Alarm: - its Monday Morning");      
}

void ExplicitAlarm(){
  Serial.println("Alarm: - this triggers only at the given date and time");       
}

void Repeats(){
  Serial.println("15 second timer");         
}

void OnceOnly(){
  Serial.println("This timer only triggers once");  
}

void digitalClockDisplay()
{
  // digital clock display of the time
  Serial.print("Unix time: ");
  Serial.println(RTC.now().unixtime());
  Serial.print(hour());
  printDigits(minute());
  printDigits(second());
  Serial.println(); 
}

void printDigits(int digits)
{
  Serial.print(":");
  if(digits < 10)
    Serial.print('0');
  Serial.print(digits);
}

User avatar
adafruit_support_rick
 
Posts: 35092
Joined: Tue Mar 15, 2011 11:42 am

Re: DS1307 RTC with Time and TimeAlarms library

Post by adafruit_support_rick »

That's great! Thanks for posting!

User avatar
sachlj
 
Posts: 7
Joined: Mon May 04, 2015 3:07 am

Re: DS1307 RTC with Time and TimeAlarms library

Post by sachlj »

This code can be used for repeat after minutes? I need to be repeated every 4 minutes cycle: start the pump with water for 1 min and 40 seconds. Then turn off the pump. I apologize for my English.

User avatar
adafruit_support_rick
 
Posts: 35092
Joined: Tue Mar 15, 2011 11:42 am

Re: DS1307 RTC with Time and TimeAlarms library

Post by adafruit_support_rick »

Yes. It will work with a four minute delay.

User avatar
sachlj
 
Posts: 7
Joined: Mon May 04, 2015 3:07 am

Re: DS1307 RTC with Time and TimeAlarms library

Post by sachlj »

Thank you for answer. What is the correct syntax for the command? Start every 4 minutes, run code 1 minute and 40 seconds in order not to stop the next code.

User avatar
adafruit_support_rick
 
Posts: 35092
Joined: Tue Mar 15, 2011 11:42 am

Re: DS1307 RTC with Time and TimeAlarms library

Post by adafruit_support_rick »

Set a repeating timer for 4 minutes in setup:

Code: Select all

void setup()
{
. . .
  Alarm.timerRepeat(240, four_minute_task);            // set up task to run every 4 minutes
. . .
}

void loop()
{
. . .
  Alarm.delay(1);   //must call Alarm.delay at least once per loop to guarantee that four_minute_task will run.
. . .
}

void four_minute_task()
{
  Serial.println(F("runs every four minutes"));
} 

User avatar
sachlj
 
Posts: 7
Joined: Mon May 04, 2015 3:07 am

Re: DS1307 RTC with Time and TimeAlarms library

Post by sachlj »

Thank you.I try.
I wrote this code but does not work as I need. Somewhere I have a mistake. Relay starts but remains switched on and off after a set time.

Code: Select all

/*code for Arduino MEGA 2560-*/

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

#define Relay_1  38 
// #define Relay_2  40
#define RELAY_ON 1
#define RELAY_OFF 0

RTC_DS1307 RTC;

void setup()
{
  Serial.begin(9600);
  digitalWrite(Relay_1, RELAY_OFF);
//  digitalWrite(Relay_2, RELAY_OFF);
  pinMode(Relay_1, OUTPUT);   
//  pinMode(Relay_2, OUTPUT);
  Wire.begin();
  
  RTC.begin();
  setSyncProvider(syncProvider);
  RTC.now();
  
  Alarm.timerRepeat(240, pump);            // set up task to run every 4 minutes
  }
  time_t syncProvider()     //this does the same thing as RTC_DS1307::get()
{
  return RTC.now().unixtime();
}

void  loop(){  
   Alarm.delay(1); // wait one second between clock display
}

void pump(){
  digitalWrite(Relay_1, RELAY_ON);// set the Relay ON
  Serial.println("Relay - ON");
  Serial.println(RTC.now().unixtime());
  delay(10000);
  digitalWrite(Relay_1, RELAY_OFF);// set the Relay OFF
   Serial.println("Relay - OFF");
  Serial.println(RTC.now().unixtime());
} 

User avatar
adafruit_support_rick
 
Posts: 35092
Joined: Tue Mar 15, 2011 11:42 am

Re: DS1307 RTC with Time and TimeAlarms library

Post by adafruit_support_rick »

Please post the output from serial monitor

User avatar
sachlj
 
Posts: 7
Joined: Mon May 04, 2015 3:07 am

Re: DS1307 RTC with Time and TimeAlarms library

Post by sachlj »

Here is the output. 2x relay is switched on and off and then turned on. I suppose for 4 minutes. Output to serial port is no even if it is set.
Here is a picture
Here is a picture
arduino.png (214.67 KiB) Viewed 1418 times

User avatar
adafruit_support_rick
 
Posts: 35092
Joined: Tue Mar 15, 2011 11:42 am

Re: DS1307 RTC with Time and TimeAlarms library

Post by adafruit_support_rick »

Your code is working for me. I reduced the alarm time to 20 seconds. I get this on Serial Monitor:

Code: Select all

Relay - ON
1430844228
Relay - OFF
1430844238
Relay - ON
1430844248
Relay - OFF
1430844258
Relay - ON
1430844268
Relay - OFF
1430844278
Relay - ON
1430844288
Relay - OFF
1430844298
It should still work, even if your RTC is not running. However, try running the attached sketch, which will initialize your RTC:

Code: Select all

/*code for Arduino MEGA 2560-*/

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

#define Relay_1  38 
// #define Relay_2  40
#define RELAY_ON 1
#define RELAY_OFF 0

RTC_DS1307 RTC;

void setup()
{
  Serial.begin(9600);
  digitalWrite(Relay_1, RELAY_OFF);
  //  digitalWrite(Relay_2, RELAY_OFF);
  pinMode(Relay_1, OUTPUT);   
  //  pinMode(Relay_2, OUTPUT);
  Wire.begin();

  RTC.begin();
  setSyncProvider(syncProvider);
  
  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__));
  }

  RTC.now();

  Alarm.timerRepeat(20, pump);            // set up task to run every 4 minutes
}

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

void  loop(){  
  Alarm.delay(1); // wait one second between clock display
}

void pump(){
  digitalWrite(Relay_1, RELAY_ON);// set the Relay ON
  Serial.println("Relay - ON");
  Serial.println(RTC.now().unixtime());
  delay(10000);
  digitalWrite(Relay_1, RELAY_OFF);// set the Relay OFF
  Serial.println("Relay - OFF");
  Serial.println(RTC.now().unixtime());
}

User avatar
sachlj
 
Posts: 7
Joined: Mon May 04, 2015 3:07 am

Re: DS1307 RTC with Time and TimeAlarms library

Post by sachlj »

Thank you. I'll try it. Also, I thought of the way to work. Can you think of how to fix the pump to be turned on for 1 minute and 40 seconds (but accurately), but they did not stop running other code? After 1 minute (simultaneously) required to run other code (read sensor values and record to mySQL). Comply with the time 1:40 is important to overflow tank that is so infused.

User avatar
adafruit_support_rick
 
Posts: 35092
Joined: Tue Mar 15, 2011 11:42 am

Re: DS1307 RTC with Time and TimeAlarms library

Post by adafruit_support_rick »

Just set another timer when you turn on the pump. When the timer expires, turn the pump off:

Code: Select all

/*code for Arduino MEGA 2560-*/

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

#define Relay_1  38 
// #define Relay_2  40
#define RELAY_ON 1
#define RELAY_OFF 0

RTC_DS1307 RTC;

void setup()
{
  Serial.begin(9600);
  digitalWrite(Relay_1, RELAY_OFF);
  //  digitalWrite(Relay_2, RELAY_OFF);
  pinMode(Relay_1, OUTPUT);   
  //  pinMode(Relay_2, OUTPUT);
  Wire.begin();

  RTC.begin();
  setSyncProvider(syncProvider);
  
  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__));
  }

  RTC.now();

  Alarm.timerRepeat(240, pumpOn);            // set up task to run every 4 minutes
}

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

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

void pumpOn(){
  Alarm.timerOnce(100, pumpOff);
  digitalWrite(Relay_1, RELAY_ON);// set the Relay ON
  Serial.println("Relay - ON");
  Serial.println(RTC.now().unixtime());
}

void pumpOff(){
  digitalWrite(Relay_1, RELAY_OFF);// set the Relay OFF
  Serial.println("Relay - OFF");
  Serial.println(RTC.now().unixtime());
}

User avatar
sachlj
 
Posts: 7
Joined: Mon May 04, 2015 3:07 am

Re: DS1307 RTC with Time and TimeAlarms library

Post by sachlj »

Thank you. It's not 100% but I can not finish himself.

User avatar
john_folsom
 
Posts: 8
Joined: Fri May 15, 2015 10:11 am

Re: DS1307 RTC with Time and TimeAlarms library

Post by john_folsom »

I'm working with these libraries to trigger an automatic pet feeder and am having a problem getting it to compile correctly. After putting in the

Code: Select all

time_t syncProvider() {
  return rtc.now().unixtime();
}
function, I get the following error -
Automated_Pet_Feeder_2.ino: In function 'time_t syncProvider()':
Automated_Pet_Feeder_2.ino:51:10: error: 'rtc' was not declared in this scope
Error compiling.

Here is the full code -

Code: Select all

//Libraries must include in this order
#include <Wire.h>
#include <RTClib.h>
#include <TimeAlarms.h>
#include <Time.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_PWMServoDriver.h"

RTC_DS1307 RTC;

//Create the Adafruit_MotorShield object
Adafruit_MotorShield AFMS = Adafruit_MotorShield();

//Create the stepper motor object
//Request the Stepper motor from the Adafruit_MotorShield:
Adafruit_StepperMotor *myMotor = AFMS.getStepper(200, 2);


void setup() {

  Serial.begin(57600);
   Wire.begin();
  RTC.begin();
  setSyncProvider(syncProvider);
  RTC.now;
  
  if(timeStatus()!= BANNED) 
     Serial.println("Unable to sync with the RTC");
  else
     Serial.println("RTC has set the system time");     
     
    
  
  //sets a daily alarm at 6am
  Alarm.alarmRepeat(06, 00, 00, FeedMe);

  //sets a daily alarm at 8pm
  Alarm.alarmRepeat(20, 32, 0, FeedMe);

//  Alarm.timerRepeat(15, FeedMe);

  AFMS.begin();

  myMotor->setSpeed(20);
}



time_t syncProvider() {
  return rtc.now().unixtime();
}

void loop() {

  DateTime now = RTC.now();
  
  Alarm.delay(1000);
//  
//  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();
//    
// 

}

//function to run when the alarm goes off
void FeedMe() {

  myMotor->step(100, FORWARD, DOUBLE);

  delay(3000);

  myMotor->step(100, BACKWARD , DOUBLE);

}



Anyone have an idea of what might be happening?

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino Shields from Adafruit”