Arduino code help needed!!

Thread Starter


Joined Jun 6, 2014

I have been working on this IR heat rate monitoring project I found on internet. It is simple setup...An ir emitter and a detector pair is used as a sensor...which is touched to your shines light up your skin and then reflected light is dected by the detector..this signal is then fed into an lm386 which amplifies the signal which is fed into an analog pin of an arduino. My Arduino has the following code running on it....

Rich (BB code):
int heart_high, heart_low;
int data[200], i, mapped;
void setup(){
  for(i=2; i<12; i++)//LEDs
    pinMode(i, OUTPUT);
  Serial.begin(115200);//for the processing sketch


void loop(){

  heart_high=0;//reset the high before we look for it,  everything is higher than 0
  heart_low = 1023;//reset the low before we look for it, everything is lower than 1023

  for(i=200; i>0; i--){              //search for the low and high out of the last 200 samples
             data = data[i-1];    // move everything back one, this will leave data[0] empty, but we put the next read there after this

        if(data>heart_high)      //check to see if we have something higher than the current high
             heart_high=data;    //if it is higher, write the new value in
        if(data<heart_low)       //same idea as the high check
  }//for loop

  data[0] = analogRead(0);    //now write the next analog value to data[0]

  mapped = map(data[0], 0, 1023, 0, 600);
  Serial.println(mapped);     //send over the current value to the processing sketch, but scale it to match the screen height

  delay(5);                   //delay in here is important, we need enough samples to catch an entire waveform, so at least 1 sec of samples should be tored in data[0], 
                              //so 5ms x 200 = 1000, we're good

  //This code is where we control the LEDs
  if((heart_high-heart_low)>150){       //first, don't even go in here unless the span of the high and low is greater than 150.  This is important, so the LEDs don't go crazy 
    //if you're flat lined

    //I'll explain how one of these works
    //There is an 'if' check for each of the LEDs
    //Basically, how this works is we want the LEDs turn represent the entire span of a heart beat, which is why we get the lowest and highest value
    //this gives us the span, which can be used to take a percentage of, and that's exactly how all these statements work
    if(data[0] > (heart_high-.95*(heart_high-heart_low)))    //this is true if the read is greater than 95% of the span
      digitalWrite(2, HIGH);

    if(data[0] > (heart_high-.9*(heart_high-heart_low)))     // greater than 90% of the span and so on
      digitalWrite(3, HIGH);

    if(data[0] > (heart_high-.8*(heart_high-heart_low)))
      digitalWrite(4, HIGH);
    if(data[0] > (heart_high-.7*(heart_high-heart_low)))
      digitalWrite(5, HIGH);
    if(data[0] > (heart_high-.6*(heart_high-heart_low)))
      digitalWrite(6, HIGH);
    if(data[0] > (heart_high-.5*(heart_high-heart_low)))
      digitalWrite(7, HIGH);
    if(data[0] > (heart_high-.4*(heart_high-heart_low)))
      digitalWrite(8, HIGH);
    if(data[0] > (heart_high-.3*(heart_high-heart_low)))
      digitalWrite(9, HIGH);
    if(data[0] > (heart_high-.2*(heart_high-heart_low)))
      digitalWrite(10, HIGH);
    if(data[0] > (heart_high-.1*(heart_high-heart_low)))
      digitalWrite(11, HIGH);

  }//span check
    for(i=2; i<12; i++)//turn all the LEDs off if we're flatlined


this is not my code...I found this on internet!!

This code flashes 10 leds with respect to your heart beat and also sends data through serial to the pc which is than read by a processing app which visualizes the waveform. It is cool, But I don't want the wave form. I want to calculate the "beats per minute". How can i do that? I don't want to use a pc instead i am planing to use a 16x2 lcd to display the calculated BPM. I have some experience with the arduino LCD library. But as i am new to programing I can't figure out how to calculate the BPM. I would be really helpful If anyone could help me modify this code to calculate the "BPM". I have to put this project on display quick response will be really helpful !!


Joined Apr 24, 2011
I am most sorry to tell you that we are not a "write your code by deadline for free" facility.

If there is someone here that speaks Arduino (and I do not) then they may be able to help you as you yourself learn the language and write the code yourself.

BPM is simply counting how many beats are observed in one minute. One can use a shorter interval (say 6 seconds) and multiply the beat count for that period (here would be 60/6=10) for a more responsive reading.

I cannot tell from the code posted if this measurement is made.

Thread Starter


Joined Jun 6, 2014
I think you misunderstood me!! I didn't say anyone to write my code..I asked for help to modify my suggesting some kind of sorting algorithm which would sort out the peaks and count it!! Anyways thanks for your reply!!


Joined Apr 24, 2011
As long as the array of data is made with some sort of known uniform interval then you already have the beginnings of your algorithm: skip thru the array and find the peaks (or valleys), and record the index of two readings.

If the interval is regular (and known) then you should be able to pull out a rate from that: (delta index) * (known interval) = 1 / (heartbeat rate)

So invert that number, conver to ASCII, and send it to the display.


Joined Jan 21, 2013
There is a delay of 5mS in your loop, assuming the rest of the code executes quickly (i.e.uS) then you can set a flag when the maximum is reached and count the number of times the loop executes before the next max is reached. The number of loop iterations divided by 200 then gives you seconds per beat - as ErnieM says - invert that for BPS multiply by 60 for BPM