How to program servos to go off on a specific time and date

I am tryin to code my arduino to turn a servo 90 degrees at a specific time and date but it is just not working wondering if my programming is off

/* Timestamp functions using a DS1307 RTC connected via I2C and Wire lib
**
** Useful for file name
**		` SD.open(time.timestamp()+".log", FILE_WRITE) `
**
**
** Created: 2015-06-01 by AxelTB
** Last Edit:
*/

#include "RTClib.h"
#include <Wire.h>
#include <Servo.h>
Servo Paste_Servo1;  // "Second_Servo"
Servo Paste_Servo2; //  "Minute_Servo"
Servo VfeedDrip_Servo; //   "Hour Servo"
Servo TMYCNDrip_Servo; //   "TMYCNDrip_Servo"

RTC_DS3231 rtc;

void setup () {
  Serial.begin(115200);
  rtc.begin();
  Wire.begin();



  if (! rtc.lostPower()) {
    Serial.println("Couldn't find RTC");
    Serial.flush();
    abort();
  }

  if (rtc.lostPower()) {
    Serial.println("RTC is NOT running, let's set the time!");
    // When time needs to be set on a new device, or after a power loss, the
    // following line sets the RTC to the date & time this sketch was compiled
    rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
    // This line sets the RTC with an explicit date & time, for example to set
    // January 21, 2014 at 3am you would call:
    rtc.adjust(DateTime(2021, 4, 18, 22, 59, 0));
  }

  // When time needs to be re-set on a previously configured device, the
  // following line sets the RTC to the date & time this sketch was compiled
  rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
  // This line sets the RTC with an explicit date & time, for example to set
  // January 21, 2014 at 3am you would call:
   rtc.adjust(DateTime(2021, 4, 18, 22, 59, 0));
}

void loop() {
 DateTime time = rtc.now();
 DateTime now = rtc.now();

 //Full Timestamp
 Serial.println(String("DateTime::TIMESTAMP_FULL:\t")+time.timestamp(DateTime::TIMESTAMP_FULL));

 //Date Only
 Serial.println(String("DateTime::TIMESTAMP_DATE:\t")+time.timestamp(DateTime::TIMESTAMP_DATE));

 //Full Timestamp
 Serial.println(String("DateTime::TIMESTAMP_TIME:\t")+time.timestamp(DateTime::TIMESTAMP_TIME));

 Serial.println("\n");

 //Delay 5s
 delay(5000);

 if(now.year() == 2021 && now.month() == 4  && now.day() == 18 && now.hour() == 22 && now.minute() == 59 && now.second() == 0) {

  Paste_Servo1.attach(9);
  Paste_Servo1.write(90);
  delay(500);

     }
}

Try printing each of the data/time values that you test before actually testing them. Are they what you expect ?

By the way, the servo attach() should go in setup() and the delay(500) is achieving nothing

There is a TimeAlarms library which you might find useful.

You have a 5-second delay in your sketch. What are the chances that you will miss the one second you are looking for?

I got this code from the library, I missed that. Would changing it to 60 be what I think it means? like the full minute or should it be removed completely

Okay got it, I switched it up tp this

/* Timestamp functions using a DS1307 RTC connected via I2C and Wire lib
**
** Useful for file name
**		` SD.open(time.timestamp()+".log", FILE_WRITE) `
**
**
** Created: 2015-06-01 by AxelTB
** Last Edit:
*/

#include "RTClib.h"
#include <Wire.h>
#include <Servo.h>
Servo Paste_Servo1;  // "Second_Servo"
Servo Paste_Servo2; //  "Minute_Servo"
Servo VfeedDrip_Servo; //   "Hour Servo"
Servo TMYCNDrip_Servo; //   "TMYCNDrip_Servo"

RTC_DS3231 rtc;

void setup () {
  Serial.begin(115200);
  rtc.begin();
  Wire.begin();
  Paste_Servo1.attach(9);



  if (! rtc.lostPower()) {
    Serial.println("Couldn't find RTC");
    Serial.flush();
    abort();
  }

  if (rtc.lostPower()) {
    Serial.println("RTC is NOT running, let's set the time!");
    // When time needs to be set on a new device, or after a power loss, the
    // following line sets the RTC to the date & time this sketch was compiled
    rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
    // This line sets the RTC with an explicit date & time, for example to set
    // January 21, 2014 at 3am you would call:
    rtc.adjust(DateTime(2021, 4, 18, 22, 59, 0));
  }

  // When time needs to be re-set on a previously configured device, the
  // following line sets the RTC to the date & time this sketch was compiled
  rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
  // This line sets the RTC with an explicit date & time, for example to set
  // January 21, 2014 at 3am you would call:
   rtc.adjust(DateTime(2021, 4, 18, 22, 59, 0));
}

