Essentially trying to determine heart rate. Current Test input signal being used is a rectified triange wave. The code below makes sense in my mind, however the values being printed to the putty terminal are all over the place, and coming in alot faster than the actual frequency of the signal. At first i thought it might be noise in the signal triggering above and below a threshold, changing the flag. However introduction of noise margins ie 0.7 on rising and 0.5 on negative edge.
Any alternate suggestions? kind of stumped.
#include "mbed.h"
Serial pc(USBTX, USBRX); // tx, rx
#define UPPER_THRESHOLD 0.7
#define LOWER_THRESHOLD 0.5
int main() {
pc.printf("Hello World!\n");
Timer t;
AnalogIn ain(p19);
DigitalOut led1(LED1);
DigitalOut led2(LED2);
DigitalOut led3(LED3);
DigitalOut led4(LED4);
int flag=0;
float interval;
double BPM;
while (1){
if (ain>UPPER_THRESHOLD && flag==0){
t.stop();
interval=t.read();
/// record time
BPM=60/interval;
pc.printf("BPM: %f \n", BPM);
t.reset();
t.start();
flag=1;
}
if (ain
if (ain<LOWER_THRESHOLD && flag==1){
flag = 0;
}
}
}
Essentially trying to determine heart rate. Current Test input signal being used is a rectified triange wave. The code below makes sense in my mind, however the values being printed to the putty terminal are all over the place, and coming in alot faster than the actual frequency of the signal. At first i thought it might be noise in the signal triggering above and below a threshold, changing the flag. However introduction of noise margins ie 0.7 on rising and 0.5 on negative edge.
Any alternate suggestions? kind of stumped.
if (ain<LOWER_THRESHOLD && flag==1){
flag = 0;
}
}
}