RTC+ Servomotor

Good evening! I am having trouble with how to synchronize my RTC and Servomotor, I have here the set of codes I have debugged this past few days and still, it does not work. No errors were detected and yet we could still not find the right codes for the system to work.

The system’s function is to set the alarm of our RTC, and when the designated time comes, the alarm would trigger the servomotor’s movement.

Here are the set of sketch we used for the said system.

#include <Wire.h>
#include "RTClib.h"
#include <Time.h>
#include <TimeAlarms.h>
#include <Servo.h>
RTC_DS1307 RTC;

Servo myservo;
int pos = 0;

// the setup routine runs once when you press reset:
void setup() {
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object 

  
  Serial.begin(9600);
  Wire.begin();
  RTC.begin();

  
    
  Serial.println("Startup Completed");
  //setTime(8,29,0,1,2,15); // set time to Saturday 8:29:00am Jan 1 2011
  // create the alarms 
  Alarm.alarmRepeat(20,45,0, Feeding);  // 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 

 }

// the loop routine runs over and over again forever:
void loop() {

  DateTime now = RTC.now();

    Serial.print(now.hour(), DEC);
    Serial.print(':');
    Serial.print(now.minute(), DEC);
    Serial.print(':');
    Serial.print(now.second(), DEC);
    Serial.println();


Alarm.delay(1000); // wait one second between clock display
}

void Feeding() {
 Serial.println("Feeding Cycle Started");  
  delay(6000);
  {
  for(pos = 0; pos < 180; pos += 1)  // goes from 0 degrees to 180 degrees 
  {                                  // in steps of 1 degree 
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(15);                       // waits 15ms for the servo to reach the position 
  } 
  for(pos = 180; pos>=0; pos-=1)     // goes from 180 degrees to 0 degrees 
  {                                
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(15);                       // waits 15ms for the servo to reach the position 
  }
}  
Serial.println("Feeding Cycle Ended");  
delay(6000);

}

Thank you for your help and it would be appreciated.

“It does not work” doesn’t tell us what the problem actually is. Does the servo move at all? Does it move in a wrong way? Maybe the RTC goes crazy? The Arduino resets or burst in flames?

I suspect that, as happens so often, two of the libraries you use manipulate the same timer and interfere with each other. Another option is that you’re trying to power the servo from an Arduino pin. Why not try a version of this program with a simple LED indication instead of a servo? That way you’ll know if at least the RTC/Alarm part works correctly, and continue from there.

You haven't really said what's wrong.

  • Presumably you have set the RTC time, since the set time line in the code is //'d out
  • Is the correct time displaying in the monitor
  • Is the "feeding cycle started" line displaying
  • Does the servo work in a plain servo sketch like sweep

Personally, I use the DS1307RTC library.

Thanks for your reply,

-Servo motor does not move maybe because it is connected to the arduino pin.
-The RTC does not go crazy for it could read the time in a proper manner.
-I would just try to use LED for it to work.

the problem is that the alarm is not triggered to the time that i have set for the alarm to be triggered. the servomotor seems fine since i have used it, and example sketches like sweep works fine with it. i think there is something in the code that is not triggering the alarm for the function inside it to work.