Calculating heart rate per minute in real time

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

beats per minute = 60*(number of beats measured)/(time in seconds to measure those beats)

im still new to this. how would i setup my code to capture the last few beats, and the time used to capture those beats?

For further help, please read "How to use this forum" and follow the directions.

Post your code properly, post a link to the sensor data sheet and describe the wiring clearly.

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
    }
}

Thank you! this will help me start!

here is the code i have so far

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 should change this:

cprTimeRead[0] = time;

to:

cprTimeRead[0] = millis();

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.