Click here to Skip to main content
15,889,876 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I'm doing a project using Arduino Uno, LM35 temperature sensor, and a pulse sensor to read the temperature and the BPM and display them on the LCD and serial monitor. First, I wrote the code for each sensor separately and there was no error and the reading looks correct and reliable. However, when I tried to combine the two codes, it doesn't show any errors but the reading looks not correct (like jumping, sometimes too high and sometimes low). So can you please help me. The code is attached below. Thanks in advance.

C#
<pre>#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
//#include <SoftwareSerial.h>
//float pulse = 0;
float temp ;//= 0;
//SoftwareSerial ser(9,10);
//String apiKey = "OO707TGA1BLUNN12";
int myBPM;
#define USE_ARDUINO_INTERRUPTS true // Set-up low-level interrupts for most acurate BPM math.
#include <PulseSensorPlayground.h> // Includes the PulseSensorPlayground Library.
 
// Variables
const int PulseWire = A0; // PulseSensor PURPLE WIRE connected to ANALOG PIN 0
const int LED7 = 7; // The on-board Arduino LED, close to PIN 13.
int Threshold = 550; // Determine which Signal to "count as a beat" and which to ignore.


PulseSensorPlayground pulseSensor; // Creates an instance of the PulseSensorPlayground object called "pulseSensor"


void setup()
{
lcd.begin(16, 2);
//pinMode(blinkPin,OUTPUT); // pin that will blink to your heartbeat!
//pinMode(fadePin,OUTPUT); // pin that will fade to your heartbeat!
Serial.begin(115200); // we agree to talk fast!
 
// analogReference(EXTERNAL);
//analogReference(INTERNAL);
lcd.clear();
lcd.setCursor(0,0);
lcd.print(" Patient Health");
lcd.setCursor(0,1);
lcd.print(" Monitoring ");
delay(4000);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Initializing....");
delay(5000);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Getting Data....");

pulseSensor.analogInput(PulseWire);
pulseSensor.blinkOnPulse(LED7); //auto-magically blink Arduino's LED with heartbeat.
pulseSensor.setThreshold(Threshold);
 
// Double-check the "pulseSensor" object was created and "began" seeing a signal.

}
// Where the Magic Happens
void loop()
{

pulse();
read_temp();
lcd.clear();
lcd.setCursor(0,0);
lcd.print("BPM :");
lcd.setCursor(7,0);
lcd.print(myBPM);
}


void pulse(){
 if (pulseSensor.begin()) {
Serial.println("We created a pulseSensor Object !"); //This prints one time at Arduino power-up, or on Arduino reset.
lcd.setCursor(0,0);
lcd.print(" Heart Rate ");
lcd.setCursor(0,1);
lcd.print("Monitor");
 
 
  int myBPM = pulseSensor.getBeatsPerMinute(); // Calls function on our pulseSensor object that returns BPM as an "int".
// "myBPM" hold this BPM value now.
if (pulseSensor.sawStartOfBeat()) { // Constantly test to see if "a beat happened".
Serial.println("♥ A HeartBeat Happened ! "); // If test is "true", print a message "a heartbeat happened".
Serial.print("BPM: "); // Print phrase "BPM: "
Serial.println(myBPM); // Print the value inside of myBPM.
lcd.clear();
lcd.setCursor(0,0);
//lcd.print("HeartBeat Happened !"); // If test is "true", print a message "a heartbeat happened".
//lcd.setCursor(5,3);
lcd.print("BPM: "); // Print phrase "BPM: "
lcd.print(myBPM);
}
delay(500); // considered best practice in a simple sketch.
  }}



void read_temp()
{
int temp_val = analogRead(A5);
float mv = ((temp_val+0.5)/1024.0)*5000;
float cel = mv/10;
temp=cel;
//temp = (cel*9)/5 + 32;
Serial.print("Temperature:");
Serial.print(temp);
Serial.print("\xC2\xB0");
Serial.println("C");

lcd.setCursor(0,1);
lcd.print("Temp.:");
lcd.setCursor(7,1);
lcd.print(temp);
lcd.setCursor(13,1);
lcd.print((char)223);
lcd.print("C");
delay(500);
}


What I have tried:

I want to read two sensors values (LM35 temp sensor and pulse sensor) and display them on an LCD and the serial monitor.
Posted
Updated 25-May-23 20:24pm
v3
Comments
Richard MacCutchan 26-May-23 2:41am    
You have removed all the detail that may be relevant to the problem. Are we to assume that you fixed it?
Richard Deeming 26-May-23 3:41am    
Removing the details of your question after it has been answered is extremely rude. I have rolled back your destructive edit.

1 solution

You have defined int myBPM in two places, and are printing the two different values. You print the locally defined value in the pulse function, and then the global value in the loop function.
 
Share this answer
 
Comments
CPallini 25-May-23 8:00am    
Good catch!
Richard MacCutchan 25-May-23 8:05am    
Thanks, I wash it was this easy when I try to debug my own code.
OriginalGriff 25-May-23 8:29am    
That's because you read what you meant to write.

So do I ... :sigh:
CPallini 25-May-23 8:56am    
So true.
CPallini 25-May-23 8:56am    
Because your code is way more contrived. :-D :-D :-D
(kidding)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900