HELP REVIEW MY ARDUINO UNO SKETCH FOR A MONITOR USING LM35 AND KY039

Hello everyone,

I am making an arduino based vital signs monitor for my final project. I have managed to get some useful sketches which I have made an integrated sketch from. I have used an LM35 sketch and KY039 basic sketch. And have also added a switch which will switch the KY039 on (not sure if this is necessary). I have added a 16x2 lcd to display the temperature and heart rate simultaneously. However I am not sure about how i have arranged my void loop section. I would like to know if my code is alright. I would like to be able to see the heart rate and temperature on both lines 0 and 1 at the same time. I'd appreciate any feedback.

/* This is the sketch for a vital signs monitor intended to senses temperature and heart rate*/
#include <LiquidCrystal.h> //Include LCD library
LiquidCrystal lcd(12, 11, 5, 4, 3, 2); //Connections to arduino pins

/*define*/

int tempPin = A1; 
int TEMPERATURE = 0;
// Pulse Monitor Test Script
int HBSensor = 8;//the heart beat sensor is connected to pin 8 of arduino uno
double alpha = 0.75;
int period = 100;
double change = 0.0;
double minval = 0.0;
int run;//for pushbutton
int LED = 7;// LED connected on this output pin

/* void setup*/

void setup() 
{
  /*temp setup*/
  Serial.begin(9600);
  
  /*lcd setup*/
  
  lcd.begin(16, 2);// lcd display 
  run = 0; //starts stopped pushbutton
  pinMode(HBSensor, INPUT); //heartbeat sensor
  pinMode (tempPin,INPUT); //temperature sensor
  pinMode (LED,OUTPUT);
  lcd.clear();//clears LCD display
  lcd.print("Current HB : "); //will display the current hb
  lcd.setCursor(0,0);//first line of lcd
  lcd.print("HB/Min : 0.0 ");
  lcd.setCursor(0,1);//second line of lcd
  lcd.print("Temp: "); //will display the current temperature reading
  lcd.print ("C");//celcius
   
  }

void loop() 
   {
    //instead of having one big loop i divided the multiple loop into three loops with different names which are
    
    loopBUTTON(); 
    loopHR();
    loopTEMP();
   }
   
 void loopBUTTON ()
//void loop 1 for pushbutton
  {
     if(run == 0)
     {
         run = 255;
     }
     else
     {
         run = 0;
     }
  
      if(run > 0)
     {
     //code you only run if button was pressed, stops running when button pressed again, so forth...
     }
  }

  //void loop 2 for heartbeat
  void loopHR ()
      {
     static double oldValue = 0;
    static double oldChange = 0;
 
    int rawValue = analogRead (HBSensor);//This reads in the value from the analog pin. this is a 10 bit number, and will be between 0 and 1023
                                                              // If this value doesn't change, you've connected up
                                                              // something wrong
    double value = alpha * oldValue + (1 - alpha) * rawValue;// Calculate an average using 75% of the
                                                                // previous value and 25% of the new 

  
          Serial.print("HB/Min = ");
          Serial.print (rawValue);
          Serial.print (",");//comma 
          Serial.println (value);//send out average value and go to next line
          oldValue = value;//save average for next iteration
          delay (2000);//wait 2 seconds
          lcd.setCursor(0,0);//print results 
          lcd.print(value);
          lcd.print(" ");
        }
        
