no. the main problem here is my program where i dont know whats wrong the connections here are all okay
the servo motor will run in the indicated time there in the program which is 5am,10am, 1pm and 4pm but its not runnig when it is already time
Please, explain more what this means.
example, when its 4pm the servo motor will run but its not running
Is this the code section that is not working ?
else if ((rtc.getTime().hour >= 16) && (rtc.getTime().hour <= 17))
{
for (pos = 45; pos <= 180; pos += 1)
{
servo1.write(pos);
servo2.write(pos);
delay(15);
}
for (pos = 180; pos >= 45; pos -= 1)
{
servo1.write(pos);
servo2.write(pos);
delay(15);
}
}
the whole void loop i think? and the void set up too?
Try this sketch to see if the RTC is working.
What do your get on the serial monitor ?
// Date and time functions using a DS3231 RTC connected via I2C and Wire lib
#include <Wire.h>
#include "RTClib.h"
RTC_DS3231 rtc;
char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
//***********************************************************************
void setup ()
{
Serial.begin(9600);
if (! rtc.begin())
{
Serial.println("Couldn't find RTC");
while (1);
}
if (rtc.lostPower())
{
Serial.println("RTC lost power");
}
}
//***********************************************************************
void loop ()
{
DateTime now = rtc.now();
int HOUR = now.hour();
Serial.print(now.year(), DEC);
Serial.print('/');
Serial.print(now.month(), DEC);
Serial.print('/');
Serial.print(now.day(), DEC);
Serial.print(" (");
Serial.print(daysOfTheWeek[now.dayOfTheWeek()]);
Serial.print(") ");
Serial.print(now.hour(), DEC);
Serial.print(':');
Serial.print(now.minute(), DEC);
Serial.print(':');
Serial.print(now.second(), DEC);
Serial.println();
if (now.hour() == HOUR)
{
Serial.println("It's time to feed the cat!");
}
delay(1000);
}
how can i insert the servo motor in this program?
One step at a time.
Does your serial monitor display anything when you try the sketch ?
Library is at:
yes
i also have that library too
What happens with this sketch ?
// Date and time functions using a DS3231 RTC connected via I2C and Wire lib
#include <Wire.h>
#include "RTClib.h" //https://github.com/adafruit/RTClib
#include <Servo.h>
int pos = 0;
Servo servo1;
Servo servo2;
RTC_DS3231 rtc;
char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
DateTime now;
//***********************************************************************
void setup ()
{
Serial.begin(9600);
rtc.begin();
//Set the time.
//rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
//
// YYYY MM DT HH MM SS
//rtc.adjust(DateTime(2021, 5, 11, 13, 59, 59));
servo1.attach(6);
servo2.attach(9);
} //END of setup()
//***********************************************************************
void loop ()
{
now = rtc.now();
Serial.print(now.year(), DEC);
Serial.print('/');
Serial.print(now.month(), DEC);
Serial.print('/');
Serial.print(now.day(), DEC);
Serial.print(" (");
Serial.print(daysOfTheWeek[now.dayOfTheWeek()]);
Serial.print(") ");
Serial.print(now.hour(), DEC);
Serial.print(':');
Serial.print(now.minute(), DEC);
Serial.print(':');
Serial.print(now.second(), DEC);
Serial.println();
//******************************
if (now.hour() >= 13 && now.hour() <= 14)
{
//*******
for (pos = 45; pos <= 180; pos += 1)
{
Serial.println("Servo is going to position 180'");
servo1.write(pos);
servo2.write(pos);
delay(15);
}
//*******
for (pos = 180; pos >= 45; pos -= 1)
{
Serial.println("Servo is going to position 0'");
servo1.write(pos);
servo2.write(pos);
delay(15);
}
}
delay(1000);
} //END of loop()
//******************************************************************************
You may have to set the time in your RTC.
The time here is 13:00
Hence:
if (now.hour() >= 13 && now.hour() <= 14) //is the time 13:00 to 14:00 inclusive ? <----<<<< set this to what you want.
will i remove the // here?
// YYYY MM DT HH MM SS
rtc.adjust(DateTime(2021, 5, 11, 13, 59, 59)); //change this to your time
After you run the above line of code uncommented, your RTC time will have been set.
Next comment that line of code and upload the sketch a second time.
if i change the time here
if (now.hour() >= 13 && now.hour() <= 14)
i will change it here too?
rtc.adjust(DateTime(2021, 5, 11, 13, 59, 59));
by the way, the time that will servo will run are 5am, 10am, 1pm, and 4pm
All of it. ![]()
a7
Test question.
What time does this sketch feed the cat ?
// Date and time functions using a DS3231 RTC connected via I2C and Wire lib
#include <Wire.h>
#include "RTClib.h" //https://github.com/adafruit/RTClib
#include <Servo.h>
int pos = 0;
Servo servo1;
Servo servo2;
RTC_DS3231 rtc;
char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
DateTime now;
//***********************************************************************
void setup ()
{
Serial.begin(9600);
rtc.begin();
//Set the time.
//rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
//
// YYYY MM DT HH MM SS
//rtc.adjust(DateTime(2021, 5, 11, 13, 59, 59));
servo1.attach(6);
servo2.attach(9);
} //END of setup()
//***********************************************************************
void loop ()
{
now = rtc.now();
Serial.print(now.year(), DEC);
Serial.print('/');
Serial.print(now.month(), DEC);
Serial.print('/');
Serial.print(now.day(), DEC);
Serial.print(" (");
Serial.print(daysOfTheWeek[now.dayOfTheWeek()]);
Serial.print(") ");
Serial.print(now.hour(), DEC);
Serial.print(':');
Serial.print(now.minute(), DEC);
Serial.print(':');
Serial.print(now.second(), DEC);
Serial.println();
//******************************
if (now.hour() == 14 && now.minute() == 15 && now.second() == 30)
{
//*******
for (pos = 45; pos <= 180; pos += 1)
{
Serial.println("Servo is going to position 180'");
servo1.write(pos);
servo2.write(pos);
delay(15);
}
//*******
for (pos = 180; pos >= 45; pos -= 1)
{
Serial.println("Servo is going to position 0'");
servo1.write(pos);
servo2.write(pos);
delay(15);
}
}
//******************************
delay(250);
} //END of loop()
//******************************************************************************
2pm?