Arduino code help needed!!

Discussion in 'Embedded Systems and Microcontrollers' started by MrOmnos, Jun 18, 2014.

  1. MrOmnos

    Thread Starter New Member

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

    Code ( (Unknown Language)):
    2. int heart_high, heart_low;
    3. int data[200], i, mapped;
    4. void setup(){
    5.   for(i=2; i<12; i++)//LEDs
    6.     pinMode(i, OUTPUT);
    7.   Serial.begin(115200);//for the processing sketch
    9. }//setup
    12. void loop(){
    14.   heart_high=0;//reset the high before we look for it,  everything is higher than 0
    15.   heart_low = 1023;//reset the low before we look for it, everything is lower than 1023
    17.   for(i=200; i>0; i--){              //search for the low and high out of the last 200 samples
    18.              data[i] = data[i-1];    // move everything back one, this will leave data[0] empty, but we put the next read there after this
    20.         if(data[i]>heart_high)      //check to see if we have something higher than the current high
    22.              heart_high=data[i];    //if it is higher, write the new value in
    24.         if(data[i]<heart_low)       //same idea as the high check
    26.              heart_low=data[i];
    27.   }//for loop
    29.   data[0] = analogRead(0);    //now write the next analog value to data[0]
    31.   mapped = map(data[0], 0, 1023, 0, 600);
    33.   Serial.println(mapped);     //send over the current value to the processing sketch, but scale it to match the screen height
    35.   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],
    36.                               //so 5ms x 200 = 1000, we're good
    39.   //This code is where we control the LEDs
    41.   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
    42.     //if you're flat lined
    44.     //I'll explain how one of these works
    45.     //There is an 'if' check for each of the LEDs
    46.     //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
    47.     //this gives us the span, which can be used to take a percentage of, and that's exactly how all these statements work
    49.     if(data[0] > (heart_high-.95*(heart_high-heart_low)))    //this is true if the read is greater than 95% of the span
    50.       digitalWrite(2, HIGH);
    51.     else
    52.       digitalWrite(2,LOW);
    54.     if(data[0] > (heart_high-.9*(heart_high-heart_low)))     // greater than 90% of the span and so on
    55.       digitalWrite(3, HIGH);
    56.     else
    57.       digitalWrite(3,LOW);
    59.     if(data[0] > (heart_high-.8*(heart_high-heart_low)))
    60.       digitalWrite(4, HIGH);
    61.     else
    62.       digitalWrite(4,LOW);
    63.     if(data[0] > (heart_high-.7*(heart_high-heart_low)))
    64.       digitalWrite(5, HIGH);
    65.     else
    66.       digitalWrite(5,LOW);
    67.     if(data[0] > (heart_high-.6*(heart_high-heart_low)))
    68.       digitalWrite(6, HIGH);
    69.     else
    70.       digitalWrite(6,LOW);
    71.     if(data[0] > (heart_high-.5*(heart_high-heart_low)))
    72.       digitalWrite(7, HIGH);
    73.     else
    74.       digitalWrite(7,LOW);
    75.     if(data[0] > (heart_high-.4*(heart_high-heart_low)))
    76.       digitalWrite(8, HIGH);
    77.     else
    78.       digitalWrite(8,LOW);
    79.     if(data[0] > (heart_high-.3*(heart_high-heart_low)))
    80.       digitalWrite(9, HIGH);
    81.     else
    82.       digitalWrite(9,LOW);
    83.     if(data[0] > (heart_high-.2*(heart_high-heart_low)))
    84.       digitalWrite(10, HIGH);
    85.     else
    86.       digitalWrite(10,LOW);
    87.     if(data[0] > (heart_high-.1*(heart_high-heart_low)))
    88.       digitalWrite(11, HIGH);
    89.     else
    90.       digitalWrite(11,LOW);  
    92.   }//span check
    93.   else
    94.     for(i=2; i<12; i++)//turn all the LEDs off if we're flatlined
    95.       digitalWrite(i,LOW);
    101. }//loop
    103. [/i][/i][/i][/i][/i]

    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 !!
  2. ErnieM

    AAC Fanatic!

    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.
  3. MrOmnos

    Thread Starter New Member

    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!!
  4. ErnieM

    AAC Fanatic!

    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.
  5. sirch2

    Well-Known Member

    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