i need to find a way to calculate in real time beats per minute (BPM) that would update in real time based on the last few readings and will change is heart rate increases or decreases
something along these lines
take a reading on the last few beats * some formula = projected BPM
output BPM on LCD
A human would count beats for a fixed time, say one minute.
A computer could measure the time between two beats.
Read about interrupts and millis()
Leo..
I'd probably put the beat times into a circular buffer and shift a pointer round on each beat, then calculate elapsed time between the current and oldest beat, then scale up to 1 minute.
Here I'll illustrate a simpler but less elegant, unoptimised method using a linear buffer, hardcoded at 4 entries. Something like this:
unsigned long beatRecordedatMs[4] = {0,0,0,0} ;
. . .
. . .
void loop() {
if (new beat received this loop iteration ) {
// shunt the current entries down in the array, losing the oldest
beatRecordedatMs[3] = beatRecordedatMs[2] ;
beatRecordedatMs[2] = beatRecordedatMs[1] ;
beatRecordedatMs[1] = beatRecordedatMs[0] ;
// add the newest beat time at the top
beatRecordedatMs[0] = millis() ;
int elapsedTimeof4Pulses = beatRecordedatMs[0] - beatRecordedatMs[3] ;
float pulseRateBeatsPerMinute = ( elapsedTimeof4Pulses / 4.0 ) * 60.0 / 1000.0
}
}
const int buttonRead = 8; //read pulse input
unsigned long time;
int BPM; //final output for rate per min
int cprTimeRead[5] = {0,0,0,0,0}; using 5 pulses time to calculate BPM
void setup(){
Serial.begin(9600);
pinMode(buttonRead, INPUT);
}
void loop(){
if ( buttonRead == HIGH){
/*i know im missing something here to calculate the mills() between each beat.. not sure what i need to do.
*/
cprTimeRead[4] = cprTimeRead[3];
cprTimeRead[3] = cprTimeRead[2];
cprTimeRead[2] = cprTimeRead[1];
cprTimeRead[0] = time;
Serial.println(cprTimeRead[5]);
timeCPR = cprTimeRead[0] - cprTimeRead[4];
BPM = ((timeCPR / 5) * 60) / 1000;
Serial.println(BPM);
lcd.setCursor(12, 0);
lcd.print(BPM);
}
You don't have to calulate the millis() between each beat. You are already doing enough in that you are calculating the diffence between the oldest and newest reading (in milliseconds) and dividing by 5 to get the average millis() between each beat.