import machine
import time
from mpu6050 import MPU6050
i2c = machine.I2C(0, sda=machine.Pin(4), scl=machine.Pin(5))
sensor = MPU6050(i2c)
ir_sensor = machine.Pin(16, machine.Pin.IN, machine.Pin.PULL_DOWN)
buzzer = machine.Pin(15, machine.Pin.OUT)
led = machine.Pin(14, machine.Pin.OUT)
IMPACT_THRESHOLD = 1.0
DROWSY_LIMIT = 2.0
eyes_closed_time = 0.0
print("--- SMART HELMET RUNNING SUCCESSFULLY ---")
while True:
accel = sensor.get_values()
g_total = (accel["AcX"]**2 + accel["AcY"]**2 + accel["AcZ"]**2)**0.5
if g_total > IMPACT_THRESHOLD:
print(f"IMPACT DETECTED: Force {g_total:.2f} G > 1.0 G. Turning LED ON!")
led.value(1)
buzzer.value(1)
else:
if ir_sensor.value() == 0:
led.value(0)
if ir_sensor.value() == 1:
eyes_closed_time += 0.2
if eyes_closed_time >= DROWSY_LIMIT:
print("WARNING: Drowsiness Detected! Wake Up!")
led.value(1)
buzzer.value(1)
else:
eyes_closed_time = 0.0
if g_total <= IMPACT_THRESHOLD:
led.value(0)
buzzer.value(0)
time.sleep(0.2)