2017-04-06 130 views
-3

在此先感谢您的帮助。多个if语句和

所以我正在研究压力传感器。这个想法是,当压力传感器达到某个值时,比如10 AND然后在达到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. 
+4

你的代码看起来确实是错误的 - 也许格式化?那么我们实际上可以看看发生了什么。 –

+2

请你可以编辑你的文章来重新格式化代码。现在阅读太难了,这会减少你获得答案的机会。 –

+0

我很抱歉,我在我的手机上发了这篇文章。我可以纠正它。 – cou

回答

0

我已编辑的代码,以反映你正在尝试做的。

基本上你需要检查你的压力是增加还是减少。 因此,您可以读取两个读数并进行比较;一个阅读是在另一个之前进行的。如果最近的读数比前一次更高,那么它不会发生报警。

如果最近的读数小于以前那么压力就会下降。所以,引起报警,但不仅仅是...

警报响起之前需要满足两个条件,压力需要在下降的路径上,压力需要低于阈值。

千帕必须走下去,并低于阈值:

(val < previous && val < threshold)

看看这个作品,对不起,我冲了一点,并没有测试它在所有:

#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. 
int val = 0; // store value 
int previous; // previous reading 

void setup() 
{ 
    pinMode(ledPin, OUTPUT); // initialize the LED pin as an output: 
    pinMode(buzzer, OUTPUT); // Set buzzer - pin 9 as an output 
    Serial.begin(9600); // initialize serial communications: 
    lcd.begin(16, 2); 
    lcd.print ("Pressure in kpa"); 
} 

void loop() { 
    float sensorValue = analogRead(analogPin); // read the value of the pressure sensor: 
    int kpa = ((sensorValue * 5/1024) * (15/5) * (6.894/1) - 10); 

    // we want to check if this is going up or down. 
    previous = val; 
    val = kpa; 

    if (val > previous) { 
    digitalWrite(ledPin, HIGH); 
    delay(1000); 
    digitalWrite(ledPin, HIGH); // make LED flashy-flashy if its going up 
    delay(1000); 
    } 
    else if (val < previous && val < threshold) { 
    digitalWrite(ledPin, LOW); // ... and we switch off the LED 
    tone(buzzer, 1000);  // WEE WAH WEE WAH WEE WAH 
    delay(1000); 
    } 

} 

我在代码中添加了闪烁的LED,因为闪烁的LED只是更好。 我也使用了else...if声明。

希望这会起作用。

干杯, Ingwe。

+0

非常感谢您的重新格式化。我正在通过我的手机进行此操作。 kpa是正确的。第一个被注释掉了。你是对的,我试过了,我其实并不需要“digitalWrite等” – cou

+0

我看到了,我想这可能是这种情况。真棒,你得到它的工作。 – Ingwe

+0

但是,这仍然不能回答我最初的问题Ingwe。我几乎认为这是IGWE – cou