Go Down

Topic: Algorithm for establishing Heart Rates (Read 3493 times) previous topic - next topic


Apr 25, 2010, 04:35 pm Last Edit: Apr 25, 2010, 04:35 pm by JControls Reason: 1
Hi all,

I have managed to get Arduino reading heart rates via a Polar Heart Rate transmitter and a Heart Rate Monitor interface. What I have it doing at the moment is counting in milliseconds the elapsed time between each heart beat. This is displayed through the serial monitor.

What I need to do now is some more Math and I am completely and utterly stuck. I need to be able to calculate the heart rate (BPM) without latency. I have an algorithm, which I think should work, but have no idea how incorporate it in my Arduino sketch! This is the algorithm:

I am going to add together the elapsed time between 10 heart beats. The total of this will be divided into 60 (Seconds). This number will then be multiplied by 10 to give the BPM/Heart Rate of the user. It could be more accurate I feel if I was to calculate it to an acuracy of 15 or 20 but that will take too long for what I am doing. Here is the sketch so far:

Code: [Select]

int inPin = 7;         // the number of the input pin
int ledPin1 = 13;
int reading;           // the current reading from the input pin
int previous = LOW;    // the previous reading from the input pin
int count = 0; //number of times button was pressed - heart beat (digital)

void setup()
 pinMode(ledPin1, OUTPUT);
 pinMode(inPin, INPUT);

void loop()
 reading = digitalRead(inPin);

 if (reading == HIGH && previous == LOW && millis() - time > debounce) {
   time = millis();
 previous = reading;

Any suggestions on how I could update my sketch to incorporate this algorithm/calculate the heart rate/BPM? Any help would be greatly appreciated.


You have a ratio.

n beats        1 beat
---------   =   --------
1 minute       t milliseconds

By using common units in the numerator and denominator:

n beats        60000 beats
---------    = ---------------
1 minute      t milliseconds

So, the number of beats per second is 60000 / t, where t is the time between heartbeats, which is what you were measuring.

If the digitalRead is reading the heartbeat monitor, you do not need to debounce it. Debouncing is only needed for mechanical switches.

There is a pulseIn command that will measure the time it takes a sensor to go HIGH or LOW that uses interrupts, rather than polling, which your process uses, and will, therefore, be more accurate.

If you're building a party toy, use your method. If you're building a serious heart rate monitor, use pulseIn.
The art of getting good answers lies in asking good questions.


Thanks PaulS.

It is a serious project, the BPM in this case will be used to trigger video and audio, it is essentially an interactive horror film where my user is interacting with the film through their heart rate. I'll see what I can do with what you've given me thanks again appreciate it.

Go Up