Running this code on Timer2

hello everybody,
I am working on a Heart Rate Monitoring project, and need some assistance on it...
I have a code which gives output as "BPM" from a Heart Rate Monitoring Sensor AD8232, I am getting proper output from the code.
with this Heart Rate monitoring device, I also have GPS in my system which consists of delays etc..

i don't want GPS code and Heart rate monitoring code gets conficts.

I want to run this Heart Rate monitoring code on Timer2.

how to do that.?

Code :

int x = 0;
int LastTime = 0;
bool BPMTiming = false;
bool BeatComplete = false;
int BPM = 0;

#define UpperThreshold 518
#define LowerThreshold 495

int LED13 = 44;   //  The on-board Arduion LED


int Signal;                // holds the incoming raw data. Signal value can range from 0-1024



void setup() {
  pinMode(LED_BUILTIN, OUTPUT);
  pinMode(LED13, OUTPUT);
  digitalWrite(LED_BUILTIN, HIGH);
  Serial.begin(9600);
}


void loop()
{
  int value = analogRead(0);
  if (value > UpperThreshold)
  {
    if (BeatComplete)
    {
      BPM = millis() - LastTime;
      BPM = int(60 / (float(BPM) / 1000));
      BPMTiming = false;
      BeatComplete = false;
    }
    if (BPMTiming == false)
    {
      LastTime = millis();
      BPMTiming = true;
    }
  }
  if ((value < LowerThreshold) & (BPMTiming))
    BeatComplete = true;


  x++;
  Signal = analogRead(0);  // Read the PulseSensor's value.


  if (Signal > UpperThreshold) {                        // If the signal is above "550", then "turn-on" Arduino's on-Board LED.
    digitalWrite(LED13, HIGH);
  } else {
    digitalWrite(LED13, LOW);               //  Else, the sigal must be below "550", so "turn-off" this LED.
  }

  Serial.print(BPM);
  Serial.print(",");
  Serial.println(Signal);
}

Please help
thanks

I want to run this Heart Rate monitoring code on Timer2.

The only timing you do is calling millis(). Why do you think you need a different timer?

I also have GPS in my system which consists of delays etc..

The data being streamed to the Arduino, from the GPS, does not have delays. If you have delay()s in your code, get rid of them.

This is an XY problem. You have a solution to a problem. You tell us the solution when what we need to know, in order to help you, is the problem. Which GPS (datasheet?) are you using? Why do you think that using Timer2 will help?

Hello Thank you for reply,

I am building a device for our rugby team which can log data into .xml file in SD card, This device consists of GNSS,Accelerometer few LEDs for indication of charging the battery and other indication like GPS acquisition etc .

Now we want to implement Heart Rate BPM data to our device, we came up with this AD8232 module which outputs analog data from a user..

See picture below of output in Serial Plotter

I was searching on the internet for help on getting the BPM from this kind of sensor output.

I came across with two codes :

1: which blinks LED on pin13 when the heart beat happens
2: which converts analog values from the sensor to BPM.

I merged both of them and the code i have is in the main thread.

I wanted to merged this code with my main code (which logs data of GNSS and Accelerometer to SD card) but came up with an issue.

When i merged the code, the hr_led (LED which blinks when heart beat happens) on Arduino mega misses heart beat sometimes.

I thought it would be timing issue and thus started this thread.

main code is attached with this thread, because as the number of words exceeding 9000.

I know my code is not fully optimised, but right now it's workable

i have added BPM code in void secondStage()

but that doesn't work

I tried to print BPM at

dataFile.print  ( BPM );

in

void secondStage()

Which GPS (datasheet?) are you using?

I am using L86 GNSS module

Thanks

_8_Star.ino (19.3 KB)

i have added BPM code in void secondStage()

but that doesn't work

It does something. "that doesn't work" is useless information. What it actually does, and how that differs from what you want would be useful information.

what you want would be useful information.

ahh! yes, sorry for that - it actually prints the BPM value there, but misses some of them and prints incorrect one.

but misses some of them

How do you know that? Does it happen when the buffer gets full and needs to actually be written to the SD card?

and prints incorrect one.

Again, how do you know that the values are incorrect? Some same data that illustrates the problem would be good.

How do you know that?

I am watching the LED (which is blinking when the heart beat happens) connected on pin 44 of my Arduino, it doesn't blinks sometimes, I have also seen in the logged file, BPM drops down when that blink misses...

Please see attached logged file.

and i don't know why it's printing such higher values at BPM place in the starting of logging

12-28-22-T-22-08-2017.xml.zip (5.34 KB)