I'm making something that detects an input from a flow meter. It pulses 4.5 times in a second. If water flows through, I want it to trigger my system. When it's activated, it waits 20 seconds and then it checks if water's still flowing. If water is still flowing through the flow meter, I want it to activate the LED. If the water has stopped flowing through the flow meter, the LED will get switched off. I think the problem I have may be due to the fact that the flow meter pulses. I simply want to know whether water is flowing through or not. How can I change the code to reflect this?
I need to finish this soon, if you could show me what's wrong and show me what it should say instead, I'd really appreciate it. Thank you!
const int sensorPin = 2; // the digital pin which has the sensor - this may need to be an analog pin?
const int ledPin = 3; // the number of the LED pin
// variables will change:
int sensorState = 0; // variable for reading the sensor status
void setup() {
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the sensor pin as an input:
pinMode(sensorPin, INPUT);
}
void loop() {
// read the state of the sensor value:
sensorState = digitalRead(sensorPin);
// check if the sensor is activated.
// if it is, the sensorState is HIGH:
if (sensorState == HIGH) {
delay(20000); // delay for 20 seconds before reading again
if (sensorState == HIGH) {
// turn LED on:
digitalWrite(ledPin, HIGH);
} else {
// turn LED off:
digitalWrite(ledPin, LOW);
}
}
}
I don't normally post a complete solution here but the original post has a good specification and a good attempt at making it work. The code below compiles but it hasn't been tested as I don't have a suitable pulse-maker close to hand.
const int sensorPin = 2; // the digital pin which has the sensor - this may need to be an analog pin?
const int ledPin = 3; // the number of the LED pin
const unsigned long NO_FLOW_TIMEOUT = 300; //milliseconds. If we don't get a pulse within this time, consider that flow has stopped.
//Make this number just a bit longer than the expected pulse interval at the lowest flow.
const unsigned long WAIT_PERIOD = 20000; //milliseconds. The amount of time that flow must exist before turning on the output LED.
// variables will change:
unsigned long LastPulseDetected = 0;
unsigned long FirstFlowDetected = 0;
void setup() {
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the sensor pin as an input:
pinMode(sensorPin, INPUT);
}
void loop() {
// read the state of the sensor value:
int sensorState = digitalRead(sensorPin);
// check if the sensor is currently sending an active pulse.
// if it is, the sensorState is HIGH:
if (sensorState == HIGH) {
if(millis() - LastPulseDetected > NO_FLOW_TIMEOUT) {
//flow has just started
FirstFlowDetected = millis();
} else {
//pulse arrived within timeout, flow is still flowing
}
LastPulseDetected = millis(); //record that we got a pulse
}
if(millis() - LastPulseDetected < NO_FLOW_TIMEOUT && millis() - FirstFlowDetected > WAIT_PERIOD) {
//flow is still flowing and it has been flowing for more than the wait period
digitalWrite(ledPin, HIGH);
} else {
digitalWrite(ledPin, LOW);
}
}