Max30100 if condition

I’ve code for MAX30100 with esp32, how can I put an if condition to both the HR and SpO2 values to turn on the LED widget in BLYNK without disturbing the normal performance of the sensor.

#include <Wire.h>
#include "MAX30100_PulseOximeter.h"
#include "MAX30100.h"
#include <U8g2lib.h>
#define BLYNK_PRINT Serial
#include <Blynk.h>
#include <WiFi.h>
#include <BlynkSimpleEsp32.h>
#define REPORTING_PERIOD_MS      1000
#define PULSE_WIDTH              MAX30100_SPC_PW_1600US_16BITS
#define IR_LED_CURRENT           MAX30100_LED_CURR_40MA  
#define LED_CURRENT              MAX30100_LED_CURR_20_8MA 
#define SAMPLING_RATE            MAX30100_SAMPRATE_100HZ
char auth[] = "Elas4wpj_P9t5rymrWnFQiKay2qiDxCw";             // You should get Auth Token in the Blynk App.
char ssid[] = "HiLink";                                     // Your WiFi credentials.
char pass[] = "kemei9911";
U8G2_SSD1306_128X32_UNIVISION_F_HW_I2C u8g2(U8G2_R0);
PulseOximeter pox;
MAX30100 sensor;
WidgetLED led1(V1);

uint32_t tsLastReport = 0;
uint32_t last_beat=0;
bool initialized=false;
int HRclean;
int SpO2;
 
void onBeatDetected()
{
  show_beat();
  last_beat=millis();
}

void show_beat() 
{
  u8g2.setFont(u8g2_font_7x13B_tf);
  u8g2.setCursor(118,10);
  u8g2.print("_");
  u8g2.sendBuffer();
}

void initial_display() 
{
  if (not initialized) 
  {
    u8g2.clearBuffer();
    u8g2.setFont(u8g2_font_7x13B_tf);
    u8g2.setCursor(18,10);
    u8g2.print("Initializing...");
    u8g2.sendBuffer(); 
    delay(3000);
   
    initialized=true;

      u8g2.clearBuffer();
      u8g2.setFont(u8g2_font_7x13B_tf); 
        if (!pox.begin()) {
          u8g2.setCursor(40,12);
          u8g2.print("FAILED");
          u8g2.setCursor(15,29);
          u8g2.print("Check Sensor !");
          u8g2.sendBuffer(); 
          for(;;);
        } else {
          u8g2.setCursor(20,12);
          u8g2.print("INITIALIZED");
          u8g2.setCursor(0,29);
          u8g2.print("Put your finger...");
          u8g2.sendBuffer(); 
        }
     delay(2000);
  }
}


void setup()
{
    u8g2.begin();
        Serial.begin(115200);
            Blynk.begin(auth, ssid, pass);
    initial_display();
    pox.begin();

    pox.setOnBeatDetectedCallback(onBeatDetected); 
    pox.setIRLedCurrent(LED_CURRENT);  
    sensor.setMode(MAX30100_MODE_SPO2_HR);
    sensor.setLedsPulseWidth(PULSE_WIDTH);
    sensor.setSamplingRate(SAMPLING_RATE);
}
 
void loop()
{
     Blynk.run();
    pox.update();
    HRclean = pox.getHeartRate();
    SpO2 = pox.getSpO2();
    if ((millis() - tsLastReport > REPORTING_PERIOD_MS) and (HRclean>30 and HRclean<220 and SpO2>30 and SpO2<100)) {
      u8g2.clearBuffer();
      u8g2.setFont(u8g2_font_7x13B_tf);
      u8g2.setCursor(0,12);  
      u8g2.print("HR");
      u8g2.setCursor(70,12);  
      u8g2.print("Bpm");
      u8g2.setCursor(0,30);
      u8g2.print("SpO2 ");
      u8g2.setCursor(70,30);
      u8g2.print("%"); 
      u8g2.setFont( u8g2_font_7x13B_tf ); 
      u8g2.setCursor(45,12);  
      u8g2.print(HRclean);
      u8g2.setCursor(45,30);  
      u8g2.print(SpO2);
      u8g2.sendBuffer();
        Blynk.virtualWrite(V5, HRclean);
        Blynk.virtualWrite(V4, SpO2);
     tsLastReport = millis();      
    }

}

Hey there,
first of all you have to read this
https://docs.blynk.io/en/legacy-platform/legacy-articles/keep-your-void-loop-clean

I think you should also re-read this…

Pete.

1 Like