Protocentral / protocentral-pulse-express

Arduino Library for the ProtoCentral Pulse Express breakout board with MAX30102 and MAX32664D, used for measurement of Spo2, heartrate and BP trending.
Other
14 stars 11 forks source link

Max30102 didn't give me the result I expected, does the problem with the code? #4

Open mamico131 opened 3 years ago

mamico131 commented 3 years ago

Hi there, I'm try max30102 with nodemcu everything is alright, except the readings I get are changing with a huge different it sometimes give a negative No. (-999) when SpO2 isn't valid, there is no other negative No. other than this, but I don't like that. I want it to give readings when its valid and waiting when it's not valid.

this is the code used:

`
// Include section
#include <Wire.h>
#include "MAX30105.h"
#include "spo2_algorithm.h"
#include "heartRate.h"

// Definitions
#define MAX_BRIGHTNESS 255

// Variables declaration
#if defined(__AVR_ATmega328P__) || defined(__AVR_ATmega168__)

uint16_t irBuffer[100]; //infrared LED sensor data
uint16_t redBuffer[100];  //red LED sensor data
#else
uint32_t irBuffer[100]; //infrared LED sensor data
uint32_t redBuffer[100];  //red LED sensor data
#endif

int32_t bufferLength; //data length
int32_t spo2; //SPO2 value
int8_t validSPO2; //indicator to show if the SPO2 calculation is valid
int32_t heartRate; //heart rate value
int8_t validHeartRate; //indicator to show if the heart rate calculation is valid

byte pulseLED = 11; //Must be on PWM pin
byte readLED = 13; //Blinks with each data read

// Heart rate vars

const byte RATE_SIZE = 4; //Increase this for more averaging. 4 is good.
byte rates[RATE_SIZE]; //Array of heart rates
byte rateSpot = 0;
long lastBeat = 0; //Time at which the last beat occurred

float beatsPerMinute;
int beatAvg;

// sampling times

unsigned long prevHRTime;
unsigned long prevSP02Time;

// Sensor object instantiation
MAX30105 particleSensor;

// Setup
void setup(){

// Initialize serial port and configure your sensor 

  Serial.begin(115200);
 Serial.println("Initializing...");

  // Initialize sensor
  if (!particleSensor.begin(Wire, I2C_SPEED_FAST)) 
  {
    Serial.println("MAX30105 was not found. Please check wiring/power. ");
    while (1);
  }
  Serial.println("Place your index finger on the sensor with steady pressure.");

  particleSensor.setup(); //Configure sensor with default settings
  particleSensor.setPulseAmplitudeRed(0x0A); //Turn Red LED to low to indicate sensor is running
  particleSensor.setPulseAmplitudeGreen(0); //Turn off Green LED

  prevHRTime = millis();
  prevSP02Time = millis();
}

// main loop

void loop(){

    unsigned long now = millis();

//    if(now - prevHRTime > 50){
//        prevHRTime  = now;
//
        heart_rate();    // Implementation is below
//
//    }

}

void heart_rate(){

 long irValue = particleSensor.getIR();

  if (checkForBeat(irValue) == true)
  {
    //We sensed a beat!
    long delta = millis() - lastBeat;
    lastBeat = millis();

    beatsPerMinute = 60 / (delta / 1000.0);
    Serial.print("true");

    // ------------------------------------------------------

      bufferLength = 100; //buffer length of 100 stores 4 seconds of samples running at 25sps

  //read the first 100 samples, and determine the signal range
  for (byte i = 0 ; i < bufferLength ; i++)
  {
    while (particleSensor.available() == false) //do we have new data?
      particleSensor.check(); //Check the sensor for new data

    redBuffer[i] = particleSensor.getRed();
    irBuffer[i] = particleSensor.getIR();
    particleSensor.nextSample(); //We're finished with this sample so move to next sample

  }

  //calculate heart rate and SpO2 after first 100 samples (first 4 seconds of samples)
  maxim_heart_rate_and_oxygen_saturation(irBuffer, bufferLength, redBuffer, &spo2, &validSPO2, &heartRate, &validHeartRate);

  //Continuously taking samples from MAX30102.  Heart rate and SpO2 are calculated every 1 second
  while (1)
  {
    //dumping the first 25 sets of samples in the memory and shift the last 75 sets of samples to the top
    for (byte i = 25; i < 100; i++)
    {
      redBuffer[i - 25] = redBuffer[i];
      irBuffer[i - 25] = irBuffer[i];
    }

    //take 25 sets of samples before calculating the heart rate.
    for (byte i = 75; i < 100; i++)
    {
      while (particleSensor.available() == false) //do we have new data?
        particleSensor.check(); //Check the sensor for new data

      digitalWrite(readLED, !digitalRead(readLED)); //Blink onboard LED with every data read

      redBuffer[i] = particleSensor.getRed();
      irBuffer[i] = particleSensor.getIR();
      particleSensor.nextSample(); //We're finished with this sample so move to next sample

      //send samples and calculation result to terminal program through UART

      Serial.print(", HR=");
      Serial.print(heartRate, DEC);

      Serial.print(", SPO2=");
      Serial.print(spo2, DEC);

      Serial.print(", SPO2Valid=");
      Serial.println(validSPO2, DEC);
    }

    //After gathering 25 new samples recalculate HR and SP02
    maxim_heart_rate_and_oxygen_saturation(irBuffer, bufferLength, redBuffer, &spo2, &validSPO2, &heartRate, &validHeartRate);
  }

    //--------------------------------------------------------

  Serial.print("IR=");
  Serial.print(irValue);
  Serial.print(", BPM=");
  Serial.print(beatsPerMinute);
//  Serial.println();
  }

  if (irValue < 50000)
  {
    beatsPerMinute = 0;
    beatAvg = 0;
    Serial.print(" No finger?");
    Serial.println();
  }

}

,

this is the result I got:

,
 No finger?
true, HR=88, SPO2=-999, SPO2Valid=0
, HR=88, SPO2=-999, SPO2Valid=0
, HR=88, SPO2=-999, SPO2Valid=0
, HR=88, SPO2=-999, SPO2Valid=0
, HR=88, SPO2=-999, SPO2Valid=0
, HR=88, SPO2=-999, SPO2Valid=0
, HR=88, SPO2=-999, SPO2Valid=0
, HR=88, SPO2=-999, SPO2Valid=0
, HR=88, SPO2=-999, SPO2Valid=0
, HR=88, SPO2=-999, SPO2Valid=0
, HR=88, SPO2=-999, SPO2Valid=0
, HR=88, SPO2=-999, SPO2Valid=0
, HR=88, SPO2=-999, SPO2Valid=0
, HR=88, SPO2=-999, SPO2Valid=0
, HR=88, SPO2=-999, SPO2Valid=0
, HR=88, SPO2=-999, SPO2Valid=0
, HR=88, SPO2=-999, SPO2Valid=0
, HR=88, SPO2=-999, SPO2Valid=0
, HR=88, SPO2=-999, SPO2Valid=0
, HR=88, SPO2=-999, SPO2Valid=0
, HR=88, SPO2=-999, SPO2Valid=0
, HR=88, SPO2=-999, SPO2Valid=0
, HR=88, SPO2=-999, SPO2Valid=0
, HR=88, SPO2=-999, SPO2Valid=0
, HR=88, SPO2=-999, SPO2Valid=0
, HR=53, SPO2=14, SPO2Valid=1
, HR=53, SPO2=14, SPO2Valid=1
, HR=53, SPO2=14, SPO2Valid=1
, HR=53, SPO2=14, SPO2Valid=1
, HR=53, SPO2=14, SPO2Valid=1
, HR=53, SPO2=14, SPO2Valid=1
, HR=53, SPO2=14, SPO2Valid=1
, HR=53, SPO2=14, SPO2Valid=1
, HR=53, SPO2=14, SPO2Valid=1
, HR=53, SPO2=14, SPO2Valid=1
, HR=53, SPO2=14, SPO2Valid=1
, HR=53, SPO2=14, SPO2Valid=1
, HR=53, SPO2=14, SPO2Valid=1
, HR=53, SPO2=14, SPO2Valid=1
, HR=53, SPO2=14, SPO2Valid=1
, HR=53, SPO2=14, SPO2Valid=1
, HR=53, SPO2=14, SPO2Valid=1
, HR=53, SPO2=14, SPO2Valid=1
, HR=53, SPO2=14, SPO2Valid=1
, HR=53, SPO2=14, SPO2Valid=1
, HR=53, SPO2=14, SPO2Valid=1
, HR=53, SPO2=14, SPO2Valid=1
, HR=53, SPO2=14, SPO2Valid=1
, HR=53, SPO2=14, SPO2Valid=1
, HR=53, SPO2=14, SPO2Valid=1
, HR=62, SPO2=14, SPO2Valid=1
, HR=62, SPO2=14, SPO2Valid=1
, HR=62, SPO2=14, SPO2Valid=1
, HR=62, SPO2=14, SPO2Valid=1
, HR=62, SPO2=14, SPO2Valid=1
, HR=62, SPO2=14, SPO2Valid=1
, HR=62, SPO2=14, SPO2Valid=1
, HR=62, SPO2=14, SPO2Valid=1
, HR=62, SPO2=14, SPO2Valid=1
, HR=62, SPO2=14, SPO2Valid=1
, HR=62, SPO2=14, SPO2Valid=1
, HR=62, SPO2=14, SPO2Valid=1
, HR=62, SPO2=14, SPO2Valid=1
, HR=62, SPO2=14, SPO2Valid=1
, HR=62, SPO2=14, SPO2Valid=1
, HR=62, SPO2=14, SPO2Valid=1
, HR=62, SPO2=14, SPO2Valid=1
, HR=62, SPO2=14, SPO2Valid=1
, HR=62, SPO2=14, SPO2Valid=1
, HR=62, SPO2=14, SPO2Valid=1
, HR=62, SPO2=14, SPO2Valid=1
, HR=62, SPO2=14, SPO2Valid=1
, HR=62, SPO2=14, SPO2Valid=1
, HR=62, SPO2=14, SPO2Valid=1
, HR=62, SPO2=14, SPO2Valid=1
, HR=-999, SPO2=-999, SPO2Valid=0
, HR=-999, SPO2=-999, SPO2Valid=0
, HR=-999, SPO2=-999, SPO2Valid=0
, HR=-999, SPO2=-999, SPO2Valid=0
, HR=-999, SPO2=-999, SPO2Valid=0
, HR=-999, SPO2=-999, SPO2Valid=0
, HR=-999, SPO2=-999, SPO2Valid=0
, HR=-999, SPO2=-999, SPO2Valid=0
, HR=-999, SPO2=-999, SPO2Valid=0
, HR=-999, SPO2=-999, SPO2Valid=0
, HR=-999, SPO2=-999, SPO2Valid=0
, HR=-999, SPO2=-999, SPO2Valid=0
, HR=-999, SPO2=-999, SPO2Valid=0
, HR=-999, SPO2=-999, SPO2Valid=0
, HR=-999, SPO2=-999, SPO2Valid=0
, HR=-999, SPO2=-999, SPO2Valid=0
, HR=-999, SPO2=-999, SPO2Valid=0
, HR=-999, SPO2=-999, SPO2Valid=0
, HR=-999, SPO2=-999, SPO2Valid=0
, HR=-999, SPO2=-999, SPO2Valid=0
, HR=-999, SPO2=-999, SPO2Valid=0
, HR=-999, SPO2=-999, SPO2Valid=0
, HR=-999, SPO2=-999, SPO2Valid=0
, HR=-999, SPO2=-999, SPO2Valid=0
, HR=-999, SPO2=-999, SPO2Valid=0
, HR=107, SPO2=-999, SPO2Valid=0
, HR=107, SPO2=-999, SPO2Valid=0
, HR=107, SPO2=-999, SPO2Valid=0
, HR=107, SPO2=-999, SPO2Valid=0
, HR=107, SPO2=-999, SPO2Valid=0
, HR=107, SPO2=-999, SPO2Valid=0
, HR=107, SPO2=-999, SPO2Valid=0
, HR=107, SPO2=-999, SPO2Valid=0
, HR=107, SPO2=-999, SPO2Valid=0
, HR=107, SPO2=-999, SPO2Valid=0
, HR=107, SPO2=-999, SPO2Valid=0
, HR=107, SPO2=-999, SPO2Valid=0
, HR=107, SPO2=-999, SPO2Valid=0
, HR=107, SPO2=-999, SPO2Valid=0
, HR=107, SPO2=-999, SPO2Valid=0
, HR=107, SPO2=-999, SPO2Valid=0
, HR=107, SPO2=-999, SPO2Valid=0
, HR=107, SPO2=-999, SPO2Valid=0
, HR=107, SPO2=-999, SPO2Valid=0
, HR=107, SPO2=-999, SPO2Valid=0
, HR=107, SPO2=-999, SPO2Valid=0
, HR=107, SPO2=-999, SPO2Valid=0
, HR=107, SPO2=-999, SPO2Valid=0
, HR=107, SPO2=-999, SPO2Valid=0
, HR=107, SPO2=-999, SPO2Valid=0
, HR=214, SPO2=63, SPO2Valid=1
, HR=214, SPO2=63, SPO2Valid=1
, HR=214, SPO2=63, SPO2Valid=1
, HR=214, SPO2=63, SPO2Valid=1
, HR=214, SPO2=63, SPO2Valid=1
, HR=214, SPO2=63, SPO2Valid=1
, HR=214, SPO2=63, SPO2Valid=1
, HR=214, SPO2=63, SPO2Valid=1
, HR=214, SPO2=63, SPO2Valid=1
, HR=214, SPO2=63, SPO2Valid=1
, HR=214, SPO2=63, SPO2Valid=1
, HR=214, SPO2=63, SPO2Valid=1
, HR=214, SPO2=63, SPO2Valid=1
, HR=214, SPO2=63, SPO2Valid=1
, HR=214, SPO2=63, SPO2Valid=1
, HR=214, SPO2=63, SPO2Valid=1
, HR=214, SPO2=63, SPO2Valid=1
, HR=214, SPO2=63, SPO2Valid=1
, HR=214, SPO2=63, SPO2Valid=1
, HR=214, SPO2=63, SPO2Valid=1
, HR=214, SPO2=63, SPO2Valid=1
, HR=214, SPO2=63, SPO2Valid=1
, HR=214, SPO2=63, SPO2Valid=1
, HR=214, SPO2=63, SPO2Valid=1
, HR=214, SPO2=63, SPO2Valid=1
, HR=136, SPO2=84, SPO2Valid=1
, HR=136, SPO2=84, SPO2Valid=1
, HR=136, SPO2=84, SPO2Valid=1
, HR=136, SPO2=84, SPO2Valid=1
, HR=136, SPO2=84, SPO2Valid=1
, HR=136, SPO2=84, SPO2Valid=1
, HR=136, SPO2=84, SPO2Valid=1
, HR=136, SPO2=84, SPO2Valid=1
, HR=136, SPO2=84, SPO2Valid=1
, HR=136, SPO2=84, SPO2Valid=1
, HR=136, SPO2=84, SPO2Valid=1
, HR=136, SPO2=84, SPO2Valid=1
, HR=136, SPO2=84, SPO2Valid=1
, HR=136, SPO2=84, SPO2Valid=1
, HR=136, SPO2=84, SPO2Valid=1
, HR=136, SPO2=84, SPO2Valid=1
, HR=136, SPO2=84, SPO2Valid=1
, HR=136, SPO2=84, SPO2Valid=1
, HR=136, SPO2=84, SPO2Valid=1
, HR=136, SPO2=84, SPO2Valid=1
, HR=136, SPO2=84, SPO2Valid=1
, HR=136, SPO2=84, SPO2Valid=1
, HR=136, SPO2=84, SPO2Valid=1
, HR=136, SPO2=84, SPO2Valid=1
, HR=136, SPO2=84, SPO2Valid=1
, HR=250, SPO2=72, SPO2Valid=1
, HR=250, SPO2=72, SPO2Valid=1
, HR=250, SPO2=72, SPO2Valid=1
, HR=250, SPO2=72, SPO2Valid=1
, HR=250, SPO2=72, SPO2Valid=1
, HR=250, SPO2=72, SPO2Valid=1
, HR=250, SPO2=72, SPO2Valid=1
, HR=250, SPO2=72, SPO2Valid=1
, HR=250, SPO2=72, SPO2Valid=1
, HR=250, SPO2=72, SPO2Valid=1
, HR=250, SPO2=72, SPO2Valid=1
, HR=250, SPO2=72, SPO2Valid=1
, HR=250, SPO2=72, SPO2Valid=1
, HR=250, SPO2=72, SPO2Valid=1
, HR=250, SPO2=72, SPO2Valid=1
, HR=250, SPO2=72, SPO2Valid=1
, HR=250, SPO2=72, SPO2Valid=1
, HR=250, SPO2=72, SPO2Valid=1
, HR=250, SPO2=72, SPO2Valid=1
, HR=250, SPO2=72, SPO2Valid=1
, HR=250, SPO2=72, SPO2Valid=1
, HR=250, SPO2=72, SPO2Valid=1
, HR=250, SPO2=72, SPO2Valid=1
, HR=250, SPO2=72, SPO2Valid=1
, HR=250, SPO2=72, SPO2Valid=1
, HR=250, SPO2=48, SPO2Valid=1
, HR=250, SPO2=48, SPO2Valid=1
, HR=250, SPO2=48, SPO2Valid=1
, HR=250, SPO2=48, SPO2Valid=1
, HR=250, SPO2=48, SPO2Valid=1
, HR=250, SPO2=48, SPO2Valid=1
, HR=250, SPO2=48, SPO2Valid=1
, HR=250, SPO2=48, SPO2Valid=1
, HR=250, SPO2=48, SPO2Valid=1
, HR=250, SPO2=48, SPO2Valid=1
, HR=250, SPO2=48, SPO2Valid=1
, HR=250, SPO2=48, SPO2Valid=1
, HR=250, SPO2=48, SPO2Valid=1
, HR=250, SPO2=48, SPO2Valid=1
, HR=250, SPO2=48, SPO2Valid=1
, HR=250, SPO2=48, SPO2Valid=1
, HR=250, SPO2=48, SPO2Valid=1
, HR=250, SPO2=48, SPO2Valid=1
, HR=250, SPO2=48, SPO2Valid=1
, HR=250, SPO2=48, SPO2Valid=1
, HR=250, SPO2=48, SPO2Valid=1
, HR=250, SPO2=48, SPO2Valid=1
, HR=250, SPO2=48, SPO2Valid=1
, HR=250, SPO2=48, SPO2Valid=1
, HR=250, SPO2=48, SPO2Valid=1
, HR=214, SPO2=61, SPO2Valid=1
,
KarimAhmed-dotcom commented 1 year ago

I'm facing the same problem

yaelgl commented 1 year ago

same problem lol

yaelgl commented 1 year ago

i add a if to get only de valid spo2

Uuka123 commented 9 months ago

I'm getting the same problem, does anyone now how to fix?