//void loop 3
void loopTEMP ()
{
int val;
val = analogRead(tempPin);//READ THE A1 PIN
float mv = ( val/1024.0)*5000; //MANIPULATE THE VALUE
float cel = mv/10;//DIVIDE BY 10
Serial.print("TEMPERATURE =  ");
Serial.print (cel);
Serial.print ("C");
lcd.setCursor(0,1);
lcd.print ("TEMPERATURE");
lcd.print (cel);
lcd.print ("*C");
Serial.println("");
delay (5000);
lcd.setCursor(0,1);
lcd.print("Press Button again.");

if(cel >= 39)
{
//blinks at 5ms intervals for 10 seconds
digitalWrite(LED, HIGH);

delay(500);

digitalWrite(LED, LOW);

delay(500);

digitalWrite(LED, HIGH);

delay(500);

digitalWrite(LED, LOW);

delay(500);

digitalWrite(LED, HIGH);

delay(500);

digitalWrite(LED, LOW);

delay(500);

digitalWrite(LED, HIGH);

delay(500);

digitalWrite(LED, LOW);

delay(500);

digitalWrite(LED, HIGH);

delay(500);

digitalWrite(LED, LOW);

delay(500);

digitalWrite(LED, HIGH);

delay(500);

digitalWrite(LED, LOW);

delay(500);

digitalWrite(LED, HIGH);

delay(500);

digitalWrite(LED, LOW);

delay(500);

digitalWrite(LED, HIGH);

delay(500);

digitalWrite(LED, LOW);

delay(500);

digitalWrite(LED, HIGH);

delay(500);

digitalWrite(LED, LOW);

delay(500);

digitalWrite(LED, HIGH);

delay(500);

digitalWrite(LED, LOW);

delay(500);

}

digitalWrite(LED, LOW);

  if(cel <= 39)
  { 
    digitalWrite(LED,LOW); //switch LED off
  }

 
}

The main problem I see with calling your other functions "loop<somethingOrOther" is that they don't loop.

The other problem I see are all those delays, which do loop, pointlessly.

Where do you actually read the button switch? Don't you need to set the led pin to output?

All of those digitalWrites and delays can be replaced by a for loop.

groundFungus:
All of those digitalWrites and delays can be replaced by a for loop state machine.

groundFungus:
Where do you actually read the button switch? Don't you need to set the led pin to output?

All of those digitalWrites and delays can be replaced by a for loop.

I forgot to add those sorry. Hope you can read through this..

/* This is the sketch for a vital signs monitor intended to senses temperature and heart rate*/
#include <LiquidCrystal.h> //Include LCD library
LiquidCrystal lcd(12, 11, 5, 4, 3, 2); //Connections to arduino pins

/define/

int tempPin = A1;
int TEMPERATURE = 0;
// Pulse Monitor Test Script
int HBSensor = 8;//the heart beat sensor is connected to pin 8 of arduino uno
double alpha = 0.75;
int period = 100;
double change = 0.0;
double minval = 0.0;
int run;//for pushbutton
int LED = 7;// LED connected on this output pin
int HBStart = 6; //button is connected to pin 6 on arduino

/* void setup*/

void setup()
{
/temp setup/
Serial.begin(9600);

/lcd setup/

lcd.begin(16, 2);// lcd display
run = 0; //starts stopped pushbutton
pinMode(HBSensor, INPUT); //heartbeat sensor
pinMode(HBStart, INPUT_PULLUP);//for pushbutton
pinMode (tempPin,INPUT); //temperature sensor
pinMode (LED,OUTPUT);
lcd.clear();//clears LCD display
lcd.print("Current HB : "); //will display the current hb
lcd.setCursor(0,0);//first line of lcd
lcd.print("HB/Min : 0.0 ");
lcd.setCursor(0,1);//second line of lcd
lcd.print("Temp: "); //will display the current temperature reading
lcd.print ("C");//celcius

}

void loop()
{
//instead of having one big loop i divided the multiple loop into three loops with different names which are

loopBUTTON();
loopHR();
loopTEMP();
}

void loopBUTTON ()

//void loop 1 for pushbutton
{
if(run == 0)
{
run = 255;
}
else
{
run = 0;
}

if(run > 0)
{
//code you only run if button was pressed, stops running when button pressed again, so forth...
}
}

//void loop 2 for heartbeat

