I have change the code a little bit. Instead of detecting the interrupt on changes, I detect it on rising front, this is more accurate because I calculate the time between two rising fronts instead of one falling front and one rising front.
Although, the arduino is not seeming to always see it, so the heart rate takes times to refresh. I will try to see on the scope tomorrow if my signal is taking all the amplitude from 5V to 0V when there is a pulse. I will probably play with the peak detector capacitor. I think I will enjoy the polar module from sparkfun...
Here is my new code:
volatile int fc, D3 = 3, D4 = 4, D5 =5, D6 = 6, D7 = 7, D8 = 8, 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, RISING); //call pulse function when an interrupt is detected on Port 2;
}
void loop()
{
int frequence;
if (state == 2) {
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 = frequence;
}
}
ssegments(); //call ssegments function
}
void pulse()
{
switch(state) {
case 0:
case 1:
thisTime = millis();
state++;
break;
case 2:
lastTime = thisTime;
state = 0;
break;
}
}
void ssegments() {
int unite, dizaine, centaine;
unite = (fc % 10); //put the first number of the decimal value of bpm in unite variable
dizaine = (((fc % 100) - unite)/10);//put the second number of the decimal value of bpm in dizaine variable
centaine = (((fc % 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;
}
}