1
7
8#include <Adafruit_GFX.h>
9#include <Adafruit_SSD1306.h>
10#include <Wire.h>
11#include "MAX30105.h"
12#include "heartRate.h"
13
14MAX30105 particleSensor;
15
16const byte RATE_SIZE = 4;
17byte rates[RATE_SIZE];
18byte rateSpot = 0;
19long lastBeat = 0;
20float beatsPerMinute;
21int beatAvg;
22
23#define SCREEN_WIDTH 128
24#define SCREEN_HEIGHT 32
25#define OLED_RESET -1
26
27Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
28
29static const unsigned char PROGMEM logo2_bmp[] =
30{ 0x03, 0xC0, 0xF0, 0x06, 0x71, 0x8C, 0x0C, 0x1B, 0x06, 0x18, 0x0E, 0x02, 0x10, 0x0C, 0x03, 0x10,
310x04, 0x01, 0x10, 0x04, 0x01, 0x10, 0x40, 0x01, 0x10, 0x40, 0x01, 0x10, 0xC0, 0x03, 0x08, 0x88,
320x02, 0x08, 0xB8, 0x04, 0xFF, 0x37, 0x08, 0x01, 0x30, 0x18, 0x01, 0x90, 0x30, 0x00, 0xC0, 0x60,
330x00, 0x60, 0xC0, 0x00, 0x31, 0x80, 0x00, 0x1B, 0x00, 0x00, 0x0E, 0x00, 0x00, 0x04, 0x00, };
34
35static const unsigned char PROGMEM logo3_bmp[] =
36{ 0x01, 0xF0, 0x0F, 0x80, 0x06, 0x1C, 0x38, 0x60, 0x18, 0x06, 0x60, 0x18, 0x10, 0x01, 0x80, 0x08,
370x20, 0x01, 0x80, 0x04, 0x40, 0x00, 0x00, 0x02, 0x40, 0x00, 0x00, 0x02, 0xC0, 0x00, 0x08, 0x03,
380x80, 0x00, 0x08, 0x01, 0x80, 0x00, 0x18, 0x01, 0x80, 0x00, 0x1C, 0x01, 0x80, 0x00, 0x14, 0x00,
390x80, 0x00, 0x14, 0x00, 0x80, 0x00, 0x14, 0x00, 0x40, 0x10, 0x12, 0x00, 0x40, 0x10, 0x12, 0x00,
400x7E, 0x1F, 0x23, 0xFE, 0x03, 0x31, 0xA0, 0x04, 0x01, 0xA0, 0xA0, 0x0C, 0x00, 0xA0, 0xA0, 0x08,
410x00, 0x60, 0xE0, 0x10, 0x00, 0x20, 0x60, 0x20, 0x06, 0x00, 0x40, 0x60, 0x03, 0x00, 0x40, 0xC0,
420x01, 0x80, 0x01, 0x80, 0x00, 0xC0, 0x03, 0x00, 0x00, 0x60, 0x06, 0x00, 0x00, 0x30, 0x0C, 0x00,
430x00, 0x08, 0x10, 0x00, 0x00, 0x06, 0x60, 0x00, 0x00, 0x03, 0xC0, 0x00, 0x00, 0x01, 0x80, 0x00 };
44
45
46void setup() {
47 display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
48 display.display();
49 delay(3000);
50
51 particleSensor.begin(Wire, I2C_SPEED_FAST);
52 particleSensor.setup();
53 particleSensor.setPulseAmplitudeRed(0x0A);
54
55}
56
57void loop() {
58 long irValue = particleSensor.getIR();
59
60if(irValue > 7000){
61 display.clearDisplay();
62 display.drawBitmap(5, 5, logo2_bmp, 24, 21, WHITE);
63 display.setTextSize(2);
64 display.setTextColor(WHITE);
65 display.setCursor(50,0);
66 display.println("BPM");
67 display.setCursor(50,18);
68 display.println(beatAvg);
69 display.display();
70
71 if (checkForBeat(irValue) == true)
72 {
73 display.clearDisplay();
74 display.drawBitmap(0, 0, logo3_bmp, 32, 32, WHITE);
75 display.setTextSize(2);
76 display.setTextColor(WHITE);
77 display.setCursor(50,0);
78 display.println("BPM");
79 display.setCursor(50,18);
80 display.println(beatAvg);
81 display.display();
82 tone(3,1000);
83 delay(100);
84 noTone(3);
85
86 long delta = millis() - lastBeat;
87 lastBeat = millis();
88
89 beatsPerMinute = 60 / (delta / 1000.0);
90
91 if (beatsPerMinute < 255 && beatsPerMinute > 20)
92 {
93 rates[rateSpot++] = (byte)beatsPerMinute;
94 rateSpot %= RATE_SIZE;
95
96
97 beatAvg = 0;
98 for (byte x = 0 ; x < RATE_SIZE ; x++)
99 beatAvg += rates[x];
100 beatAvg /= RATE_SIZE;
101 }
102 }
103
104}
105 if (irValue < 7000){
106 beatAvg=0;
107 display.clearDisplay();
108 display.setTextSize(1);
109 display.setTextColor(WHITE);
110 display.setCursor(30,5);
111 display.println("Please Place ");
112 display.setCursor(30,15);
113 display.println("your finger ");
114 display.display();
115 noTone(3);
116 }
117
118}
119