DS1307 RTClib action when time come - help

hey there
im still learning and want to ask -
how i can set action that will be triggered, when exact time occur? before i used iarduino_RTC library, but got some strange device behavior, so decided to change to more modern lib.

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

RTC_DS1307 rtc;

const int buttonPin = 3;
int buttonState = 0;
int PowerPin = 4; 

int angle = 112; 
Servo myservo;  // create servo object to control a servo
const int servoPWcontrol = 5;     // the number of serwo porwe control 

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

  pinMode(PowerPin, OUTPUT);
  digitalWrite(PowerPin, HIGH);  
  pinMode(buttonPin, INPUT);
   
  if (! rtc.begin()) 
  {
    Serial.println("Couldn't find RTC");
    while (1);
  }

  if (! rtc.isrunning()) 
  {
    Serial.println("RTC is NOT running!");
  }
  
    rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));//auto update from computer time
    //rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));// to set the time manualy 
  
    myservo.attach(9);
    myservo.write(angle);
    pinMode(servoPWcontrol, OUTPUT);
    digitalWrite(servoPWcontrol, LOW);   // Turn powersource od servo OFF
    Serial.println("Type Command by first letter (gettime - G)");
}

void loop () 
{
  delay(900);
  
   DateTime now = rtc.now();
   
   buttonState = digitalRead(buttonPin);
if

here im trying to set if my time 10:00:00, then next action will be triggered, but im not sure what to do in that lib. i tried to read library documentation, but found no answer. here is what supposed to execute after:

//if(now.hour())
    {  
    digitalWrite(servoPWcontrol, HIGH);
    delay(1000);
    myservo.write(70);
    delay(1500);
    myservo.write(112);
    delay(1000);
    digitalWrite(servoPWcontrol, LOW);
    Serial.println("Servo moved. They need more...");
    delay(60000);
    }  

ty!

I don't see any reference to the alarm time in the code that you posted. Why have you got a 9 second delay() in loop() ?

The library appears to provide now.hour(), now.minute() and even now.second() functions so can't you simply check the relevant values to see whether your alarm time has been reached and, if so, take action ?

It's 900 ms, almost 1 sec. So I can check time almost every second.
And yes, I'm trying to figure command how to to set "alarm" (action) in exact time.

My apologies, but why have a delay() at all. Neither the Arduino or the RTC will care how often you check the time

As to testing whether your alarm time has been reached then consider something like this

if (now.hour() == 10 && now.minute() == 0)
{
    //do something
}
1 Like

so action time not execute more than once, in choosen time. im not sure how to set to execute only once..
and it will consume less power, no?

Set a boolean to true once the alarm time is reached and use its value to control whether the dependant code runs or not

Not enough that you will notice, if at all

anyway, i add seconds too, so it executing only once.
thanks for help.
i saw many times, that folks adding delay, as they said - to spare power more, because of less commands executing, when its not needed... guess that false statement? :slight_smile:

Whether the Arduino is executing a delay() or whether it is doing something else it is always doing something. It is never idle, so you might just as well have it do something useful instead of just wasting its time

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