1

I am doing a BPM monitoring system. The serial monitor displays accurate BPM values ranging from 60 to 100+. But my LCD display is not accurate. Example, on the serial monitor, my bpm is about 67, but on the LCD it displays 676. What do I need to do to fix it?

PulseSensorPlayground pulseSensor; // Creates an instance of the PulseSensorPlayground object called "pulseSensor"

void setup() { 
    Serial.begin(9600); // For Serial Monitor
    lcd.begin();
    lcd.backlight();
 
    // Configure the PulseSensor object, by assigning our variables to it.
    pulseSensor.analogInput(PulseWire);
    pulseSensor.blinkOnPulse(LED13); //auto-magically blink Arduino's LED with heartbeat.
    pulseSensor.setThreshold(Threshold);
 
    // Double-check the "pulseSensor" object was created and "began" seeing a signal.
    if (pulseSensor.begin()) {
        Serial.println("We created a pulseSensor Object !"); //This prints one time at Arduino power-up, or on Arduino reset.
    }
}
 
void loop() { 
    int myBPM = pulseSensor.getBeatsPerMinute(); // Calls function on our pulseSensor object that returns BPM as an "int".
    // "myBPM" hold this BPM value now.
    if (pulseSensor.sawStartOfBeat()) { // Constantly test to see if "a beat happened".
        Serial.println("♥ A HeartBeat Happened ! "); // If test is "true", print a message "a heartbeat happened".
        Serial.print("BPM: "); // Print phrase "BPM: "
        Serial.println(myBPM); // Print the value inside of myBPM.

        lcd.setCursor(0,0);
        lcd.print("BPM: "); // Print phrase "BPM: "
        lcd.print(myBPM);
    }
    delay(50); // considered best practice in a simple sketch.
}
7
  • I fixed the formatting and the indentation for you. You should always keep the indentation right (the Arduino IDE even has an autoformatting function for this), because it helps you spot structural errors.
    – chrisl
    Nov 9 '21 at 8:45
  • 5
    What happens if you add lcd.print(" "); after the other LCD code to fill the space behind the BPM with spaces? I suspect that the additional 6 comes from a former display call and just is retained because you don't overwrite it. Adding the spaces will make sure, that the space after the BPM is always empty.
    – chrisl
    Nov 9 '21 at 8:48
  • I am thinking the same thing. And if I am understanding this correctly, you mean adding lcd.print(" "); after lcd.print("BPM: ");? I already did sir but the former display still keeps displaying.
    – AccelLim
    Nov 9 '21 at 9:06
  • 1
    @AccelLim: You need to insert lcd.print(" "); after lcd.print(myBPM);. You want the last digit of the number to be removed.
    – PMF
    Nov 9 '21 at 9:17
  • If you use the standard LCD lib from Arduino you could clear the Screen with lcd.clear() instead of simply position the cursor to (0,0) with lcd.setCursor(0,0). Link: arduino.cc/en/Reference/LiquidCrystalClear Nov 9 '21 at 9:48
4

The additional "6" seems to come from one of the first values. Maybe the first value printed is just a rogue value and all the following values are correct.

The point is that this rogue value may take one more character on the display than the other (correct) values. And when you just move the cursor and print again the previously displayed text is only changed if it is overwritten by the new text. The correct values are shorter, so the last digit of the rogue value is not overwritten. This of course doesn't happen with Serial, since Serial doesn't print to the same space again and again (like a LCD does with its limited space).

What to do now: You can use lcd.clear() to remove all text from the display before printing new values. Though this can lead to a noticeable flicker on the display. Often it is better to just make sure to overwrite all possible leftover text with spaces via lcd.print(" ");. Insert this after the lcd.print(myBPM) (since you want to remove all leftover digits after that). In this case you only have 1 leftover digit, so one space is enough. To be sure that this won't happen again when for whatever reason you get more than one leftover digit you can use multiple spaces (lcd.print(" ");).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.