from machine import Pin, SoftI2C, ADC, I2C
import ssd1306
import utime
from picozero import Speaker, LED
xAxis = ADC(Pin(27))
yAxis = ADC(Pin(26))
button = Pin(16, Pin.IN, Pin.PULL_UP)
i2c = SoftI2C(scl=Pin(5), sda=Pin(4))
oled_width = 128
oled_height = 64
oled = ssd1306.SSD1306_I2C(oled_width, oled_height, i2c)
light = LED(17)
speaker = Speaker(15)
menu_items = ["Alarm", "Flashlight", "Temperature", "SOS"]
current_selection = 0
in_alarm_mode = False
flashlight_on = False
sos_active = False
MPU6050_ADDR = 0x68
PWR_MGMT_1 = 0x6B
TEMP_OUT_H = 0x41
def init_i2c():
return I2C(0, scl=Pin(9), sda=Pin(8), freq=400000)
def write_byte(i2c, addr, reg, data):
i2c.writeto_mem(addr, reg, bytearray([data]))
def read_word(i2c, addr, reg):
data = i2c.readfrom_mem(addr, reg, 2)
value = (data[0] << 8) | data[1]
return value if value < 0x8000 else value - 0x10000
def init_mpu6050(i2c):
write_byte(i2c, MPU6050_ADDR, PWR_MGMT_1, 0)
def read_temp(i2c):
temp_raw = read_word(i2c, MPU6050_ADDR, TEMP_OUT_H)
temp_celsius = (temp_raw / 340.0) + 36.53
return temp_celsius
def display_menu():
oled.fill(0)
for i, item in enumerate(menu_items):
if i == current_selection:
oled.text('> ' + item, 0, i * 10)
else:
oled.text(' ' + item, 0, i * 10)
oled.show()
def sound_alarm():
while in_alarm_mode:
speaker.on()
utime.sleep(0.1)
speaker.off()
utime.sleep(0.1)
check_button()
def check_button():
if button.value() == 0:
stop_alarm()
def stop_alarm():
global in_alarm_mode, sos_active
in_alarm_mode = False
sos_active = False
speaker.off()
display_menu()
def toggle_flashlight():
global flashlight_on
flashlight_on = not flashlight_on
if flashlight_on:
light.on()
oled.fill(0)
oled.text("Flashlight On", 0, 0)
oled.show()
else:
light.off()
display_menu()
def send_sos():
global sos_active
sos_active = True
for _ in range(3):
light.on()
speaker.on()
utime.sleep(0.1)
light.off()
speaker.off()
utime.sleep(0.1)
for _ in range(3):
light.on()
speaker.on()
utime.sleep(0.3)
light.off()
speaker.off()
utime.sleep(0.3)
for _ in range(3):
light.on()
speaker.on()
utime.sleep(0.1)
light.off()
speaker.off()
utime.sleep(0.1)
sos_active = False
display_menu()
i2c = init_i2c()
init_mpu6050(i2c)
oled.fill(0)
oled.text("Booting...", 0, 0)
oled.show()
utime.sleep(3)
display_menu()
while True:
xValue = xAxis.read_u16()
yValue = yAxis.read_u16()
buttonValue = button.value()
if yValue <= 600 and current_selection < len(menu_items) - 1:
current_selection += 1
display_menu()
utime.sleep(0.2)
elif yValue >= 60000 and current_selection > 0:
current_selection -= 1
display_menu()
utime.sleep(0.2)
if buttonValue == 0:
if in_alarm_mode:
stop_alarm()
elif sos_active:
stop_alarm()
else:
if menu_items[current_selection] == "Alarm":
oled.fill(0)
oled.text("Alarm Activated", 0, 0)
oled.show()
in_alarm_mode = True
sound_alarm()
elif menu_items[current_selection] == "Flashlight":
toggle_flashlight()
elif menu_items[current_selection] == "Temperature":
temp_celsius = read_temp(i2c)
oled.fill(0)
oled.text("Temperature:", 0, 0)
oled.text(f"{temp_celsius:.2f} C", 0, 10)
oled.show()
while button.value() == 1:
utime.sleep(0.1)
display_menu()
elif menu_items[current_selection] == "SOS":
oled.fill(0)
oled.text("Sending SOS...", 0, 0)
oled.show()
send_sos()
utime.sleep(0.05)