Help with Arduino Code

Discussion in 'Programmer's Corner' started by dyeraaron, Jun 1, 2012.

  1. dyeraaron

    Thread Starter Active Member

    Oct 27, 2008
    57
    0
    Hello guys,

    so I've been working on my capstone for my bachelors. My team and I are implementing several circuits into a field vest used for military / Government officials. My application is two temp sensors which will differentiate between the ambient and the body temp and then will turn on either a cooling or heating system....the other is I have designed an ECG or electrocardio graph (Heart Beat Monitor circuit) I get a good reading on my oscope from the ECG using an IR emitter and detector ...it produces a nice analog ( almost digital looking) sine wave.

    so I'm using an Arduino Uno....and as of now I'm interfacing a 16 x 2 LCD ( will later use a 20 x 4) but for now...I'm showing the Body temp: in the the first row of the LCD ( the second temp will be on the 20 x 4)

    but I have a BPM: on the display as well....I want to to be able to take the output of the ECG and calcuklate somewhat of an accurate reading of how many geats it would be per minute.

    I know it will be like taking 6 High inputs and * 10 to equal a minute....somehow I need to add in a time value for millis() as well...

    I have the output of the ECG going into pin 2 digital side of the Arduino....here is my code so far....

    Code ( (Unknown Language)):
    1.  
    2. /*
    3.   Temperature Sensor, Heart Beat monitor and GPS
    4.  
    5. */
    6.  
    7. #include <LiquidCrystal.h>                      // include LCD library
    8.  
    9.  
    10. LiquidCrystal lcd(7, 8, 9, 10, 11, 12);        
    11.  
    12. // initialize the LCD with the numbers of the interface pins from the Arduino  
    13.  
    14. //declare variables
    15.  
    16. float tempC;
    17.  
    18. float tempF;
    19.  
    20. int BPM;
    21.  
    22.  
    23.  
    24.  
    25.  
    26. // Analog Inputs for ADC
    27. int tempPin = A0;
    28.  
    29. // Digital Inputs
    30. int BPMpin = 2;
    31.  
    32.  
    33. void setup()
    34. {
    35.  
    36.   pinMode (tempPin, INPUT);
    37.  
    38.   pinMode (BPMpin, INPUT);
    39.    
    40.  
    41.   // set up the LCD's number of columns and rows:
    42.  
    43.  
    44.   lcd.begin(16,2);
    45.  
    46.   lcd.print("Initializing....");
    47.  
    48.   lcd.setCursor(0, 1);
    49.  
    50.   lcd.print(".");
    51.  
    52.     delay(1000);
    53.  
    54.   lcd.setCursor(1, 1);
    55.  
    56.   lcd.print(".");
    57.  
    58.     delay(1000);
    59.    
    60.   lcd.setCursor(2, 1);
    61.  
    62.   lcd.print(".");
    63.  
    64.     delay(1000);
    65.    
    66.   lcd.setCursor(3, 1);
    67.  
    68.   lcd.print(".");
    69.  
    70.     delay(1000);
    71.    
    72.   lcd.setCursor(4, 1);
    73.  
    74.   lcd.print(".");
    75.  
    76.     delay(1000);
    77.    
    78.   lcd.setCursor(5, 1);
    79.  
    80.   lcd.print(".");
    81.  
    82.     delay(1000);
    83.    
    84.   lcd.clear();
    85.  
    86.   lcd.setCursor(3, 0);
    87.  
    88.   lcd.print("Welcome To");
    89.  
    90.     delay(2000);
    91.  
    92.   lcd.setCursor(0, 1);
    93.  
    94.   lcd.print("Vital-Ballistics");
    95.  
    96.     delay(2000);
    97.    
    98.   lcd.clear();
    99.  
    100.   lcd.setCursor(0, 0);
    101.  
    102.   lcd.print("Body Temp:");
    103.  
    104.   lcd.setCursor(0, 1);
    105.  
    106.   lcd.print("Heart BPM:");
    107.  
    108. }
    109. void loop()
    110. {      
    111.  
    112.    // print result to lcd display
    113.  
    114.  
    115.  
    116.    // Body Temp Calculations to display
    117.    
    118.    
    119.    
    120.   tempC = analogRead(tempPin);                  //read the value from the sensor
    121.   tempC = (5.0 * tempC * 100.0)/1024.0;    //convert analog to temp
    122.   tempF = ((tempC*9)/5) + 32;                   //convert celcius to farenheit
    123.  
    124.   lcd.setCursor(10, 0);
    125.   lcd.print(tempC,1);
    126.   lcd.print((char)223);                          // The degrees symbol 0xDF in Hex
    127.   lcd.print("F");
    128.  
    129.  
    130.  
    131.  
    132. //BPM DISPLAY
    133.  
    134. BPM = digitalRead(BPMpin);
    135.  
    136.  
    137. // Calculation will go here!!!!
    138.  
    139. lcd.setCursor(10, 1);
    140. lcd.print(BPM);
    141.  
    142.  
    143. // Blinking Cursor just for looks :)
    144.  
    145.     lcd.setCursor(14,1);
    146.     lcd.blink();
    147.    
    148.  
    149.  
    150.  
    151.  
    152.   delay(1000);
    153.  
    154.  
    155. }
    156.  
     
    Last edited by a moderator: Jun 1, 2012
  2. Markd77

    Senior Member

    Sep 7, 2009
    2,803
    594
    What I would suggest is timing the interval between each beat, then take 1/time = frequency. If your time is in seconds then multiply frequency by 60 to get BPM.
    Some averaging is going to be important, with a weighted average if you want faster responsiveness.
     
  3. dyeraaron

    Thread Starter Active Member

    Oct 27, 2008
    57
    0
    I have just been so bogged down and obsessed with figuring this out...I have sat in front of a monitor with arduino compiler now for 2 days straight...

    so on my oscope, my pulses are 500 m S apart...so my frequency = 1.25 Hz

    so taking 1.25 Hz * 60 seconds = 75 BPM in this case....

    so I don't want to declare BPM a float because I don't want decimals, but I want the BPM: to be fluxuating due to the input...obviously my heart beat isn't ALWAYS going to be 75 BPM and with my finger off I would want it to say 0...

    so in my code:

    // BPM display

    BPM = digitalRead( BPMpin) ;


    // formula for the calculation ???

    lcd.setCursor(10, 1); // shows the BPM on this column on LCD

    lcd.print(BPM);


    if (BPMpin ==0)

    {
    lcd.setCursor(10, 1);

    lcd.print(0);

    }




    I just don't know how to write a good enough code that WILL WORK for the formula portion...

    I'm not a great programmer by trade.
     
  4. justtrying

    Active Member

    Mar 9, 2011
    329
    343
    Whether this will help you, I do not know. I've been down the same road before. With Arduino, when input frequency fluctuates, you need to deal with millis or use external interrupts to accurately record your BPM. I had a similar problem with a different project, millis worked for me, but I think yours needs interrupts. Check out this link http://randomcontent.wolfnexus.net/RandomSite/arduino-hrm/ it is similar to what you are describing, I think.

    Also, you could look at peak detect - each beat has a peak, measure millis between consecutive beats, that is your period. Than you will be able to accurately calculate fluctuating BPMs. That is how I would do it.

    p.s. I found myself staring at the code for 2 weeks ;) when I was doing my project
     
    Last edited: Jun 1, 2012
  5. Markd77

    Senior Member

    Sep 7, 2009
    2,803
    594
    For a start, 500ms apart pulses is 2Hz, I guess that's a typo.
    To avoid floats (a good idea):
    BPM=60000/time(ms)
    It's 16 bit integer division, not sure how long that takes on an arduino, it's under 250 instruction cycles on a PIC.
     
Loading...