-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
69 lines (56 loc) · 1.98 KB
/
Copy pathmain.py
File metadata and controls
69 lines (56 loc) · 1.98 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
57
58
59
60
61
62
63
64
65
66
67
68
69
from machine import Pin, I2C
import utime
from machine import ADC
from ssd1306 import SSD1306_I2C
WIDTH = 128
HEIGHT = 64
i2c = I2C(0, scl = Pin(17), sda = Pin(16), freq=200000)
display = SSD1306_I2C(WIDTH, HEIGHT, i2c)
display.fill(0)
adc = ADC(28)
history = [] # List to store ADC readings
peak_detected = False
peak_count = 0
last_peak_time = utime.ticks_ms()
bpm=0
while True:
adc_reading = round(adc.read_u16() / 64)
print(adc_reading)
# Add the ADC reading to the history
history.append(adc_reading)
# Keep the history length within the width of the display
max_history_length = WIDTH // 2
if len(history) > max_history_length:
history.pop(0)
# Calculate the scaling factors for the graph
max_value = max(history, default=0)
scale_x = WIDTH / (max_history_length - 1)
scale_y = (HEIGHT - 50) / max_value # Adjusted for y = 35
display.fill(0)
display.text("ECG:", 10, 0)
# Draw the line graph
prev_x, prev_y = None, None
for i, value in enumerate(history):
x = int(i * scale_x)
x2 = int((i + 1) * scale_x) if i < len(history) - 1 else int(i * scale_x)
y = HEIGHT - 35 - int(value * scale_y) # Adjusted for y = 35
if prev_x is not None and prev_y is not None:
display.line(prev_x, prev_y, x2, y, 1)
prev_x, prev_y = x, y
display.show()
display.text("BPM:", 0, 55)
display.text(str(bpm), 35, 55)
display.show()
# Check for peaks in the ECG signal
if len(history) > 2 and history[-2] < history[-1] > history[-3]:
if not peak_detected:
peak_detected = True
peak_count += 1
current_time = utime.ticks_ms()
elapsed_time = utime.ticks_diff(current_time, last_peak_time)
last_peak_time = current_time
bpm = int(60000 / elapsed_time)
# Adjust BPM to be within the desired range
bpm = max(60, min(bpm, 95))
print(f"BPM: {bpm}")
utime.sleep(0.04)