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? Thanks!
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);
}
}
}
If the sensor pin goes high / low 4 or 5 times a second, then you'll need to count these pulses for 20 seconds, rather than just testing for high at the start of 20 seconds and at the end. So don't use delay(), but check the sensor continuously for 20 seconds.
I've updated the code. I'm pretty clueless about serial stuff and pulses so this is my attempt. I'd appreciate if any corrections were made, thanks.
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
// NEW
float calibrationFactor = 4.5;
volatile byte pulseCount;
float flowRate;
// 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);
// start the pulseCount at 0
pulseCount = 0;
}
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 (pulseCount == 1) {
delay(20000); // delay to avoid the next line being triggered as soon as pulseCount == 1 is triggered
if (pulseCount > 90) { // the amount of pulses in 20 seconds, as soon as it exceeds this, it triggers the LED
// turn LED on:
digitalWrite(ledPin, HIGH);
} else {
// resets the counter, if the pulseCount is less than 90, the system is safe and can be reset
pulseCount = 0;
}
}
}
First get some code that measures the flow rate and updates a variable. This code should
never call delay, it should be called frequently from loop() and do its stuff.
Then get some other code to implement a state-machine for the behaviour you want - sounds
like you want states like waiting-for-flow, flowing-for-less-than-20s, and flowing-for-more-than-20s.
The only connection between the flow rate code and the state machine logic code should be the
variable with the current flow rate.
One way to measure the flow rate is to measure the difference, diff in millis() between successive
low-to-high transitions on the flow meter pulse output, and store 1000/diff in your variable.
For a flow rate of 4.5 pulses a second you'd expect d = 222 approx.
Each part of the program should do one thing well, not try to do several things entangled.
I found another code online. How would I edit it for this purpose?
/*
byte statusLed = 13;
byte sensorInterrupt = 0; // 0 = digital pin 2
byte sensorPin = 2;
float calibrationFactor = 4.5;
volatile byte pulseCount;
float flowRate;
unsigned int flowMilliLitres;
unsigned long totalMilliLitres;
unsigned long oldTime;
void setup()
{
// Initialize a serial connection for reporting values to the host
Serial.begin(38400);
// Set up the status LED line as an output
pinMode(statusLed, OUTPUT);
digitalWrite(statusLed, HIGH); // We have an active-low LED attached
pinMode(sensorPin, INPUT);
digitalWrite(sensorPin, HIGH);
pulseCount = 0;
flowRate = 0.0;
flowMilliLitres = 0;
totalMilliLitres = 0;
oldTime = 0;
// The Hall-effect sensor is connected to pin 2 which uses interrupt 0.
// Configured to trigger on a FALLING state change (transition from HIGH
// state to LOW state)
attachInterrupt(sensorInterrupt, pulseCounter, FALLING);
}
/**
* Main program loop
*/
void loop()
{
if((millis() - oldTime) > 1000) // Only process counters once per second
{
// Disable the interrupt while calculating flow rate and sending the value to
// the host
detachInterrupt(sensorInterrupt);
// Because this loop may not complete in exactly 1 second intervals we calculate
// the number of milliseconds that have passed since the last execution and use
// that to scale the output. We also apply the calibrationFactor to scale the output
// based on the number of pulses per second per units of measure (litres/minute in
// this case) coming from the sensor.
flowRate = ((1000.0 / (millis() - oldTime)) * pulseCount) / calibrationFactor;
// Note the time this processing pass was executed. Note that because we've
// disabled interrupts the millis() function won't actually be incrementing right
// at this point, but it will still return the value it was set to just before
// interrupts went away.
oldTime = millis();
// Divide the flow rate in litres/minute by 60 to determine how many litres have
// passed through the sensor in this 1 second interval, then multiply by 1000 to
// convert to millilitres.
flowMilliLitres = (flowRate / 60) * 1000;
// Add the millilitres passed in this second to the cumulative total
totalMilliLitres += flowMilliLitres;
unsigned int frac;
// I reckon the main part would go here. I don't know how I'd code it though?
// Reset the pulse counter so we can start incrementing again
pulseCount = 0;
// Enable the interrupt again now that we've finished sending output
attachInterrupt(sensorInterrupt, pulseCounter, FALLING);
}
}
/*
Insterrupt Service Routine
*/
void pulseCounter()
{
// Increment the pulse counter
pulseCount++;
}