void loop() {
 DateTime time = rtc.now();
 DateTime now = rtc.now();

 //Full Timestamp
 Serial.println(String("DateTime::TIMESTAMP_FULL:\t")+time.timestamp(DateTime::TIMESTAMP_FULL));

 //Date Only
 Serial.println(String("DateTime::TIMESTAMP_DATE:\t")+time.timestamp(DateTime::TIMESTAMP_DATE));

 //Full Timestamp
 Serial.println(String("DateTime::TIMESTAMP_TIME:\t")+time.timestamp(DateTime::TIMESTAMP_TIME));

 Serial.println("\n");

 //Delay 5s
 delay(5000);

 if(now.year() == 2021 && now.month() == 4  && now.day() == 18 && now.hour() == 22 && now.minute() == 59 && now.second() == 0) {

  Paste_Servo1.write(90);
  

     }
}

Also, By print out you mean off the RTC tracker under serial monitor?

Im getting stuck at the end, I tried to make it so that i can use the explicit alarm feature to trigger the servo but im not sure how to implement it

/* Timestamp functions using a DS1307 RTC connected via I2C and Wire lib
**
** Useful for file name
**		` SD.open(time.timestamp()+".log", FILE_WRITE) `
**
**
** Created: 2015-06-01 by AxelTB
** Last Edit:
*/

#include "RTClib.h"
#include <Wire.h>
#include <Servo.h>
#include <TimeLib.h>
#include <TimeAlarms.h>

Servo Paste_Servo1;  // "Second_Servo"
Servo Paste_Servo2; //  "Minute_Servo"
Servo VfeedDrip_Servo; //   "Hour Servo"
Servo TMYCNDrip_Servo; //   "TMYCNDrip_Servo"

RTC_DS3231 rtc;

AlarmId id;

void setup () {
  Serial.begin(115200);
  while (!Serial) ;
  rtc.begin();
  Wire.begin();
  setTime(22,24,0,4,19,21);
  Paste_Servo1.attach(9);



  if (! rtc.lostPower()) {
    Serial.println("Couldn't find RTC");
    Serial.flush();
    abort();
  }

  if (rtc.lostPower()) {
    Serial.println("RTC is NOT running, let's set the time!");
    // When time needs to be set on a new device, or after a power loss, the
    // following line sets the RTC to the date & time this sketch was compiled
    rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
    // This line sets the RTC with an explicit date & time, for example to set
    // January 21, 2014 at 3am you would call:
    rtc.adjust(DateTime(2021, 4, 18, 22, 59, 0));
  }

  // When time needs to be re-set on a previously configured device, the
  // following line sets the RTC to the date & time this sketch was compiled
  rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
  // This line sets the RTC with an explicit date & time, for example to set
  // January 21, 2014 at 3am you would call:
   rtc.adjust(DateTime(2021, 4, 18, 22, 59, 0));
}

void loop() {
 DateTime time = rtc.now();
 DateTime now = rtc.now();

 //Full Timestamp
 Serial.println(String("DateTime::TIMESTAMP_FULL:\t")+time.timestamp(DateTime::TIMESTAMP_FULL));

 //Date Only
 Serial.println(String("DateTime::TIMESTAMP_DATE:\t")+time.timestamp(DateTime::TIMESTAMP_DATE));

 //Full Timestamp
 Serial.println(String("DateTime::TIMESTAMP_TIME:\t")+time.timestamp(DateTime::TIMESTAMP_TIME));

 Serial.println("\n");

 //Delay 5s
 delay(5000);
   } void ExplicitAlarm() {
  Paste_Servo1.write(90) ("Alarm: - this triggers only at the given date and time");
}

Don’t you have to set a date and time for the alarm to get the alarm to trigger? I don’t see anywhere where you do that. In the TimeAlarm example it uses something like:

  Alarm.alarmRepeat(dowSaturday,8,30,30,WeeklyAlarm);  // 8:30:30 every Saturday

You obviously didn’t replace “delay(500);” with “Alarm.delay(5000);”. Perhaps you should seek documentation for the TimeAlarm library.

I see it does by week Im wondering how to do it by date (month included)

The RTC chip maintains Month and Date values. You’d have to read the documentation for the TimeAlarms library to see if it can set an alarm for a particular date.

I am fairly sure that TimeAlarms does not support month as one of its trigger values but it would not be difficult to use the values from the RTC directly

Looks like you can use:
triggerOnce(time_t value, OnTick_t onTickHandler)
You just have to create a time_t value to represent the date and time to trigger. Unfortunately, there is no function to do that in one line. The only function for creating a time_t value is ‘makeTime()’ but the argument it takes is a tmElements_t struct and not individual values. Here is a function to fill in the struct and return the time_t:

time_t makeTime_t(int hr,int min,int sec,int dy, int mnth, int yr)
{
  tmElements_t tm;

  // year can be given as full four digit year or two digts (2010 or 10 for 2010);  
  // it is converted to years since 1970
  if( yr > 99)
      yr = yr - 1970;
  else
      yr += 30;  
  tm.Year = yr;
  tm.Month = mnth;
  tm.Day = dy;
  tm.Hour = hr;
  tm.Minute = min;
  tm.Second = sec;
  return makeTime(tm);
}

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.