print("Pill Medicine Reminder ")
print("Created by: FARISHA HANUM BINTI SANYZAM on 22/4/2025")
# Import libraries
from machine import Pin, SoftI2C, ADC, PWM
import ssd1306_library
from utime import sleep, time
# Initialize I2C for OLED
i2c = SoftI2C(scl=Pin(22), sda=Pin(21))
print("I2C Devices found:", [hex(x) for x in i2c.scan()])
#Object Declaration (only needed when device has library)
#Object_Name = Library_Name.Class_Name (....)
screen = ssd1306_library.SSD1306_I2C(128, 64, i2c, addr=0x3C)
screen.fill(1)
screen.text("System Starting", 0, 20, 0)
screen.show()
# Pin Declaration
servo = PWM(Pin(5), freq=50)
servo.duty(0)
buzzer = PWM(Pin(18), freq=440, duty=0)
pot = ADC(Pin(35))
pot.atten(ADC.ATTN_11DB)
ldr = ADC(Pin(34))
ldr.atten(ADC.ATTN_11DB)
red = PWM(Pin(12), freq=1000, duty=0)
green = PWM(Pin(13), freq=1000, duty=0)
blue = PWM(Pin(14), freq=1000, duty=0)
LED = Pin(26, Pin.OUT, value=0)
confirm_btn = Pin(25, Pin.IN, Pin.PULL_UP)
stop_btn = Pin(27, Pin.IN, Pin.PULL_UP)
# Medicine schedule
PILL_SCHEDULE = [
{"delay": 10, "name": "Ubat Darah Tinggi", "dose": "1 tablet"},
{"delay": 30, "name": "Ubat Kolesterol", "dose": "2 tablet"}
]
# Light thresholds
LDR_DARK = 1000
LDR_BRIGHT = 2000
def set_rgb(r, g, b):
red.duty(int(r * 1023 / 255))
green.duty(int(g * 1023 / 255))
blue.duty(int(b * 1023 / 255))
def check_light():
light = ldr.read()
if light < LDR_DARK:
LED.value(1)
set_rgb(50, 0, 0)
return False
elif light > LDR_BRIGHT:
LED.value(0)
set_rgb(0, 0, 0)
return True
return None
def move_servo():
set_rgb(0, 50, 50)
servo.duty(40)
sleep(0.5)
servo.duty(115)
sleep(0.5)
servo.duty(0)
set_rgb(0, 0, 0)
def get_volume():
pot_value = pot.read()
return max(0, min(1023, pot_value // 4))
def sound_buzzer():
volume = get_volume()
buzzer.duty(volume)
buzzer.freq(800)
set_rgb(50, 50, 0)
sleep(0.2)
buzzer.duty(0)
set_rgb(0, 0, 0)
sleep(0.1)
def show_alert(title, msg1, msg2=""):
screen.fill(1)
screen.text(title, 0, 10, 0)
screen.text(msg1, 0, 30, 0)
if msg2:
screen.text(msg2, 0, 45, 0)
vol = get_volume()
screen.text(f"Vol: {vol}", 80, 55, 0)
screen.show()
# Main Program
set_rgb(0, 50, 0)
sleep(1)
set_rgb(0, 0, 0)
start_time = time()
schedule_index = 0
while True:
light_status = check_light()
current_time = time()
elapsed = current_time - start_time
if schedule_index < len(PILL_SCHEDULE):
med = PILL_SCHEDULE[schedule_index]
if elapsed >= med["delay"]:
if light_status is False:
show_alert("Peringatan!", "Cahaya gelap!")
sleep(5)
continue
show_alert("Masa Ambil Ubat!", med["name"], f"Dos: {med['dose']}")
move_servo()
# Alert sequence
while True:
sound_buzzer()
if confirm_btn.value() == 0:
set_rgb(0, 100, 0) # Bright green on confirm
show_alert("", "Bismillah!", "Ubat diambil")
schedule_index += 1
buzzer.duty(0)
sleep(2)
set_rgb(0, 0, 0)
break
elif stop_btn.value() == 0:
set_rgb(100, 0, 0) # Bright red on stop
show_alert("", "Reminder", "Dihentikan")
schedule_index += 1
buzzer.duty(0)
sleep(2)
set_rgb(0, 0, 0)
break
sleep(0.1)
if schedule_index >= len(PILL_SCHEDULE):
set_rgb(0, 0, 100)
show_alert("", "Semua Ubat", "Sudah Diambil")
sleep(60)
start_time = time()
schedule_index = 0
set_rgb(0, 0, 0)
sleep(0.5)