Professional Documents
Culture Documents
Pulsometer Program
Pulsometer Program
A voltage signal is detected on pin A0 with an AC component due to heart beats filtered and
amplified. Heart rate is obtained from
To eliminate noise, each data point used is the result of averaging several measures in a short
period of time (a few tens of milliseconds), and then getting a sample
For each sample we find the minima and calculate the period from the time between
successive minima. We then take the periods obtained and in order to get a more accurate
The heart rate obtained is printed in multiples of 2 (because of lack of precision more detail is
unnecessary). The process is repeated in the loop function indefinitely.
*/
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x3F,16,2);
double long t0=0; // Time reference for the time of the first minimum
int long sensorValue=0; // Reference value for the averaging of the signal
long tprev=0; // Reference time for the time when each sampling starts
double vector[15]; // Vector to store the minima; the number of minima depends on
the time length of the sample and the heart rate, but it should be < 15
void setup(){
lcd.init();
lcd.backlight();
lcd.print("Please wait");
void loop(){
int mk; // Index for the position of the heart rate closest to the mean in
each sample
values[n]=avsensorValue; // time of the interval. Since this isn't the time of the
point but the time of the last value of
count=0; // a possible error of 1-2 beats per minute (bpm) in the heart
rate. This will not be significant, though.
sensorValue=0;
if(n>149){ // When our data sample is full, we analyse the data to get the
heart rate (a few seconds will have past)
// a local minimum
int minim=1023;
for(int k=m-10;k<=m+10;k++){
if(values[k]<minim){
minim=values[k];
mk=k;
double T=times[m]-t0; // the previous minimum. For the first value, only the
time of the minimum is obtained (t0=0) and no period
if(t0>0){ // nor heart rate is calculated. When the next minimum is found,
then t0>0 and equal to the time of the first
// Now that we have a vector with the first l values storing the heart rates measured in the
sample (from vector[0] to vector[l-1]), we try to get the most
//With the mean: we calculate the mean and we eliminate the value farthest from it; we
repeat this process until only two values remain, and we take the
int g=l;
while(g>2){
for(int k=0;k<g;k++){
valor=valor+vector[k];
double mean=valor/g;
double disp=0;
for(int k=0;k<g;k++){
if(abs(vector[k]-mean)>=disp){
disp=abs(vector[k]-mean);
mk=k;
}
double disc=vector[g-1];
vector[g-1]=vector[mk];
//Serial.println(vector[mk]);
vector[mk]=disc;
g=g-1;
for(int k=0;k<g;k++){
valor=valor+vector[k];
double mean=valor/g;
double avHR=int(mean/2)*2;
Serial.println(avHR);
lcd.clear();
lcd.print("Heart rate:"+String(avHR));
//With the median: we calculate the median and use it as the heart rate value
// for(int h=0;h<l-(k+1);h++){
// if(vector[h]>vector[h+1]){
// int t=vector[h];
// vector[h]=vector[h+1];
// vector[h+1]=t;
// }
// }
// }
//
// }
// else{
// Serial.println(int(((vector[l/2-1]+vector[l/2])/2)/10)*5);
// }
n=0; // Reinitialize n