import machine
import time
from machine import Pin, I2C
from mpu6050 import MPU6050 # Assuming MPU6050 is used for accelerometer and gyroscope
from lcd_api import LcdApi
from pico_i2c_lcd import I2cLcd
# Initialize LCD
i2c = I2C(0, scl=Pin(1), sda=Pin(0), freq=400000)
lcd = I2cLcd(i2c, 0x27, 2, 16)
# Initialize MPU6050 (accelerometer and gyroscope)
i2c = I2C(0, scl=Pin(9), sda=Pin(8))
mpu = MPU6050(i2c)
# Initialize EMG sensor and buzzer pins
emg_pin = Pin(2, Pin.IN)
buzzer_pin = Pin(3, Pin.OUT)
# Function to read EMG sensor
def read_emg():
return emg_pin.value()
# Function to detect tremors using accelerometer and gyroscope
def detect_tremors():
accel_x, accel_y, accel_z = mpu.acceleration
gyro_x, gyro_y, gyro_z = mpu.gyro
# Implement tremor detection logic based on thresholds
# Example:
if abs(gyro_x) > 100 or abs(gyro_y) > 100:
return True
else:
return False
# Function to display data on LCD
def display_data(message):
lcd.clear()
lcd.move_to(0, 0)
lcd.putstr(message)
# Main loop to monitor and alert
while True:
if read_emg():
# EMG activity detected
display_data("Muscle activity")
buzzer_pin.on() # Activate buzzer
time.sleep(1)
buzzer_pin.off()
elif detect_tremors():
# Tremor detected
display_data("Tremor detected")
buzzer_pin.on()
time.sleep(1)
buzzer_pin.off()
else:
display_data("Monitoring")
time.sleep(0.1) # Adjust as needed