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.