Hey there, I am new to Arduino and I have to do a project on a real time wearable stress sensor. One of the parameters I would like to measure is the heart rate, which I am currently using the grove ear clip heart rate sensor. One problem i have when using the grove ear clip sensor is that it takes very long to display the heart rate as opposed to other heart rate sensors like the heart rate pulse sensor. I am trying to make a real time sensor and as such would like to edit the code for the grove ear clip such that it can provide quicker feedback. Is that possible? I am also unable to understand why the heart rate calculations are not done within void loop() but are instead done in external functions.
Below is the code for the grove ear clip sensor:
// Function: This program can be used to measure heart rate, the lowest pulse in the program be set to 30.
// Use an external interrupt to measure it.
// Hardware: Grove - Ear-clip Heart Rate Sensor, Grove - Base Shield, Grove - LED
// Arduino IDE: Arduino-1.0
// Author: FrankieChu
// Date: Jan 22, 2013
// Version: v1.0
// by www.seeedstudio.com
#define LED 4//indicator, Grove - LED is connected with D4 of Arduino
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()
{
pinMode(LED, OUTPUT);
Serial.begin(115200);
// Serial.println("Please ready your chest belt.");
delay(5000);
arrayInit();
// Serial.println("Heart rate test begin.");
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();
}
/*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);
}
data_effect=1;//sign bit
}
/*Function: Interrupt service routine.Get the sigal from the external interrupt*/
void interrupt()
{
temp[counter]=millis();
// Serial.println(counter,DEC);
// Serial.println(temp[counter]);
switch(counter)
{
case 0:
sub=temp[counter]-temp[20];
// Serial.println(sub);
break;
default:
sub=temp[counter]-temp[counter-1];
// Serial.println(sub);
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!" );
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();
}