from pdisplay import Display
from pbuzzer import PillBuzzer
from pinput import Input
from pmotor import Motor
from pled import Blinker
from prfid import RFID
from time import sleep, ticks_ms, ticks_diff
PIN_BUZZER = 16
PIN_DISPLAY_SDA = 18
PIN_DISPLAY_SCL = 19
PIN_MOTOR = 20
PIN_INPUT_HOUR = 13
PIN_INPUT_MIN = 12
PIN_INPUT_CONFIRM = 11
PIN_RFID_SPI_RST = 0
PIN_RFID_SCK = 2
PIN_RFID_MISO = 4
PIN_RFID_MOSI = 3
PIN_RFID_CS = 1
PIN_BLINKER = 15
GRACE = 10000
display = Display(PIN_DISPLAY_SDA, PIN_DISPLAY_SCL)
buzzer = PillBuzzer(PIN_BUZZER)
blinker = Blinker(PIN_BLINKER, 0.2)
display.display_pill_reminder()
motor = Motor(PIN_MOTOR)
pill_served = False
def scanned(card_id):
global pill_served
if pill_served:
return
# FIX 3: Your exact Wokwi JSON Tag integer and the '!=' operator
if card_id != 28804199028494596:
print(f"Invalid card ID: {card_id}")
return
print(f"Scanned card with ID: {card_id}")
blinker.stop()
buzzer.stop() # <--- FIXED: Added so the buzzer actually shuts up!
pill_served = True
countdown = 3 # FIX 5: Actually set to 5 for the test!
delay_sec = 0
def input_confirmed(hr, mn):
global delay_sec, countdown # <--- FIXED: Removed the undefined timer_running
print("Timer confirmed!")
print("Time set: {:02d}:{:02d}".format(hr, mn))
# THE FREEZE FIX: If 00:00 is entered, default to a 5-second test!
if hr == 0 and mn == 0:
print("00:00 detected! Running 3-second test...")
delay_sec = 3
else:
delay_sec = mn * 60 + hr * 3600
countdown = delay_sec
def timer_elapsed():
global pill_served
blinker.start()
display.display_pill_reminder()
while not pill_served:
# --- Phase 1: Music is playing ---
# Note: play_tune_async sets is_playing = True internally when it starts
buzzer.play_tune_async()
while not pill_served and buzzer.is_playing:
rfid.update()
sleep(0.01)
# --- Phase 2: 5-second Quiet Window (only if not yet scanned) ---
if not pill_served:
print("Song finished. Starting 5s quiet window...")
start_quiet = ticks_ms()
# Scan continuously for 5000ms without playing sound
while not pill_served and ticks_diff(ticks_ms(), start_quiet) < 5000:
rfid.update()
sleep(0.01)
# Tag has been verified
motor.push_pill() #
print("SERVING PILL")
pill_served = False
input = Input(PIN_INPUT_HOUR, PIN_INPUT_MIN, PIN_INPUT_CONFIRM, input_confirmed, display)
rfid = RFID(0, PIN_RFID_SCK, PIN_RFID_MISO, PIN_RFID_MOSI, PIN_RFID_CS, PIN_RFID_SPI_RST, scanned)
while True:
if input.confirmed:
if countdown > 0:
display.display_countdown(countdown)
countdown -= 1
sleep(1)
if countdown == 0:
timer_elapsed()
countdown = delay_sec
continue
sleep(1)Loading
ssd1306
ssd1306