2017-04-06 6 views
-3

ご協力いただきありがとうございます。複数のif文と

私は圧力センサーに取り組んでいます。考え方は、圧力センサーが一定の値(例えば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. 
+4

あなたのコードは間違っているようです - おそらくフォーマットしていますか?実際に何が起こっているのかを実際に見ることができます。 –

+2

あなたの投稿を編集してコードを再フォーマットできますか?現状のままで読むのは難しいので、答えを得るチャンスは減ります。 –

+0

私はお詫びします、私は私の電話でこのポストを作った。私はそれを修正することができます。 – cou

答えて

0

以下は私のコードです。

あなたの圧力が増減しているかどうかを確認する必要があります。 これは2つの読みを取って比較することができます。一方の読みは他方の前に読み込まれます。最新の読み取り値が前回の読み取り値よりも高い場合は、アラームの原因はありません。

最新の読み値がそれ以前の値より小さければ、圧力は下がります。したがって、アラームの原因はまだありません。

アラームが鳴るには2つの条件が満たされる必要があります。圧力は下向きの経路にあり、圧力はしきい値以下にする必要があります。

KPA下って行くと、閾値以下でなければなりません。

(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ステートメントも使用しました。

これがうまくいきます。

乾杯、

+0

大変ありがとうございます。私は私の電話でこれをやっていた。 kpaは正しいです。最初のものはコメントアウトされました。あなたは正しいです、私はそれを試し、私は実際には "digitalWriteなど"が必要ではなかった – cou

+0

私はこれが事実かもしれないと思った。素晴らしいですね。 – Ingwe

+0

しかし、それは私の最初の質問Ingweにまだ答えていません。私はほとんどそれがIGWEだと思った。 – cou