import time
import board
import busio
import digitalio
import pwmio
import analogio
# Import your newly uploaded custom LCD files!
from i2c_pcf8574_interface import I2CPCF8574Interface
from lcd import LCD
# ==========================================
# 1. SETUP HARDWARE
# ==========================================
# Setup I2C
i2c = busio.I2C(board.GP1, board.GP0)
# Initialize your custom LCD (Address 0x27 for Wokwi)
interface = I2CPCF8574Interface(i2c, 0x27)
lcd = LCD(interface, num_cols=16, num_rows=2)
# Button
btn = digitalio.DigitalInOut(board.GP15)
btn.direction = digitalio.Direction.INPUT
btn.pull = digitalio.Pull.UP
# Weight Sensor (Potentiometer)
load_sensor = analogio.AnalogIn(board.GP26)
# Buzzer
buzzer = digitalio.DigitalInOut(board.GP14)
buzzer.direction = digitalio.Direction.OUTPUT
# Servo
pwm_servo = pwmio.PWMOut(board.GP16, frequency=50)
def set_hatch(angle):
min_duty = 1638
max_duty = 8192
pwm_servo.duty_cycle = int(min_duty + (angle / 180) * (max_duty - min_duty))
set_hatch(0)
# ==========================================
# 2. VARIABLES
# ==========================================
TARGET_WEIGHT = 40000
FEED_INTERVAL_SECONDS = 15 # Automatically feeds every 15 seconds for testing
last_feed_time = time.monotonic()
lcd.clear()
lcd.print("Feeder Ready!\nNo Requirements")
time.sleep(2)
# ==========================================
# 3. MAIN LOOP
# ==========================================
while True:
# Calculate time using the internal stopwatch
current_time = time.monotonic()
time_elapsed = current_time - last_feed_time
time_left = max(0, FEED_INTERVAL_SECONDS - int(time_elapsed))
# Update LCD with a countdown
lcd.set_cursor_pos(0, 0)
lcd.print(f"Auto-feed in: {time_left:02d}s ")
button_pressed = not btn.value
time_match = time_elapsed >= FEED_INTERVAL_SECONDS
if button_pressed or time_match:
lcd.clear()
lcd.print("Dispensing...\nAdd weight!")
# Open Hatch
set_hatch(90)
# Wait for weight (turn the dial)
while load_sensor.value < TARGET_WEIGHT:
time.sleep(0.1)
# Close Hatch
set_hatch(0)
lcd.clear()
lcd.print("Bowl is full!")
# Beep
buzzer.value = True
time.sleep(0.5)
buzzer.value = False
time.sleep(2)
lcd.clear()
last_feed_time = time.monotonic()
time.sleep(0.1)