from machine import I2C, Pin
import time
from mpu6050 import MPU6050
from lcd_api import LcdApi
from i2c_lcd import I2cLcd
i2c = I2C(0, scl=Pin(1), sda=Pin(0), freq=400000)
lcd_addr = i2c.scan()[0]
lcd = I2cLcd(i2c, lcd_addr, 2, 16)
mpu = MPU6050(i2c)
led = Pin(15, Pin.OUT)
button = Pin(14, Pin.IN, Pin.PULL_DOWN)
step_count = 0
threshold = 1.2
last_step_time = time.ticks_ms()
lcd.clear()
lcd.putstr("Steps: 0")
def reset_steps(pin):
global step_count
step_count = 0
lcd.move_to(7, 0)
lcd.putstr(" ")
lcd.move_to(7, 0)
lcd.putstr(str(step_count))
button.irq(trigger=Pin.IRQ_RISING, handler=reset_steps)
while True:
accel = mpu.get_values()
az = accel['Az'] / 16384.0
now = time.ticks_ms()
if abs(az - 1.0) > threshold and time.ticks_diff(now, last_step_time) > 300:
step_count += 1
last_step_time = now
led.on()
time.sleep(0.1)
led.off()
lcd.move_to(7, 0)
lcd.putstr(" ")
lcd.move_to(7, 0)
lcd.putstr(str(step_count))
time.sleep(0.05)