I need to apply IF statement in Widget LED

hello,
I have a problem to apply in led to off and on
I have max30100 sensor and make blynk app to show spo2 and heart rate and then make if conditional, my idea is

  • IF spo2 and hr in normal range led will stell off
  • else le will on

It’s worked but just when led on the value stopped like freezing
must i turn off esp32 to work again
I search on YouTube and google for solution put i didn’t find
pleas help me just explain the mistake :pensive:

this is prat of my code to show you my if conditional

if(HRclean>60 && HRclean<90 && SpO2>95 && SpO2<100)
 {  
led.on();
}
else if (HRclean>=90 && SpO2<95)
   {
led.off();
}     

Well, you have an if with one set of criteria, and an else if with a different set of criteria.

What if neither of those criteria are met?

I’d start by adding-in some serial print commands before the if statement that shows what each of the values are, then a serial print statement inside your if and else if statements so you know when they have evaluated as true.

Pete.

Try changing to

if((HRclean<=90 && HRclean>=60) && (SpO2<=100 && SpO2>=95))

and the below as well

else if (SpO2<=95 && HRclean>=90)

hi pete
I do that and I know the value and led worked put problem is when (led on) the value stopped to receive from max30100 sensor because this I must turn off esp32 and turn on again to calculate another patient
I don’t know if my explanation is good :slightly_frowning_face:

this all code

#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[] ="        ";             // You should get Auth Token in the Blynk App.
char ssid[]  ="         ";                                     // Your WiFi credentials.
char pass[] ="        ";
WidgetLED led(V1); 
BlynkTimer timer;
U8G2_SSD1306_128X32_UNIVISION_F_HW_I2C u8g2(U8G2_R0);
PulseOximeter pox;
MAX30100 sensor;

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(112500);
   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();
  timer.run();
  pox.update();
  HRclean = pox.getHeartRate();
   SpO2 = pox.getSpO2();  
  tsLastReport = millis();      
      Blynk.virtualWrite(V5, HRclean);
      Blynk.virtualWrite(V4, SpO2);   
  

  // }
 if((HRclean<90 && HRclean>=60) && (SpO2<100 && SpO2>=95))
{       
  u8g2.clearBuffer();
  u8g2.setFont(u8g2_font_7x13B_tf);
  u8g2.setCursor(0,12);  
  u8g2.print("HR:");
  u8g2.setCursor(45,12);  
  u8g2.print("Bpm");
  u8g2.setCursor(0,30);
  u8g2.print("SpO2:");
  u8g2.setCursor(57,30);
  u8g2.print("%"); 
  u8g2.setFont( u8g2_font_7x13B_tf ); 
  u8g2.setCursor(25,12);  
  u8g2.print(HRclean);
  u8g2.setCursor(40,30);  
  u8g2.print(SpO2);
  u8g2.setFont( u8g2_font_7x13B_tf ); 
  u8g2.setCursor(70,30);  
  u8g2.print("Negative");
  u8g2.sendBuffer();
led.off();
}   
else if (SpO2<95 && HRclean>=90)   {

  u8g2.clearBuffer();
  u8g2.setFont(u8g2_font_7x13B_tf);
  u8g2.setCursor(0,12);  
  u8g2.print("HR:");
  u8g2.setCursor(45,12);  
  u8g2.print("Bpm");
  u8g2.setCursor(0,30);
  u8g2.print("SpO2:");
  u8g2.setCursor(57,30);
  u8g2.print("%"); 
  u8g2.setFont( u8g2_font_7x13B_tf ); 
  u8g2.setCursor(25,12);  
  u8g2.print(HRclean);
  u8g2.setCursor(40,30);  
  u8g2.print(SpO2);
  u8g2.setFont( u8g2_font_7x13B_tf ); 
  u8g2.setCursor(70,30);  
  u8g2.print("Positive");
   u8g2.sendBuffer();
led.on();
 }
   else (HRclean<60 && SpO2>=100)
   {
led.off(); 
   }
}

Please edit your post and add triple backticks ( ``` ) before and after your sketch.

AmsAlert thank you for answer but still same problem :slightly_frowning_face:

done thanks

You must read this first
https://docs.blynk.io/en/legacy-platform/legacy-articles/keep-your-void-loop-clean

I recommend you:

1- keep your void loop clean, just

void loop()
{
  Blynk.run();
  timer.run();
}

Nothing else.

2- avoid using delay with blynk and use blynk timer instead, check this very useful topic

3- keep your sketch as simple as possible.

Just some small house keeping: change to (HRclean<=60 && SpO2>=100)

else statement is the if/else trap, this will execute if all other conditions are not met, so change to

else
   {
       led.off(); 
   }

I see that you are updating 2 Virtual PIN’s, but not the LED PIN, I may have missed it, but check on you side that you are doing the same

Blynk.virtualWrite(V1,1);    // LED On
Blynk.virtualWrite(V1, 0);   // LED Off