void loopHR ()
{
static double oldValue = 0;
static double oldChange = 0;

int rawValue = analogRead (HBSensor);//This reads in the value from the analog pin. this is a 10 bit number, and will be between 0 and 1023
// If this value doesn't change, you've connected up
// something wrong
double value = alpha * oldValue + (1 - alpha) * rawValue;// Calculate an average using 75% of the
// previous value and 25% of the new

Serial.print("HB/Min = ");
Serial.print (rawValue);
Serial.print (",");//comma
Serial.println (value);//send out average value and go to next line
oldValue = value;//save average for next iteration
delay (2000);//wait 2 seconds
lcd.setCursor(0,0);//print results
lcd.print(value);
lcd.print(" ");
}

//void loop 3
void loopTEMP ()
{
int val;
val = analogRead(tempPin);//READ THE A1 PIN
float mv = ( val/1024.0)*5000; //MANIPULATE THE VALUE
float cel = mv/10;//DIVIDE BY 10
Serial.print("TEMPERATURE = ");
Serial.print (cel);
Serial.print ("C");
lcd.setCursor(0,1);
lcd.print ("TEMPERATURE");
lcd.print (cel);
lcd.print ("*C");
Serial.println("");
delay (5000);
lcd.setCursor(0,1);
lcd.print("Press Button again.");

if(cel >= 39)
{
//blinks at 5ms intervals for 10 seconds
digitalWrite(LED, HIGH);

delay(500);

digitalWrite(LED, LOW);

delay(500);

digitalWrite(LED, HIGH);

delay(500);

digitalWrite(LED, LOW);

delay(500);

digitalWrite(LED, HIGH);

delay(500);

digitalWrite(LED, LOW);

delay(500);

digitalWrite(LED, HIGH);

delay(500);

digitalWrite(LED, LOW);

delay(500);

digitalWrite(LED, HIGH);

delay(500);

digitalWrite(LED, LOW);

delay(500);

digitalWrite(LED, HIGH);

delay(500);

digitalWrite(LED, LOW);

delay(500);

digitalWrite(LED, HIGH);

delay(500);

digitalWrite(LED, LOW);

delay(500);

digitalWrite(LED, HIGH);

delay(500);

digitalWrite(LED, LOW);

delay(500);

digitalWrite(LED, HIGH);

delay(500);

digitalWrite(LED, LOW);

delay(500);

digitalWrite(LED, HIGH);

delay(500);

digitalWrite(LED, LOW);

delay(500);

}

digitalWrite(LED, LOW);

if(cel <= 39)
{
digitalWrite(LED,LOW); //switch LED off
}

}

TolpuddleSartre:
The main problem I see with calling your other functions "loop<somethingOrOther" is that they don't loop.

The other problem I see are all those delays, which do loop, pointlessly.

I don't know how to handle multiple loops. I got this method of renaming them on stack exchange. How else can I go about it?
And should I remove the delays?

If you want your sketch to be responsive (most people do) you have to get rid of delays.

My experience of stackexchange is that it is normally more sensible when naming functions.

If you want to be taken seriously, please start using code tags.

digitalWrite(LED, LOW);

  if(cel <= 39)
  { 
    digitalWrite(LED,LOW); //switch LED off
  }

Pretty pointless conditional.

And should I remove the delays?

Some links to methods for timing without delay().

https://forum.arduino.cc/index.php?topic=223286.0

https://forum.arduino.cc/index.php?topic=503368.0

https://www.arduino.cc/en/Tutorial/BlinkWithoutDelay

TolpuddleSartre:

digitalWrite(LED, LOW);

if(cel <= 39)
  {
    digitalWrite(LED,LOW); //switch LED off
  }


Pretty pointless conditional.

Thanks!
So apart from the delays, will the LCD print both temperature and heart rate simultaneously from the way i've structured the sketch?
Or will it display both values one after the other?

Does me placing the lcd.print statement at each different loop mean they'll be displayed one after the other? Or should I place all the lcd.print statements altogether?