94Views5Replies

Author Options:

Arduino with flow sensor and relay Answered

Hello. I'm needing help on a project with a water flow sensor. Basically, I'm trying to activate a relay (to turn on a motor) when the Arduino board senses any water flow present for 15 seconds or more. I was expecting the relay to turn on after the flow sensor picked up water flow for 15 seconds. After I ran the code, I had water flowing through the flow sensor for 5 seconds and then I shut off the water. The relay still turned on 10 seconds later. I didn't want this to happen. Instead, I would like to have the relay turn on only after there is water flow for at least 15 seconds. Please review my code below. I would appreciate any help. Thank you!!!

byte sensorInterrupt = 0;

byte statusLed = 13;

byte sensorPin = 2;

const int threshold = 15000; // threshold

float calibrationFactor = 4.5;

volatile byte pulseCount;

float flowRate;

unsigned int flowMilliLitres;

unsigned long totalMilliLitres;

unsigned long oldTime;

unsigned int countstart = 0;

unsigned long starttime;

unsigned long endtime;

unsigned long elapsedtime;

void setup()

{

Serial.begin(9600);

pinMode(statusLed, OUTPUT);

digitalWrite(statusLed, HIGH); // We have an active-low LED attached

pinMode(sensorPin, INPUT);

digitalWrite(sensorPin, HIGH);

pinMode(7, OUTPUT);// connected to relay

pulseCount = 0;

flowRate = 0.0;

flowMilliLitres = 0;

totalMilliLitres = 0;

oldTime = 0;

attachInterrupt(sensorInterrupt, pulseCounter, FALLING);

}

void loop()

{

while (1){

if((millis() - oldTime) > 1000) // Only process counters once per second

{

detachInterrupt(sensorInterrupt);

flowRate = ((1000.0 / (millis() - oldTime)) * pulseCount) / calibrationFactor;

oldTime = millis();

flowMilliLitres = (flowRate / 60) * 1000;

totalMilliLitres += flowMilliLitres;

unsigned int frac;

Serial.print("Flow rate: ");

Serial.print(int(flowRate)); // Print the integer part of the variable

Serial.print("L/min");

Serial.print("\t"); // Print tab space

Serial.print("Output Liquid Quantity: ");

Serial.print(totalMilliLitres);

Serial.println("mL");

Serial.print("\t"); // Print tab space

Serial.print(totalMilliLitres/1000);

Serial.print("L");

pulseCount = 0;

attachInterrupt(sensorInterrupt, pulseCounter, FALLING);

}

if( totalMilliLitres > 0 && countstart == 0)

{

starttime = millis();

countstart = 1;

}

if ( totalMilliLitres > 0 )

{

endtime = millis();

elapsedtime = endtime - starttime;

}

if (elapsedtime >= threshold){

digitalWrite(7,HIGH); // Activate relay to turn on motor

delay(10000);

digitalWrite(7,LOW); // Deactivate relay to turn on motor

totalMilliLitres = 0;

countstart = 0;

elapsedtime = 0;

break;

}

else{

digitalWrite(statusLed, LOW); // Turn the LED off

digitalWrite(7, LOW);// turn relay OFF

}

}

}

void pulseCounter()

{

// Increment the pulse counter

pulseCount++;

}

5 Replies

user
steveastrouk (author)2018-03-08

I don't see why you detach/attach the interrupt. You also don't seem to use pulsecounter in your decision to switch the relay. If you want to sense ANY flow, then every time you check the timer, check if a flag has been inverted in the interrupt routin, or check that old pulse count <> new pulse count.

Select as Best AnswerUndo Best Answer

user
FriedZombie (author)2018-03-08

The code isn't that readable in this form

Select as Best AnswerUndo Best Answer

user

I attached my code as a separate file. Are you able to view it?

Select as Best AnswerUndo Best Answer

user
randofo (author)Eric-soberano2018-03-08

The attachment is not appearing. I will report this to our bugs team. Perhaps you could add it to the topic?

Select as Best AnswerUndo Best Answer

user
Eric-soberano (author)randofo2018-03-08

Hello. I added the code to the topic. Thanks.

Select as Best AnswerUndo Best Answer