# Project#8 - Let's measure our heart rate # Connections: No additional connections need to be made if you're coming here directly from project#7 with one small exception" # Connect the heart rate sensor to the "HEART" jack BEFORE powering up the Pico. Plugging this sensor in while powered will cause a reset. # In this project, we'll measure our heart rate and average it. import machine # Think of "import" as importing a library. This library allows for us to use GPIOs import utime # This "library" allows for us to tell time if connected to the internet, and use delays like 'utime.sleep(1)' #The below declarations are new since project#7 HeartSensor = machine.ADC(28) # The heart sensor connect internally to GPIO#28 which is an ADC pin. # ASTOP = machine.Pin(6, machine.Pin.IN) #Setup the ASTOP (AUDIO STOP) pint o GPIO 6. Set it as an input. cs = machine.Pin(5, machine.Pin.OUT) #Setup the CS (Chip select pin for the audio chip. GPIO5. Set as output. cs.value(1) # Set the CS line to 1 (HIGH/3.3v) spi_sck=machine.Pin(2) # Set SPI clock to GPIO pin 2 spi_tx=machine.Pin(3) # Set SPI MASTER OUT to GPIO pin 3 spi_rx=machine.Pin(4) # Set SPI MASTER IN to GPIO pin 4 spi=machine.SPI(0,baudrate=500000,sck=spi_sck,mosi=spi_tx, phase=1, polarity=0) # Setup SPI communications settings utime.sleep(1) # Sleep/Wait for one second hold = '\x00' # 'hold' acts as the pointer for the little buddy talker chip. Load the HEX value of the audio bite that you want to play, then call the playaudio() function. # LightSensor = machine.ADC(26) # Setup GPIO26 to act as the ADC input for the light sensor. Buzzer = machine.Pin(22, machine.Pin.OUT) #Setup the Buzzer, which is connected to GPIO 22. Declare it as an output. Microphone = machine.ADC(27) # Setup GPIO27 to act as the ADC input for the microphone. This is the AD1 line Button = machine.Pin(15, machine.Pin.IN) #Setup the SEL1 button pin to GPIO 15. Set it as an input and name it "Button" Button2 = machine.Pin(14, machine.Pin.IN) #Setup the SEL2 button pin to GPIO 14. Set it as an input and name it "Button2" DIPA = machine.Pin(21, machine.Pin.IN) #Setup DIP Switch Channel-A as an output DIPB = machine.Pin(20, machine.Pin.IN) #Setup DIP Switch Channel-B as an output DIPC = machine.Pin(19, machine.Pin.IN) #Setup DIP Switch Channel-C as an output DIPD = machine.Pin(18, machine.Pin.IN) #Setup DIP Switch Channel-D as an output LEDRED = machine.Pin(16, machine.Pin.OUT) # Set up GPIO16 as an output, and name it LEDRED LEDBLU = machine.Pin(17, machine.Pin.OUT) # Do the same for GPIO17 LEDRED.value(0) # Turn the red LED off. If you set it to (1), then you'd be turning it on LEDBLU.value(0) # Turn the blue LED off. If you set it to (1), then you'd be turning it on state = 0 # state is a variable/storage register. Set it to a value of 0. hold = 0 # hold is another variable that we'll use to store data in hearthold = 0 counter = 0 conversion = 3.3/(65535) # Conversion helps us to turn ADC readings (Numeral values) into a voltage representation. # 3.3v is the full scale voltage. 0-65565 is the full 16-bit scale value used for ADC conversions. 0000000000000000-1111111111111111 def heartbeataverage(): # This function will average your heart rate and offer a visual representation on the red LED. print("Average your Heartrate. Press Button#1/SW1 to end this sequence") # Print this to the shell state = 0 # Introduce variables state, timer, counter, and hearthold to 0. timer = 0 counter = 0 hearthold = 0 LEDRED.value(0) # Turn the red LED off while state == 0: # Loop the following while state equals 0. We're waiting here for a low voltage. No beat. # This first while loop waits for a "High/heart beat" to end so that we can start fresh with a new beat. hold = HeartSensor.read_u16() # Sample the heart sensor and take an ADC reading. Place the result into hold. hold = hold*conversion # Convert the ADC value into a voltage representation. See previous projects for more info if hold < 1.5: # If the heart sensor voltage is less than 1.5v, then do the following: utime.sleep(0.01) # Sleep for 10ms state = 1 # Set state to 1 to end the while loop, as we've recevived a heart beat # With the above while loop being done, we can now start timing our heart beats while Button.value() == True: # If you press button#1, it will end this sequence quickly and nullify the result. state = 0 # Set state to 0 LEDRED.value(1) # Turn the LED on while state == 0: # Do the following while state equals 0 timer = timer + 1 # Add one to timer. Increment timer by 1. This timer times the frequency of our heart rate. utime.sleep(0.001) # Wait 1ms hold = HeartSensor.read_u16() # Sample the heart rate sensor ADC and place the result into hold hold = hold*conversion # Convert to voltage if hold < 1.5: # If no beat is detected, set state to 1 state = 1 # See above LEDRED.value(0) # Turn the LED off while state == 1: # Loop the following while state is 1 timer = timer + 1 # Add one ti timer utime.sleep(0.001) # Wait 1ms hold = HeartSensor.read_u16() # Sample the heart sensor, place in hold, convert to voltage. hold = hold*conversion # See above if hold > 1.5: # If a heart beat is detected, set state back to 0 state = 0 # State now equals 0 counter = counter + 1 # Add one to counter timer = 60000 / timer # change the value in timer by dividing itself by 60000ms (60 seconds) hearthold = hearthold + timer # Add the new timer value to hearthold if counter == 10: # If counter equals ten, then we've taken ten heart beat samples hearthold = hearthold / 10 # If so, divide the value in hearthold by 10 and we'll get our averaged heartbeat. if hearthold < 200: # Only print to the shell if the value is realistic. 200BPM isn't realistic. If is a false reading. print("Heartrate Average:",hearthold,"BPM") # Print this to the shell else: print("Invalid Measurement...") # If the value was higher than 200, print this to the shell counter = 0 # Reset all variables and start again. timer = 0 hearthold = 0 timer = 0 timer = 0 print("Done...") # Print the following to the shell. while True: # This is the main loop. Call the heartbeataverage function over and over. That's all. heartbeataverage()