-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHeartBeatSensor.py
More file actions
56 lines (43 loc) · 1.63 KB
/
HeartBeatSensor.py
File metadata and controls
56 lines (43 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
from machine import Pin
import time
#Onboard LED and sensor pin setup with an in-built pull down resistor for stablity of the signal
sensorPin = Pin(15, Pin.IN, Pin.PULL_DOWN)
led = Pin(25, Pin.OUT)
#Pulse counters
pulseCount = 0
lastPulseTime = 0
#The function to count pulses
def pulseDetected(pin):
global pulseCount, lastPulseTime
#The current time in miliseconds
currentTime = time.ticks_ms()
#Buffer avoid multiple counts for the same pulse
if time.ticks_diff(currentTime, lastPulseTime) > 100:
pulseCount += 1
lastPulseTime = currentTime
#The interupt function
sensorPin.irq(trigger=Pin.IRQ_RISING, handler=pulseDetected)
#Measuring the pulses' and printing the count on the serial monitor
try:
while True:
led.value(1)
print("Measuring heart rate for 10 seconds...")
#Measure heart rate for 10 seconds, and print the heart rate
for second in range(1, 11):
startTime = time.ticks_ms()
startPulse = pulseCount
#Count pulses for 1 second
while time.ticks_diff(time.ticks_ms(), startTime) < 1000:
time.sleep(0.01)
#Calculate and display the heart rate for each second
beats = pulseCount - startPulse
heartRate = beats * 60
print(f"Second {second}: {heartRate} BPM")
led.value(0)
print("Measurement complete. Waiting before next session...\n")
#Five seconds cooldown period.
time.sleep(5)
#Error handling
except KeyboardInterrupt:
print('Stopped.')
led.value(0)