【发布时间】:2017-04-06 09:42:17
【问题描述】:
提前感谢您的帮助。
所以我正在研究压力传感器。这个想法是,当压力传感器达到某个值时,比如 10,然后在达到 10 后急剧下降,激活蜂鸣器。 (就像,我把我的汽车轮胎打到 10,我想知道是否有泄漏)
我只是为了我的生活不能让这发生。我知道我只是错过了一些非常小的东西,但我将非常感谢任何帮助。
下面是我的代码:
#include <LiquidCrystal.h> LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
// These constants won't change: const int analogPin = A0; // pin that the sensor is attached to const int ledPin = 13; // pin that the LED is attached to const int threshold = 5; // an arbitrary threshold level that's in the range of the analog input const int buzzer = 9; // pin that the buzzer is connected to.
void setup() { // initialize the LED pin as an output: pinMode(ledPin, OUTPUT); // initilizes the pin as an output. pinMode(buzzer, OUTPUT); // Set buzzer - pin 9 as an output
// initialize serial communications: Serial.begin(9600); lcd.begin(16, 2); lcd.print ("Pressure in kpa"); }
void loop() { // read the value of the pressure sensor: float sensorValue = analogRead(analogPin); // int kpa = (((sensorValue*5)/1024)*(120/5)-116); // 116 was subtracted to equilibrate the sensor int kpa = ((sensorValue * 5 / 1024) * (15 / 5) * (6.894 / 1) - 10); // // if the analog value is greater than whatever threshold we use , turn on the LED:
if (kpa > threshold )
{
digitalWrite(ledPin, HIGH);
digitalWrite(buzzer, HIGH);
tone(buzzer, 1000); // this controls the picth of the sound. (Hz)
delay(1000); } else {
digitalWrite(ledPin, LOW);
digitalWrite(buzzer, LOW);
noTone(buzzer); // Stop sound...
delay(1000); }
// print the analog value: Serial.println(kpa); delay(1); // delay in between reads for stability lcd.setCursor (0, 1); lcd.print (kpa);
}
Thanks.
【问题讨论】:
-
您的代码似乎真的错了——可能是格式化?那么我们就可以实际看看发生了什么。
-
请您编辑您的帖子以重新格式化代码。按原样阅读太难了,这会降低你得到答案的机会。
-
抱歉,我确实在手机上发过这篇文章。我可以改正。
标签: c++ if-statement arduino pressure