0

in this project im trying to create a 3 conditions for my max30100 sensors, here the 3 conditions: When spo2 is greater than or equal to 94, green led is on, red led is off When spo2 is less than 94 but greater than 80, green led is off, red led is on Else,both led is off

My main problem is the first condition and second condition may be correct but when the value below 80, the red led is on instead off Is there any problem with my code

#include <LiquidCrystal_I2C.h> 
#include "MAX30100_PulseOximeter.h"

LiquidCrystal_I2C lcd (0x27, 16,2); 
 
#define REPORTING_PERIOD_MS     1000


PulseOximeter pox;
uint32_t tsLastReport = 0;
const uint8_t red_pin = 4;
const uint8_t green_pin = 5;

 
void onBeatDetected()
{
    Serial.println("Beat!");
}
 
void setup()  
{
    Serial.begin(115200);
    Serial.print("Initializing pulse oximeter..");
    pinMode(red_pin, OUTPUT);
    pinMode(green_pin, OUTPUT);
    lcd.init ();
    lcd. backlight ();
    lcd.print("Initializing...");
    delay(3000);
    lcd.clear();
 

    if (!pox.begin()) {
       Serial.println("FAILED");
        for(;;);
    } else {
        Serial.println("SUCCESS");
   }

     pox.setIRLedCurrent(MAX30100_LED_CURR_7_6MA);
 
    // Register a callback for the beat detection
   pox.setOnBeatDetectedCallback(onBeatDetected);
}
 
void loop()
{
    // Make sure to call update as fast as possible
    pox.update();
    if (millis() - tsLastReport > REPORTING_PERIOD_MS) {

        Serial.print("Heart rate:");
        Serial.print(pox.getHeartRate());
        Serial.print("bpm / SpO2:");
        Serial.print(pox.getSpO2());
        Serial.println("%");
       
        lcd.clear();
        lcd.setCursor(0,0);
        lcd.print("BPM : ");
        lcd.print(pox.getHeartRate());
       
        lcd.setCursor(0,1);
        lcd.print("SpO2: ");
        lcd.print(pox.getSpO2());
        lcd.print("%");

   
        tsLastReport = millis();
        uint8_t spo2 = pox.getSpO2();
        if (94<=spo2) {
          digitalWrite(green_pin, HIGH);
          digitalWrite(red_pin, LOW);
          } 
        else if (80<spo2<94){
          digitalWrite(red_pin, HIGH);
          digitalWrite(green_pin, LOW);
          }
        else
          {
          digitalWrite(red_pin, LOW);
          digitalWrite(green_pin, LOW);
          }
        }
}
Gerhardh
  • 11,688
  • 4
  • 17
  • 39
Md fiz
  • 1

0 Answers0