First, I am receiving the signal from a Polar chest belt with the Rick Moll's circuit (http://rick.mollprojects.com/hrm/)
** I will soon replace it with the Polar Heart Rate module from Sparkfun: http://www.sparkfun.com/commerce/product_info.php?products_id=8660.
I put the signal on Port 2 of Arduino to use an interrupt in my code (which is at the bottom of this topic).
I, then, use port 3 to port 6 to send the BCD code to the the 7 segments and port 7 and 8 to choose which segment I use.
Here is my first version of the schematic:
Here is my code, I would like to know a better way to calculate the heart rate. Now I am doing an average of some value than i do it again and again. The beat per minute showing on my watch is varying slower than what I have on the 7-segments and changes in the beat per minute are less important. How could I improve this? Thank you for your help.
volatile int moyennefrequence, D3 = 3, D4 = 4, D5 =5, D6 = 6, D7 = 7, D8 = 8;
volatile int state = 0, lastTime, thisTime;
void setup()
{
pinMode(D3, OUTPUT); //P3 to P8 as output
pinMode(D4, OUTPUT);
pinMode(D5, OUTPUT);
pinMode(D6, OUTPUT);
pinMode(D7, OUTPUT);
pinMode(D8, OUTPUT);
attachInterrupt(0, pulse, CHANGE); //call pulse function when an interrupt is detected on Port 2;
}
void loop()
{
static int i = 0, m, fc[5], waitTime, sommefrequence = 0, frequence;
long DEAD_TIME = 2000;
if (state == 2) { //if 2nd pulse detected
frequence = (60000 / (thisTime-lastTime)); //calculate bpm using time between the two pulses
if ((frequence >= 40) && (frequence <= 220)) { //if bpm is between 40 and 220
fc[i] = frequence; //add bpm at the position "i" in the array
i++; //change array position
}
}
waitTime = millis()-lastTime; //Time since no pulses were detected
if (waitTime > DEAD_TIME) { //if too long
moyennefrequence = 0; //bpm = 0;
}
if (i == 4) { //if array is full
for(m = 0; m < 5; m++) {
sommefrequence = fc[m] + sommefrequence; //addition of the bpm in the array
}
moyennefrequence = (sommefrequence / 4); //calculate average
i = 0; //reset array position
sommefrequence = 0; //reset addition result
}
ssegments(); //call ssegments function
}
void pulse()
{
if (state == 2) { //if second pulse is detected
lastTime = thisTime; //the time of the first pulse is put in the lastTime variable
state = 1; //reset pulse counter at 1
}
if (state < 2) { //if less than 2 pulse has been detected
thisTime = millis(); //put the current time in thisTime variable
state = state++; //increment pulse counter
}
}
void ssegments() {
int unite, dizaine, centaine;
unite = (moyennefrequence % 10); //put the first number of the decimal value of bpm in unite variable
dizaine = (((moyennefrequence % 100) - unite)/10);//put the second number of the decimal value of bpm in dizaine variable
centaine = (((moyennefrequence % 1000) - dizaine - unite)/100);//put the third number of the decimal value of bpm in centaine variable
digitalWrite(D7, LOW); //address of first number
digitalWrite(D8, HIGH);
chiffre(unite); //send unite to funtion chiffre to be displayed
delay(1);
digitalWrite(D7, HIGH);//address of second number
digitalWrite(D8, LOW);
chiffre(dizaine); //send dizaine to funtion chiffre to be displayed
delay(1);
digitalWrite(D7, HIGH);//address of third number
digitalWrite(D8, HIGH);
chiffre(centaine); //send centaine to funtion chiffre to be displayed
delay(1);
}
void chiffre(int i) { //send bcd value of decimal number to ports D3 to D6
switch(i){
case 1:
digitalWrite(D6, HIGH);
digitalWrite(D3, LOW);
digitalWrite(D4, LOW);
digitalWrite(D5, LOW);
break;
case 2:
digitalWrite(D6, LOW);
digitalWrite(D3, HIGH);
digitalWrite(D4, LOW);
digitalWrite(D5, LOW);
break;
case 3:
digitalWrite(D6, HIGH);
digitalWrite(D3, HIGH);
digitalWrite(D4, LOW);
digitalWrite(D5, LOW);
break;
case 4:
digitalWrite(D6, LOW);
digitalWrite(D3, LOW);
digitalWrite(D4, HIGH);
digitalWrite(D5, LOW);
break;
case 5:
digitalWrite(D6, HIGH);
digitalWrite(D3, LOW);
digitalWrite(D4, HIGH);
digitalWrite(D5, LOW);
break;
case 6:
digitalWrite(D6, LOW);
digitalWrite(D3, HIGH);
digitalWrite(D4, HIGH);
digitalWrite(D5, LOW);
break;
case 7:
digitalWrite(D6, HIGH);
digitalWrite(D3, HIGH);
digitalWrite(D4, HIGH);
digitalWrite(D5, LOW);
break;
case 8:
digitalWrite(D6, LOW);
digitalWrite(D3, LOW);
digitalWrite(D4, LOW);
digitalWrite(D5, HIGH);
break;
case 9:
digitalWrite(D6, HIGH);
digitalWrite(D3, LOW);
digitalWrite(D4, LOW);
digitalWrite(D5, HIGH);
break;
case 0:
digitalWrite(D6, LOW);
digitalWrite(D3, LOW);
digitalWrite(D4, LOW);
digitalWrite(D5, LOW);
break;
}
}

Sorry about my english, I usually speak french. ![]()