0% found this document useful (0 votes)
15 views5 pages

MAX30100 Pulse Oximeter Code Example

This Arduino code is measuring heart rate using a pulse oximeter sensor. It initializes the sensor and OLED display libraries. It defines several functions for displaying text, calculating the average heart rate over readings, and setting up a menu interface on the OLED. The menu allows the user to select an age bracket, which sets the normal heart rate ranges. It then measures and displays the heart rate, indicating if it is normal or abnormal based on the selected range.

Uploaded by

Elyen Mage
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views5 pages

MAX30100 Pulse Oximeter Code Example

This Arduino code is measuring heart rate using a pulse oximeter sensor. It initializes the sensor and OLED display libraries. It defines several functions for displaying text, calculating the average heart rate over readings, and setting up a menu interface on the OLED. The menu allows the user to select an age bracket, which sets the normal heart rate ranges. It then measures and displays the heart rate, indicating if it is normal or abnormal based on the selected range.

Uploaded by

Elyen Mage
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

#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


{
[Link](u8g2_font_cursor_tf); //assign a font
[Link](8,10); //where to print
if (beat==0) {
[Link]("_");
beat=1;
}
else
{
[Link]("^");
beat=0;
}
[Link](); //send to arduino
}

void initial_display() // declare function for display the measuring


{
if (not initialized)
{
[Link]();
show_beat();
[Link](24,12);
[Link](u8g2_font_smart_patrol_nbp_tf); //set function
[Link]("Place finger");
[Link](0,30); //location
[Link]("on the sensor");
[Link](); //send to arduino
initialized=true; //the sensor has been initialized
}
}

void display_calculating(int j) //declare function to display while calculating


{
if (not calculating) {
[Link]();
calculating=true; //make calculating true
initialized=false; //make calculating false to start measuring
}
show_beat(); //callback show-beat
[Link](24,12); //location
[Link](u8g2_font_smart_patrol_nbp_tf); //set font
[Link]("Measuring");
[Link](0,30); //location
for (int i=0;i<=j;i++) { //increment the counting with
[Link](". "); // ”.” as indicator
digitalWrite(6,HIGH); //Buzzer

}
[Link](); //send to arduino
}
void display_values() //declare function to display value
{
[Link]();
[Link](u8g2_font_smart_patrol_nbp_tf); //set font
[Link](30,12); //location
[Link](average_beat); //call average-beat
[Link](" Bpm");

if(average_beat > lowersetpoint && average_beat < uppersetpoint){ //condition to set the min & max values for
age bracket

[Link](u8g2_font_smart_patrol_nbp_tf);
[Link](30,30); //location
[Link]("NORMAL");
[Link](); //send to arduino
}

if(average_beat > uppersetpoint || average_beat < lowersetpoint){ //condition to set the min&max values for age
bracket
[Link](u8g2_font_smart_patrol_nbp_tf);
[Link](25,30); //location
[Link]("ABNORMAL");
[Link](); //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

[Link](u8g2_font_smart_patrol_nbp_tf);

if(window == 1){ //first age bracket 0-1


lowersetpoint = 100;
uppersetpoint = 160;
[Link]();
[Link](25,10);
[Link]("AGE: 0 - 1?");
}
if(window == 2){ //2nd bracket 1-10 yrs. old
lowersetpoint = 70;
uppersetpoint = 120;
[Link]();
[Link](25,10);
[Link]("AGE: 1 - 10?");
}
if(window == 3){ //3rd age bracket 10 yrs. Old and above
lowersetpoint = 60;
uppersetpoint = 100;
[Link]();
[Link](25,10);
[Link]("10 & above?");
}

if(window == 4){ //Yes button has been click


[Link](onBeatDetected);
if ((millis() - tsLastReport > REPORTING_PERIOD_MS) and (not calculation_complete)) {
calculate_average([Link](),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


[Link](15,30);
[Link]("YES");

[Link](75,30);
[Link]("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;

[Link](); //send to arduino

void setup()
{
[Link](115200);
[Link](); //initialized the OLED library
[Link](); //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
[Link]();
menu();
}

You might also like