#include <Wire.h>
#include <RTClib.h>
#include <Servo.h>
#include <LiquidCrystal_I2C.h>
RTC_DS3231 rtc;
Servo myservo;
LiquidCrystal_I2C lcd(0x27, 20, 4);
const int servoPin = 9; // Pin yang terhubung ke servo
const int rotateInterval = 1000; // Setiap 4 jam dalam milidetik
unsigned long lastRotateTime = 0;
void setup() {
Serial.begin(9600);
Wire.begin();
if (!rtc.begin()) {
Serial.println("Modul RTC tidak ditemukan!");
while (1);
}
myservo.attach(servoPin);
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("Servo Rotation");
}
void loop() {
DateTime now = rtc.now();
unsigned long currentTime = now.unixtime() * 1000; // Waktu dalam milidetik
// Putar servo setiap 4 jam
if (currentTime - lastRotateTime >= rotateInterval) {
myservo.write(90); // Putar servo ke 90 derajat
delay(1000); // Tunggu sebentar agar servo bergerak
myservo.write(0); // Kembali ke posisi awal
lastRotateTime = currentTime;
}
lcd.setCursor(0, 1);
lcd.print("Last rotation:");
lcd.setCursor(0, 3);
lcd.print("Interval: 4 hours");
delay(1000);
}