Go Down

Topic: MAX30102 Heart rate sensor Stability (Read 129 times) previous topic - next topic

dietherdeyvv

Sep 16, 2019, 06:15 am Last Edit: Sep 16, 2019, 06:44 am by dietherdeyvv
Hello. I am using MAX30102 heart rate sensor and I only want to get one value from that sensor and display it. It displays a heart rate value continuously.

For example it displays this series of values:
60, 54, 60, 42, 68, 72, 72, 65

it is quite inaccurate and I think my for loop code is doing the problem.
so I want to read suggestions to people on how I can get the "65" heart rate value which is the last value of the sensor.

in my code, example, 65 is the value of the for loop 2399 and that will be the value i will display.

i want to maybe improve the code to get values every 30 seconds, and I do not know how.

I am using a for loop to do so and it is not really working really well for me. Can someone suggest to improve my code?


Code: [Select]
else if ( x == 5 ) {
    irValue = particleSensor.getIR();
    if (irValue > 7000) {
      if (checkForBeat(irValue) == true) {
        for ( a = 0; a < 2400; a++) {
          getHeart();
          heart_str = beatAvg;
          Serial.println(beatAvg);
          Serial.println(" -- ");
          Serial.println(beatsPerMinute);

          display.clear();
          display.setTextAlignment(TEXT_ALIGN_CENTER);
          display.setFont(ArialMT_Plain_10);
          display.drawString(64, 15, "HEART RATE");
          display.setFont(Lato_Medium_12);
          display.drawString(64, 30, heart_str + " bpm");
          display.display();
          delay(1);

          if ( a == 700 ) {
            Serial.println("TO FIREBASE: ");
            Serial.print(beatAvg);
            Firebase.setInt("heartrunning", beatAvg);
          }
          if ( a == 1500 ) {
            Serial.println("TO FIREBASE: ");
            Serial.print(beatAvg);
            Firebase.setInt("heartrunning", beatAvg);
          }
          if ( a == 1900 ) {
            Serial.println("TO FIREBASE: ");
            Serial.print(beatAvg);
            Firebase.setInt("heartrunning", beatAvg);
          }
          if ( a >= 2399 ) {
            x = 6;
            Firebase.setInt("DATA/" + date_str + "/" + numGenerated + "/heartrate", beatAvg);
            Firebase.setInt("heartrate", beatAvg);
            Serial.println("stopped.");
            Serial.println(beatAvg);
            display.setTextAlignment(TEXT_ALIGN_CENTER);
            display.setFont(Lato_Light_9);
            display.drawString(64, 55, "Data Sent.");
            display.display();

            // heart condition for notifications
            float normalHeart = 0; // normal
            float belowHeart = 10; // below normal
            float aboveHeart = 20; // above normal
            if (beatAvg>=101) {
              Firebase.setFloat("DATA/" + date_str + "/" + numGenerated + "/heartstat", aboveHeart);
              Firebase.setFloat("heartstat", aboveHeart);
              heartStat = "Above Normal";
            }
            else if (beatAvg<=59) {
              Firebase.setFloat("DATA/" + date_str + "/" + numGenerated + "/heartstat", belowHeart);
              Firebase.setFloat("heartstat", belowHeart);
              heartStat = "Below Normal";
            }
            else {
              Firebase.setFloat("DATA/" + date_str + "/" + numGenerated + "/heartstat", normalHeart);
              Firebase.setFloat("heartstat", normalHeart);
              heartStat = "Normal";
            }

            Serial.println("stopped.");
            display.clear();
            display.setTextAlignment(TEXT_ALIGN_CENTER);
            display.setFont(ArialMT_Plain_10);
            display.drawString(64, 15, "HEART RATE");
            display.setFont(Lato_Medium_12);
            display.drawString(64, 30, heartStat);
            display.display();
            delay(5000);
          }
        }
      }
    }

    if (irValue < 50000) {
      beatAvg = 0;
      display.clear();
      display.setTextAlignment(TEXT_ALIGN_CENTER);
      display.drawString(64, 22, "Please Place ");
      display.drawString(64, 32, "your finger ");
      display.display();
      Serial.println("no data");
      delay(1);
    }
  }

pert

it is not really working really well for me.
Much too vague. Please provide a detailed explanation of what behavior you expect and what behavior you are experiencing that is not as you expect.

dietherdeyvv

Much too vague. Please provide a detailed explanation of what behavior you expect and what behavior you are experiencing that is not as you expect.
edited now. i am sorry.

pert

i want to maybe improve the code to get values every 30 seconds, and I do not know how.
The BlinkWithoutDelay tutorial will show you how to accomplish this:
https://www.arduino.cc/en/Tutorial/BlinkWithoutDelay
The only difference is that instead of changing the state of an LED pin every 1000 ms, you will be doing a reading from the sensor every 30000 ms. So you need to change the value of the interval variable and also replace the LED code with your sensor reading and displaying code.

Go Up