I am having trouble with the LCD display of my heart rate using Grove ear clip heart sensor. When the heart rate is displayed, there will be weird characters following it. Is there any way to fix it?
Below is my code
// include the library code:
#include <LiquidCrystal.h>
#define LED 4//indicator, Grove - LED is connected with D4 of Arduino
// initialize the liquid crystal library with the numbers of the interface pins
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
// HRV
boolean led_state = LOW;//state of LED, each time an external interrupt will change the state of LED
unsigned char counter;
unsigned long temp[21];
unsigned long sub;
bool data_effect = true;
unsigned int heart_rate = digitalRead(2);//the measurement result of heart rate
const int max_heartpluse_duty = 2000;//you can change it follow your system's request.
//2000 meams 2 seconds. System return error
//if the duty overtrip 2 second.
void setup()
{
Serial.begin(115200);
lcd.begin(20, 4);
pinMode(LED, OUTPUT);
delay(5000);
arrayInit();
attachInterrupt(digitalPinToInterrupt(2), interrupt, RISING);//set interrupt 0,digital port 2
}
void loop()
{
digitalWrite(LED, led_state);//Update the state of the indicator
void sum();
void interrupt();
void arrayInit();
delay(100);
}
/*Function: calculate the heart rate*/
void sum()
{
if (data_effect)
{
heart_rate = 1200000 / (temp[20] - temp[0]); //60*20*1000/20_total_time
Serial.print("Heart_rate_is:\t");
Serial.println(heart_rate);
lcd.setCursor(0, 2);
lcd.print("Heart rate: ");
lcd.println(heart_rate);
// delay(100);
}
data_effect = 1; //sign bit
}
/*Function: Interrupt service routine.Get the sigal from the external interrupt*/
void interrupt()
{
temp[counter] = millis();
switch (counter)
{
case 0:
sub = temp[counter] - temp[20];
break;
default:
sub = temp[counter] - temp[counter - 1];
break;
}
if (sub > max_heartpluse_duty) //set 2 seconds as max heart pluse duty
{
data_effect = 0; //sign bit
counter = 0;
Serial.println("Heart rate measure error,test will restart!" );
lcd.setCursor(0, 2);
lcd.print("Heart rate: error");
arrayInit();
}
if (counter == 20 && data_effect)
{
counter = 0;
sum();
}
else if (counter != 20 && data_effect)
counter++;
else
{
counter = 0;
data_effect = 1;
}
}
/*Function: Initialization for the array(temp)*/
void arrayInit()
{
for (unsigned char i = 0; i < 20; i ++)
{
temp[i] = 0;
}
temp[20] = millis();
}