#include "MAX30100_PulseOximeter.
h"
#include <U8g2lib.h>
#include <Wire.h>
#define REPORTING_PERIOD_MS 500
U8G2_SSD1306_128X32_UNIVISION_F_HW_I2C u8g2(U8G2_R0);
byte window = 1; //sensor
PulseOximeter pox;
const int numReadings=10;
float filterweight=0.5;
uint32_t tsLastReport = 0;
uint32_t last_beat=0;
int readIndex=0; //create a variable integer called ‘readIndex’
int average_beat=0; // create a variable integer called ‘average beat’
bool calculation_complete=false; // starting condition for a complete calculation(holds true/false)
bool calculating=false;
bool initialized=false;
byte lowersetpoint; //minimum value per age bracket
byte uppersetpoint; //minimum value per age bracket
unsigned long previousmillis = 0; // previous time in second
unsigned long currentmillis; //current time in second
unsigned long interval = 1000; //count interval of 1 second
int buttonState; // the current reading from the input pin
int lastButtonState = LOW;
unsigned long lastDebounceTime = 0; // the last time the output pin was toggled
unsigned long debounceDelay = 50; // the debounce time; increase if the output flickers
// Callback (registered below) fired when a pulse is detected
void onBeatDetected() //declare a function when a beat is detected
{
show_beat();
last_beat=millis(); //in seconds
}
void show_beat() //declare a function to show the beat
{
u8g2.setFont(u8g2_font_cursor_tf); //assign a font
u8g2.setCursor(8,10); //where to print
if (beat==0) {
u8g2.print("_");
beat=1;
}
else
{
u8g2.print("^");
beat=0;
}
u8g2.sendBuffer(); //send to arduino
}
void initial_display() // declare function for display the measuring
{
if (not initialized)
{
u8g2.clearBuffer();
show_beat();
u8g2.setCursor(24,12);
u8g2.setFont(u8g2_font_smart_patrol_nbp_tf); //set function
u8g2.print("Place finger");
u8g2.setCursor(0,30); //location
u8g2.print("on the sensor");
u8g2.sendBuffer(); //send to arduino
initialized=true; //the sensor has been initialized
}
}
void display_calculating(int j) //declare function to display while calculating
{
if (not calculating) {
u8g2.clearBuffer();
calculating=true; //make calculating true
initialized=false; //make calculating false to start measuring
}
show_beat(); //callback show-beat
u8g2.setCursor(24,12); //location
u8g2.setFont(u8g2_font_smart_patrol_nbp_tf); //set font
u8g2.print("Measuring");
u8g2.setCursor(0,30); //location
for (int i=0;i<=j;i++) { //increment the counting with
u8g2.print(". "); // ”.” as indicator
digitalWrite(6,HIGH); //Buzzer
}
u8g2.sendBuffer(); //send to arduino
}
void display_values() //declare function to display value
{
u8g2.clearBuffer();
u8g2.setFont(u8g2_font_smart_patrol_nbp_tf); //set font
u8g2.setCursor(30,12); //location
u8g2.print(average_beat); //call average-beat
u8g2.print(" Bpm");
if(average_beat > lowersetpoint && average_beat < uppersetpoint){ //condition to set the min & max values for
age bracket
u8g2.setFont(u8g2_font_smart_patrol_nbp_tf);
u8g2.setCursor(30,30); //location
u8g2.print("NORMAL");
u8g2.sendBuffer(); //send to arduino
}
if(average_beat > uppersetpoint || average_beat < lowersetpoint){ //condition to set the min&max values for age
bracket
u8g2.setFont(u8g2_font_smart_patrol_nbp_tf);
u8g2.setCursor(25,30); //location
u8g2.print("ABNORMAL");
u8g2.sendBuffer(); //send to Arduino
}
void calculate_average(int beat) //declare function to calculate BPM
{
if (readIndex==numReadings) { // wait until the read Index is equivalent to numReadings or 10
calculation_complete=true;
calculating=false;
initialized=false;
readIndex=0;
display_values();
}
if (not calculation_complete and beat>30 and beat<220 {
average_beat = filterweight * (beat) + (1 - filterweight ) * average_beat;
readIndex++; //read Index will increment
display_calculating(readIndex);
}
}
void menu(){ //declare MENU
u8g2.setFont(u8g2_font_smart_patrol_nbp_tf);
if(window == 1){ //first age bracket 0-1
lowersetpoint = 100;
uppersetpoint = 160;
u8g2.clearBuffer();
u8g2.setCursor(25,10);
u8g2.print("AGE: 0 - 1?");
}
if(window == 2){ //2nd bracket 1-10 yrs. old
lowersetpoint = 70;
uppersetpoint = 120;
u8g2.clearBuffer();
u8g2.setCursor(25,10);
u8g2.print("AGE: 1 - 10?");
}
if(window == 3){ //3rd age bracket 10 yrs. Old and above
lowersetpoint = 60;
uppersetpoint = 100;
u8g2.clearBuffer();
u8g2.setCursor(25,10);
u8g2.print("10 & above?");
}
if(window == 4){ //Yes button has been click
pox.setOnBeatDetectedCallback(onBeatDetected);
if ((millis() - tsLastReport > REPORTING_PERIOD_MS) and (not calculation_complete)) {
calculate_average(pox.getHeartRate(),pox.getSpO2());
tsLastReport = millis();
}
if ((millis()-last_beat>10000)) {
calculation_complete=false;
average_beat=0;
average_SpO2=0;
}
if(window < 4){ // if Yes button has not been clicked
u8g2.setCursor(15,30);
u8g2.print("YES");
u8g2.setCursor(75,30);
u8g2.print("NO");
}
if(digitalRead(8) != 1){ // Yes Button
calculation_complete=false;
calculating=false;
initialized=false;
beat=0;
setup();
window = 4;
}
int reading = !digitalRead(7); //NO button
if (reading != lastButtonState) {
// reset the debouncing timer
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
// whatever the reading is at, it's been there for longer than the debounce
// delay, so take it as the actual current state:
// if the button state has changed:
if (reading != buttonState) {
buttonState = reading;
if (buttonState == HIGH) {
window++; //will increment the window
if(window >= 4){
window = 1;
}
}
}
}
lastButtonState = reading;
u8g2.sendBuffer(); //send to arduino
void setup()
{
Serial.begin(115200);
u8g2.begin(); //initialized the OLED library
pox.begin(); //initialized the sensor Max30100 library
initial_display();
pinMode(7, INPUT_PULLUP); //NO button
pinMode(8, INPUT_PULLUP); //Yes Button
pinMode(6,OUTPUT); //BUZZER and LED
void loop()
{
// Make sure to call update as fast as possible
pox.update();
menu();
}