import time # Importing time module for sleep functionality # Importing Speaker from picozero (assuming you'll use this later)
from machine import Pin, ADC, I2C, PWM, SPI # Importing Pin and ADC classes from machine module
from pico_i2c_lcd import I2cLcd
import random
from neopixel import Neopixel
import max7219
pixels = Neopixel(17, 0, 6, "GRB")
time.sleep(0.05)
# To-Do List:
# 1. Implement a button that starts scanning and turns on the LED - DONE
# 2. Use temperature or light sensor as the muscle sensor - DONE
# 3. Output the results on the console - DONE
# 4. Convert the values to a realistic value DONE
# 5. Print a different message depending on the condition DONE
# 5. Print the results on either a screen or seven-segment display DONE
# Extras:
# 1. Make random values for the muscle test DONE
# 2. Use a buzzer to indicate that it is testing DONE
# 3. Dot Matrix with name of team
# Initializing components
on_offLed = Pin(14, Pin.OUT) # LED to indicate test start
startTestButton = Pin(16, Pin.IN, Pin.PULL_DOWN) # Button to start the test
muscle_sensor = ADC(26) # ADC to read sensor values
buzzer = PWM(Pin(15))
#Intialize the SPI for dot matrix
spi = SPI(0, baudrate=10000000, polarity=1, phase=0, sck=Pin(2), mosi=Pin(3))
ss = Pin(5, Pin.OUT)
# creating an I2C object for lcd, specifying the data (SDA) and clock (SCL) pins used in the Raspberry Pi Pico
i2c = I2C(0, sda=Pin(0), scl=Pin(1), freq=400000)
# getting I2C address
I2C_ADDR = i2c.scan()[0]
# creating an LCD object using the I2C address and specifying number of rows and columns in the LCD
# LCD number of rows = 2, number of columns = 16
lcd = I2cLcd(i2c, I2C_ADDR, 4, 20)
# Number of readings to average
num_readings = 16
# Create matrix display instant, which has four MAX7219 devices.
display = max7219.Matrix8x8(spi, ss, 4)
#Set the display brightness. Value is 1 to 15.
display.brightness(1)
#Define the scrolling message
scrolling_message = "Dev Squad"
#Get the message length
length = len(scrolling_message)
#Calculate number of columns of the message
column = (length * 8)
#Clear the display.
display.fill(0)
display.show()
#sleep for one one seconds
time.sleep(1)
#neopixel setup
colors = [
(0xb6, 0xe4, 0x30),
(0x42, 0xd1, 0xe0),
]
pixel_index = 0
color_index = 0
def dot_matrix_start():
pass
def neo_pixel_start():
global pixel_index, color_index
pixels.set_pixel(pixel_index, colors[color_index])
pixels.show()
#print(pixel_index, colors[color_index])
pixel_index += 1
if pixel_index == 16:
pixel_index = 0
color_index = (color_index + 1) % 2
time.sleep(0.1)
def play_monitor_sound():
beep_duration = 0.1 # Duration of each beep in seconds
pause_duration = 0.1 # Duration of the pause between beeps
# Beep pattern similar to a blood pressure monitor
beep_pattern = [1000, 1000, 1500, 1500, 1000, 1000] # Frequencies for the beeps
for freq in beep_pattern:
buzzer.freq(freq) # Set the frequency for the beep
buzzer.duty_u16(32768) # Set duty cycle to 50%
time.sleep(beep_duration) # Wait for beep duration
buzzer.duty_u16(0) # Turn off the buzzer
time.sleep(pause_duration) # Wait before next beep
def high_risk():
if display_value>= 2999:
print("!Very High risk of injury, please end your training session.")
lcd.putstr(str(display_value) + " µV" + " Very high risk of injury, please end your training session")
def moderate_risk():
if 1500 < display_value < 2999:
print("Moderate muscle activation, you are able to do high-intensity training")
lcd.putstr(str(display_value) + " µV" + " Moderate muscle activation, you are ready to do high-intensity training")
def low_risk():
if 150 < display_value < 1500:
print("Low muscle activation, you are able to moderate-intensity training")
lcd.putstr(str(display_value) + " µV" + " Low muscle activation, you are ready to do moderate-intensity training")
def very_low_risk():
if 50 < display_value < 150:
print("Low muscle activation, you are able to do low-intensity training")
lcd.putstr(str(display_value) + " µV" + " Low muscle activation, you should start with low-intensity training")
def at_rest():
if display_value < 50:
print("Very low muscle activation, please start with low-intensity training to avoid injury")
lcd.putstr(str(display_value) + " µV" + " Very low muscle activation, please start with low-intensity training")
lcd.putstr("Please press the button.")
# Main loop
while True:
total_val = 0 # Reset total value for averaging
#buzzer.on()
button_pressed = startTestButton.value() # Check if the button is pressed
display_value = random.randint(5, 4000)
neo_pixel_start()
# Starting the test if the button is pressed
if button_pressed == 1:
lcd.clear()
on_offLed.value(1) # Turn on the LED to indicate test start
print("EMG test starting now, results will take a few seconds")
lcd.putstr("EMG test starting now, results will take a few seconds")
play_monitor_sound()
time.sleep(0.5)
# Taking multiple readings to average
#for i in range(num_readings):
# Read ADC digital output
#results_digital = muscle_sensor.read_u16()
# Convert digital value to analog voltage (0-3.3V)
#analog_val = float(results_digital / (2**16 - 1) * 3.3)
#total_val += analog_val # Accumulate the analog values for averaging
#time.sleep(0.01) # Small delay between readings
# Calculate the average analog value
#average_analog_val = total_val / num_readings
#display_value = int(average_analog_val * 1000) # Convert to mV for display
#display_value += 10
lcd.clear()
# Output the result
print(display_value, "µV")
high_risk()
moderate_risk()
low_risk()
very_low_risk()
at_rest()
time.sleep(0.01) # Short delay
on_offLed.value(0) # Turn off the LED after the test