Hey guys, I have this project that turns on the servo motor at a specific time using the RTC module.
Here's my code
I tried uploading this program to my mega but what happens is that the RTC code is being bypassed and the servo starts moving.
I'm not really good at programming. I hope anyone could help me
P.S. I tried using an i2c scanner, it did scanned the module
Mod edit: added code tags
Here's my code
C:
#include <DS3231.h>
#include <Servo.h>
Servo myservo;
int pos = 0;
int Relay = 9;
DS3231 rtc(SDA, SCL);
Time t;
const int OnHour = 1;
const int OnMin = 15;
const int OffHour = 1;
const int OffMin = 16;
void setup() {
Serial.begin(9600);
rtc.begin();
pinMode(Relay, OUTPUT);
digitalWrite(Relay, LOW);
myservo.attach(9);
}
void loop() {
t = rtc.getTime();
Serial.print(t.hour);
Serial.print(" hour(s), ");
Serial.print(t.min);
Serial.print(" minute(s)");
Serial.println(" ");
delay (1000);
if(t.hour == OnHour && t.min == OnMin){
digitalWrite(Relay,HIGH);
Serial.println("LIGHT ON");
}
else if(t.hour == OffHour && t.min == OffMin){
digitalWrite(Relay,LOW);
Serial.println("LIGHT OFF");
}
{
for (pos = 0; pos <= 60; 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(10); // waits 15ms for the servo to reach the position
}
for (pos = 60; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(10); // waits 15ms for the servo to reach the position
}
}
}
I'm not really good at programming. I hope anyone could help me
P.S. I tried using an i2c scanner, it did scanned the module
Mod edit: added code tags
Last edited: