7
7
HEIGHT = 64
8
8
9
9
i2c = I2C (0 , scl = Pin (17 ), sda = Pin (16 ), freq = 200000 )
10
- display = SSD1306_I2C (128 , 64 , i2c )
10
+ display = SSD1306_I2C (WIDTH , HEIGHT , i2c )
11
11
display .fill (0 )
12
12
13
13
adc = ADC (28 )
14
+ history = [] # List to store ADC readings
15
+ peak_detected = False
16
+ peak_count = 0
17
+ last_peak_time = utime .ticks_ms ()
18
+ bpm = 0
14
19
while True :
15
- adc_reading = round (adc .read_u16 ())
20
+ adc_reading = round (adc .read_u16 () / 64 )
16
21
print (adc_reading )
17
- display .text (str (adc_reading ),0 ,14 )
18
- display .show ()
22
+ # Add the ADC reading to the history
23
+ history .append (adc_reading )
24
+
25
+ # Keep the history length within the width of the display
26
+ max_history_length = WIDTH // 2
27
+
28
+ if len (history ) > max_history_length :
29
+ history .pop (0 )
30
+
31
+ # Calculate the scaling factors for the graph
32
+ max_value = max (history , default = 0 )
33
+ scale_x = WIDTH / (max_history_length - 1 )
34
+ scale_y = (HEIGHT - 50 ) / max_value # Adjusted for y = 35
35
+
19
36
display .fill (0 )
37
+ display .text ("ECG:" , 10 , 0 )
38
+
39
+ # Draw the line graph
40
+ prev_x , prev_y = None , None
41
+ for i , value in enumerate (history ):
42
+ x = int (i * scale_x )
43
+ x2 = int ((i + 1 ) * scale_x ) if i < len (history ) - 1 else int (i * scale_x )
44
+ y = HEIGHT - 35 - int (value * scale_y ) # Adjusted for y = 35
45
+
46
+ if prev_x is not None and prev_y is not None :
47
+ display .line (prev_x , prev_y , x2 , y , 1 )
48
+
49
+ prev_x , prev_y = x , y
50
+
51
+ display .show ()
52
+ display .text ("BPM:" , 0 , 55 )
53
+ display .text (str (bpm ), 35 , 55 )
54
+ display .show ()
55
+ # Check for peaks in the ECG signal
56
+ if len (history ) > 2 and history [- 2 ] < history [- 1 ] > history [- 3 ]:
57
+ if not peak_detected :
58
+ peak_detected = True
59
+ peak_count += 1
60
+ current_time = utime .ticks_ms ()
61
+ elapsed_time = utime .ticks_diff (current_time , last_peak_time )
62
+ last_peak_time = current_time
63
+ bpm = int (60000 / elapsed_time )
64
+
65
+ # Adjust BPM to be within the desired range
66
+ bpm = max (60 , min (bpm , 95 ))
67
+ print (f"BPM: { bpm } " )
68
+
20
69
utime .sleep (0.04 )
0 commit comments