Hi, I want to know if it would be possible to code a pressure sensor to work with low pressure. (i.e. when pressure is released, PCB is turned on and stays on until pressure is applied in which a countdown is initiated and turned off). If so, how can I begin to approach this I want my pressure sensor to power an amplifier circuit, led, and microcontroller. Thanks for the help!
No problem. You can buy pressure switches, which are just switches, and can be used to control power to your setup.
Otherwise you can use an Arduino to read a pressure sensor and control the power to other devices. Obviously, the Arduino has to be powered in order to read the sensor.
Here is an example using an analog pressure sensor. The sensor is simulated with a potentiometer and the pump by an LED. The pump (LED) will turn on if the the output from the sensor (pot) goes above 700 (upperThreshold) and remain on till the sensor output falls below 300 (lowerThreshold). The pump will remain off till the sensor output once again increases to over 700.
Is this what you want the code to do?
const byte potPin = A0;
const byte ledPin = 7;
int lowerThreshold = 300;
int upperThreshold = 700;
void setup()
{
Serial.begin(115200);
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW);
}
void loop()
{
// BWOD timer so as not to flood serial monitor
// see below
static unsigned long timer = 0;
unsigned long interval = 200;
if (millis() - timer >= interval)
{
timer = millis();
int potValue = analogRead(potPin);
Serial.print("pot value = ");
Serial.println(potValue);
if (potValue <= lowerThreshold)
{
digitalWrite(ledPin, LOW);
Serial.println("Pump off ");
}
else if (potValue >= upperThreshold)
{
digitalWrite(ledPin, HIGH);
Serial.println("Pump on ");
}
}
}
Some non-blocking timing tutorials:
Blink without delay() (BWOD).
Blink without delay detailed explanation
Beginner's guide to millis().
Several things at a time.
Thanks! It is actually the opposite, however. I want the pump (LED) to turn on until it reaches the upper threshold (700 in the example) and then initiate a turn-off countdown once that threshold is surpassed. Thankfully, your code shows how I can easily switch the threshold properties and achieve this, so thank you for this wonderful example.
You are welcome.
Hopefully the non-blocking tutorials will help you to write the countdown without blocking so that the code can instantly respond to changes in the input.
If your question was answered to your satisfaction, please mark the thread as solved so that other members do not waste their time opening the thread wanting to help only to find that the problem has been solved.
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.