-3

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.
cou
  • 5

1 Answers1

0

I have edited the code to reflect what you are trying to do.

Essentially you need to check if your pressure is increasing or decreasing. So for this you can take two readings and compare them; one reading is taken before the other. If the latest reading is higher than the previous then its going up, no cause for alarm.

If the latest reading is less that the previous then the pressure is going down. So, cause for alarm but not just yet...

TWO conditions need to be met before the alarm sounds, the pressure needs to be on a downward path AND the pressure needs to be below the threshold value.

kpa must be going down AND be below threshold:

(val < previous && val < threshold)

See if this works, sorry I rushed it a bit and haven't tested it at all:

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

}

I added a flashing LED to the code because flashing LEDs are just better. I also used an else...if statement.

Hopefully this will work.

Cheers, Ingwe.

Ingwe
  • 146
  • 13
  • Thank you very much for the reformat. I was doing this over my phone. The kpa is correct. The first one was commented out. You are correct, I tried it and I didn't actually need the "digitalWrite etc" – cou Apr 06 '17 at 11:16
  • I see, I figured this may be the case. Awesome you got it working. – Ingwe Apr 06 '17 at 11:20
  • However, that still doesn't answer my initial question Ingwe. I almost thought it was IGWE – cou Apr 06 '17 at 11:23
  • How do I edit my initial post. Sorry I'm new here I just can't find an edit button to my original question. – cou Apr 06 '17 at 11:26
  • I suspect I may not be following you then. Looking at your question again I notice the word 'sharply'. Now, in your code the buzzer will sound when the kpa is more than the threshold you defined. Are you wanting it to buzz when the kpa reaches 10 then drops below the defined threshold **within a certain period of time**? – Ingwe Apr 06 '17 at 11:27
  • No. So basically, we are pumping air into a car tire. So the pressure sensor is going to start as 0,1,3,5,6,7,8 etc. Now we know the tire gets full at 10kpa. We want to be able to know immediately when there is a leak. so the sensor is going to read something like (0,1,2,3,4,5,6,7,8,9, 10,(leaks) 9,8,7,6,5,4,3)-pressure drops. – cou Apr 06 '17 at 11:37
  • I will make an edit to my original answer, let me know if its what you are after. I think I understand. – Ingwe Apr 06 '17 at 11:39
  • Ok. so the buzzer comes on when there is a drop AFTER it has gotten to the 10 and decreased. Thanks – cou Apr 06 '17 at 11:42