from machine import Pin, I2C
import time
import utime
import ssd1306
import framebuf
# Set up button pins
button_hour = Pin(13, Pin.IN)
button_minute = Pin(12, Pin.IN)
button_confirm = Pin(11, Pin.IN)
# I2C pins for Pico
sda = Pin(18)
scl = Pin(19)
i2c = I2C(1, sda=sda, scl=scl, freq=400000)
display = ssd1306.SSD1306_I2C(128, 64, i2c, 0x3C)
# Button setup
push_button = Pin(10, Pin.IN, Pin.PULL_DOWN)
# Red LED setup
redled = Pin(15, Pin.OUT)
hours = 0
minutes = 0
confirmed = False
# Debounce time
DEBOUNCE_DELAY = 0.2 # seconds (200ms)
def wait_for_release(pin):
# Waits for button release with debounce
time.sleep(0.02) # small initial debounce (20ms)
while pin.value() == 1:
time.sleep(0.01)
time.sleep(DEBOUNCE_DELAY) # delay after release
def draw_pill_image(x, y, width, height): #This prints a image of a "pill" if not necessary can be discarded
"""Draws a pill-like shape."""
buffer = bytearray(width * height // 8)
frame = framebuf.FrameBuffer(buffer, width, height, framebuf.MONO_HLSB)
frame.hline(2, 0, width - 4, 1)
frame.hline(2, height - 1, width - 4, 1)
frame.vline(0, 2, height - 4, 1)
frame.vline(width - 1, 2, height - 4, 1)
frame.pixel(1, 1, 1)
frame.pixel(1, 2, 1)
frame.pixel(1, height - 3, 1)
frame.pixel(1, height - 2, 1)
frame.pixel(width - 2, 1, 1)
frame.pixel(width - 2, 2, 1)
frame.pixel(width - 2, height - 3, 1)
frame.pixel(width - 2, height - 2, 1)
frame.vline(width // 2, 0, height, 1)
display.blit(frame, x, y)
def display_pill_reminder():
"""Displays the pill reminder message and image, blinks red LED."""
display.fill(0)
display.text("Medication time!", 1, 0, 1)
draw_pill_image(40, 20, 48, 24)
display.show()
led_state = 0
blink_start = utime.ticks_ms()
blink_interval = 500 # Adjust blink speed
while push_button.value() == 0: # Wait for button press
now = utime.ticks_ms()
if utime.ticks_diff(now, blink_start) >= blink_interval:
redled.value(led_state)
led_state = 1 - led_state # Toggle LED
blink_start = now
utime.sleep_ms(10) # check the button every 10ms
redled.off() # turn off the led once the button is pressed.
def display_countdown(remaining_time):
"""Displays the countdown timer with text."""
hours = remaining_time // 3600
minutes = (remaining_time % 3600) // 60
seconds = remaining_time % 60
time_str = "{:02d}:{:02d}:{:02d}".format(hours, minutes, seconds)
display.fill(0)
display.text("*PILL PUSHER*", (128 - len("*PILL PUSHER*") * 8) // 2, 0, 1)
display.text("Next pill in:", 0, 16, 1)
display.text(time_str, (128 - len(time_str) * 8) // 2, 32, 1)
display.show()
#########
def display_timer_setup():
"""Displays the timer setup screen and updates values dynamically."""
global hours, minutes, confirmed
while not confirmed:
display.fill(0) # Clear the screen
display.text("Set the timer:", 0, 0, 1) # Line 1
display.text("Hours set to: {:02d}".format(hours), 0, 16, 1) # Line 2
display.text("Mins set to: {:02d}".format(minutes), 0, 32, 1) # Line 3
display.show()
if button_hour.value() == 1:
hours = (hours + 1) % 24
wait_for_release(button_hour)
if button_minute.value() == 1:
minutes = (minutes + 1) % 60
wait_for_release(button_minute)
if button_confirm.value() == 1:
confirmed = True
wait_for_release(button_confirm)
def display_confirmation():
"""Displays confirmation message for 3 seconds before moving forward."""
display.fill(0)
display.text("Timer confirmed!", 0, 0, 1) # Line 1
display.text("Final time set:", 0, 16, 1) # Line 2
display.text("{:02d}:{:02d}".format(hours, minutes), (128 - len("{:02d}:{:02d}".format(hours, minutes)) * 8) // 2, 32, 1)
display.show()
utime.sleep(3) # Hold the confirmation for 3 seconds
# Main loop
display_timer_setup() # Runs the setup process
display_confirmation() # Shows confirmation for 3 seconds before starting the countdown
countdown_seconds = 10 # Set countdown to 5 seconds for testing
while True:
if countdown_seconds <= 0:
display_pill_reminder()
countdown_seconds = 10 # Reset countdown
else:
display_countdown(countdown_seconds)
countdown_seconds -= 1
utime.sleep(1)
'''
# Setting the timer
print("Set the timer:")
while not confirmed:
# Cycles through hours
if button_hour.value() == 1:
hours = (hours + 1) % 24
print("Hours set to:", hours)
wait_for_release(button_hour)
# Cycles through minutes
if button_minute.value() == 1:
minutes = (minutes + 1) % 60
print("Minutes set to:", minutes)
wait_for_release(button_minute)
# Sets final timer value
if button_confirm.value() == 1:
confirmed = True
wait_for_release(button_confirm)
# Simple print to confirm, not sure how we'll incorperate all the "prints" with the screen
print("Timer confirmed!")
print("Final time set: {:02d}:{:02d}".format(hours, minutes))
'''