from machine import I2C, Pin, PWM
from time import sleep
from ds1307 import RTClib
from lcd_api import LcdApi
from i2c_lcd import I2cLcd
# I2C setup
i2c = I2C(0, sda=Pin(21), scl=Pin(22), freq=100000)
# RTC setup
rtc = DS1307(i2c)
# LCD setup
LCD_I2C_ADDR = 0x27
lcd = I2cLcd(i2c, LCD_I2C_ADDR, 2, 16)
# Servo setup
servo = PWM(Pin(5), freq=50)
# Servo function
def set_servo_angle(angle):
duty = int(40 + (angle / 180) * 115) # Convert angle to duty cycle
servo.duty(duty)
sleep(1)
servo.duty(0)
# Feeding function
def feed():
lcd.clear()
lcd.putstr("FEEDING TIME..")
set_servo_angle(180)
sleep(3)
set_servo_angle(0)
lcd.clear()
# Main loop
try:
while True:
# Read time from RTC
rtc_time = rtc.datetime() # (year, month, day, weekday, hour, minute, second, subsecond)
year, month, day, _, hour, minute, second, _ = rtc_time
# Display date and time on LCD
lcd.clear()
lcd.putstr(f"Tang: {day:02d}-{month:02d}-{year}")
lcd.move_to(0, 1)
lcd.putstr(f"Waktu: {hour:02d}:{minute:02d}:{second:02d}")
# Feeding schedules
if hour == 6 and minute == 30 and second == 0:
feed()
elif hour == 13 and minute == 15 and second == 0:
feed()
elif hour == 17 and minute == 45 and second == 0:
feed()
sleep(1)
except KeyboardInterrupt:
print("Simulation stopped.")