jeudi 6 avril 2017

Multiple if statements and

Thanks in advance for your help.

So I'm working on a pressure sensor. The idea is that when the pressure sensor gets to a certain value, say 10 AND then decreases sharply after it has gotten to 10, activate the buzzer. (So it's like, I pump my car tire to 10 and I want to know if there is a leak)

I just for the life of me can not make this happen. I know I'm just missing something very small but I will greatly appreciate any help.

Below is my code:

    #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.

Aucun commentaire:

Enregistrer un commentaire