【解決方法】Arduino 上で LM35 とパルスセンサーのコードを組み合わせると不正確な読み取り値が発生する原因は何ですか?また、それを修正するにはどうすればよいですか?

プログラミングQA


Arduino Uno、LM35温度センサー、パルスセンサーを使用して温度とBPMを読み取り、LCDとシリアルモニターに表示するプロジェクトを行っています。 まず、各センサーのコードを個別に作成しましたが、エラーはなく、読み取り値は正確で信頼できるようです。 ただし、2 つのコードを結合しようとすると、エラーは表示されませんが、読み取り値が正しくないように見えます (ジャンプしたり、高すぎる場合と低すぎる場合があります)。 それで、助けていただけませんか。 コードは以下に添付されています。 前もって感謝します。

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);
}

私が試したこと:

I want to read two sensors values (LM35 temp sensor and pulse sensor) and display them on an LCD and the serial monitor.

解決策 1

あなたが定義しました int myBPM 2 か所にあり、2 つの異なる値が出力されています。 ローカルに定義された値を pulse 関数、次にグローバル値 loop 関数。

コメント

タイトルとURLをコピーしました