Help with Arduino Code

Thread Starter

dyeraaron

Joined Oct 27, 2008
57
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....

Rich (BB code):
/*
  Temperature Sensor, Heart Beat monitor and GPS
  
*/
 
#include <LiquidCrystal.h>                      // include LCD library
 
 
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);         
 
// initialize the LCD with the numbers of the interface pins from the Arduino   

//declare variables

float tempC;
 
float tempF;
 
int BPM;
 
 
 

 
// Analog Inputs for ADC
int tempPin = A0;

// Digital Inputs
int BPMpin = 2;
 

void setup()
{
  
  pinMode (tempPin, INPUT);
  
  pinMode (BPMpin, INPUT);
   

  // set up the LCD's number of columns and rows:
 
 
  lcd.begin(16,2);
  
  lcd.print("Initializing....");
  
  lcd.setCursor(0, 1);
  
  lcd.print(".");
  
    delay(1000);
  
  lcd.setCursor(1, 1);
  
  lcd.print(".");
  
    delay(1000);
    
  lcd.setCursor(2, 1);
  
  lcd.print(".");
  
    delay(1000);
    
  lcd.setCursor(3, 1);
  
  lcd.print(".");
  
    delay(1000);
    
  lcd.setCursor(4, 1);
  
  lcd.print(".");
  
    delay(1000);
    
  lcd.setCursor(5, 1);
  
  lcd.print(".");
  
    delay(1000);
    
  lcd.clear();
  
  lcd.setCursor(3, 0);
  
  lcd.print("Welcome To");
  
    delay(2000);
  
  lcd.setCursor(0, 1);
  
  lcd.print("Vital-Ballistics");
  
    delay(2000);
    
  lcd.clear();
  
  lcd.setCursor(0, 0);
  
  lcd.print("Body Temp:");
  
  lcd.setCursor(0, 1);
  
  lcd.print("Heart BPM:");
  
}
void loop()
{      
  
   // print result to lcd display
  
  
  
   // Body Temp Calculations to display
   
   
   
  tempC = analogRead(tempPin);                  //read the value from the sensor
  tempC = (5.0 * tempC * 100.0)/1024.0;    //convert analog to temp 
  tempF = ((tempC*9)/5) + 32;                   //convert celcius to farenheit
  
  lcd.setCursor(10, 0);
  lcd.print(tempC,1);
  lcd.print((char)223);                          // The degrees symbol 0xDF in Hex
  lcd.print("F");
  
  
  
  
//BPM DISPLAY
 
BPM = digitalRead(BPMpin);
 
 
// Calculation will go here!!!!

lcd.setCursor(10, 1);
lcd.print(BPM);

  
// Blinking Cursor just for looks :)

    lcd.setCursor(14,1);
    lcd.blink();
   
  
  
 
  
  delay(1000);
  
  
}
 
Last edited by a moderator:

Markd77

Joined Sep 7, 2009
2,806
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.
 

Thread Starter

dyeraaron

Joined Oct 27, 2008
57
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.
 

justtrying

Joined Mar 9, 2011
439
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:

Markd77

Joined Sep 7, 2009
2,806
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.
 
Top