Need help with if else statement for heart rate monitor project.

Hi all. Programming is not my strong point. I know the if else statement is fairly basic but I am having some trouble with it. I am building a heart monitor with a warning system. The warning system consists of an LCD, one red LED and a Piezo buzzer. What I want the if else statement to do is - if the heart rate is less than 140 then the LCD will display Regular Heart Rate, the LED and buzzer will stay off. Else the LCD will display WARNING! Irregular Heart Rate. The LED and buzzer will flash and beep at the same rate. Here is the code I have so far. Any help will be greatly appreciated. thank you in advance.

#include "Wire.h"
#include "hrmi_funcs.h"
#include <LiquidCrystal.h>


#define HRMI_HOST_BAUDRATE 9600
#define HRMI_I2C_ADDR      127

#define MAX_IN_BUFFSIZE 16

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);


char serInStr[MAX_IN_BUFFSIZE];   // Serial input string array
int numEntries = 0;               // Number of HR values to request
int numRspchars;                  // Number of Response chars to read
char i2cRspArray[34];                // I2C response array, sized to read 32 HR values
char hrmi_addr = HRMI_I2C_ADDR;   // I2C address to use
int led = 13;



void setup()
{
 // Initialize the I2C communication
 hrmi_open();

 // Initialize the serial interface
 Serial.begin(HRMI_HOST_BAUDRATE);
 
 lcd.begin(16, 2);
  pinMode(led, OUTPUT); 
  pinMode(9, OUTPUT);
 
 
}


/*
* Arduino main code loop
*/
void loop()
{
 // Request a set of heart rate values
 hrmiCmdArg(hrmi_addr, 'G', (char) numEntries);
     
 // Get the response from the HRMI
 numRspchars = numEntries + 2;
 if (hrmiGetData(hrmi_addr, numRspchars, i2cRspArray) != -1) {
   // send the results back on the serial interface in ASCII form
   Serial.print("Request "); Serial.print(numEntries); Serial.print(" => ");
   for (int i=0; i<numRspchars; i++) {
     Serial.print(i2cRspArray[i], DEC);
     Serial.print(" ");
   }
   Serial.println();
  }
   
 // Setup to do it again
 if (++numEntries > 30)
     numEntries = 0;
 delay(1000);                  // Delay 1 second between commands
 
 if (???? < 140){
   digitalWrite(led, LOW);
   lcd.setCursor(0,1);
   lcd.print("Regular Heart Rate"); 
 }
 
 else{
   lcd.setCursor(0,0);
   lcd.print("WARNING!");
   lcd.setCursor(0,0);
   lcd.print("Irregular Heart Rate");
   digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
   delay(200);               // wait for a second
   digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
   delay(200);               // wait for a second
   analogWrite(9,128);
   delay(500);
   digitalWrite(9, LOW);
   delay(200);
 
 }

Thank you.

Hi, what is the problem you are experiencing with the code?
What is ???? supposed to mean?

Tom........ :slight_smile:

Thanks for replying Tom. Some if the heart rate code isn't my own. So I am not sure what to put in where the ???? are. I have tried different things but nothing seems to work. I don't know what to put in to the if statement for the heart rate reading.
Thanks.

I think the problem is that you haven't written any code to actually obtain the heart rate. It's pointless trying to use the heart rate to control something until you have got the heart rate.

As a minor point, you seem to be outputting two messages in the same position on your display, which means the second one will overwrite the first one. Perhaps you intended them to be displayed on different lines. Also, you only clear one line of the display when the alarm condition is absent (and it's not the line you're currently displaying your messages on). You probably want to clear both lines.

Thanks Peter. I am getting heart rate readings on the serial monitor. I meant to have the second message on the display as (0,1). Thanks I will add the clear display lines now.

i was just wondering if you could reply with the updated code? i am trying to do almost the exact same thing and if it works that would be very helpful!