LED turn off

I am making a leak detection sketch and really need to say if the flow meter is reading flow then count for x amount of time and if longer sound alarm. Im using and interrupt to read the pulse output and cant time anything because of that. but my else statement is not turning of the led when i stop the flow. WHY???

//interupt that the pin is conected to, the flow meter
int flowMeterInt = 0; // PIN 3

volatile unsigned long flowTime = 10;

int pin =3;
unsigned long duration;

void setup() {
  
   // put your setup code here, to run once:

   //I think you do this to set rate in serial
 Serial.begin(115200);

   //Interrupt code.. im using leanardo so int.0 is on PIN 3
 attachInterrupt(flowMeterInt, leakFlow, FALLING);

 pinMode(pin, INPUT);

}







void loop()
{
  duration = pulseIn(pin, HIGH);
}

void leakFlow()
{
 // Serial.println(flowTime);
  Serial.println(duration);

 if(flowMeterInt,FALLING)
 {
  
   digitalWrite(13,HIGH);
 }
 else
 {
   
   digitalWrite(13, LOW);
   
   }
  
}

A serial print inside an ISR... that wont work. Move the serial print to the main loop and activate it with a trigger on the isr. Serial library, if Im correct, uses interrupts too, so there may be a conflict.

And that if statement, what are you trying to state? Because the interrupt is going to be triggered when falling, then that is going to be True always

I think this is wrong:

if(flowMeterInt,FALLING)

Normally, the expression inside the parenthesis following an if statement should evaluate to a single value, which is true (non-zero) or false (zero). You've got two terms/variables and I'm not expert enough to say that's not valid, but I've never seen it.

Alright, now if I put the serial print in the main loop it just keeps going no matter if the meter is moving or not. It needs to just read if the flow meter is getting flow or not, not how much. This is how I thought you did that. The LED is off until flow starts then it starts but it never goes off. Here is some refined code.

//interupt that the pin is conected to, the flow meter
int flowMeterInt = 0; // PIN 3

volatile unsigned long flowTime = 10;


void setup() {
 
  // put your setup code here, to run once:

  //I think you do this to set rate in serial
Serial.begin(115200);

  //Interrupt code.. im using leanardo so int.0 is on PIN 3
attachInterrupt(flowMeterInt, leakFlow, FALLING);


}

void loop()
{
 
 
 
}

void leakFlow()
{
 Serial.println(flowTime);
 
if(flowMeterInt,FALLING)
{
 
  digitalWrite(13,HIGH);
}
else
{
  
  digitalWrite(13, LOW);
  
  }
 
}

Im not sure but from what I know i think the if statement is okay???

Posted by DVDdoug - Today at 01:52 am Quote
I think this is wrong:
Code: [Select]
if(flowMeterInt,FALLING)

Normally, the expression inside the parenthesis following an if statement should evaluate to a single value, which is true (non-zero) or false (zero). You've got two terms/variables and I'm not expert enough to say that's not valid, but I've never seen it.

cmkwood:
I am making a leak detection sketch and really need to say if the flow meter is reading flow then count for x amount of time and if longer sound alarm.

I think that means that you want to sound an alarm if the flow meter doesn't deliver a pulse for a certain period of time. Is that right?

Im using and interrupt to read the pulse output and cant time anything because of that.

I don't understand this statement. Do you avoid using timing functions because your sketch uses interrupts? I don't know of a good reason to avoid using, say, millis(), just because you're using other interrupts for something else.

... but my else statement is not turning of the led when i stop the flow. WHY???

The code, as written, will always turn the LED on, and it will never turn it off.

if(flowMeterInt,FALLING)

I don't think this line works the way you expect. Here's what happens: The compiler treats the comma as an operator, and evaluates its operands left to right, discarding each value as it moves to the next operand, and keeping only the value returned by the last operand . It evaluates variable flowmeter, finds that it's zero, and then discards it. Then, it evaluates the value of FALLING - that's a macro, defined in Arduino.h, with a value of 2. The last value that it evaluated is what it tests - 2, which evaluates as true. So, this test always passes, no matter what. It will always turn the LED on, and it will never turn it off.

If you're trying to test whether a falling edge has occurred on the interrupt pin, there's no point - as others have pointed out, a falling edge on the interrupt is what got you into the ISR in the first place. I think that this may be what you want to do:

  • Have the ISR log the time of the falling edge.
  • Repeatedly examine the time of the last falling edge in loop(), comparing it to the current time.
  • When the difference between the current time and the time of the last falling edge gets big enough, set the alarm.
    For visual feedback, you may want the ISR to turn on the LED unconditionally, and let loop() turn it off when the last pulse gets too old.

Im not sure but from what I know i think the if statement is okay???

I'm absolutely sure that it is not OK

tmd3 you almost have it, the test is to see if there is a leak in the water system. Some plumbers will go to the city meter and see if it is running when all the water is off and that would mean a leak. My meter will do the same but be let you know then the water is running in the house. So lets say you use water for 20min at the most well then set the timer to 25min and if the water runs more than that there is a leak in the line. I need to read a pulse output meter and see how long it runs for to see if there is a leak. now in small leak situations there is 24sec between pulses but the water is still flowing. Now I have all the hardware figured out but the software has me stumped. I have done what I can from my weeks of research but I'm lost. I need to 1. read the pulse output meter. 2. time it and make the time value changeable from 30sec to 2hrs.(maybe a potentiometer??) 3. set if the time limit is met then blink a LED sound an alarm. I have the simple stuff figured out like blinking a LED and I can read the Pulse output meter too. I just cant get it all to mesh. Any help or examples (the best way I learn) would be loved.

OK, here's what I'm reading: For a residence, you expect that the water demand will will persist for at most some reasonable period of time, and then go to zero, or nearly to zero. You want to monitor the pulse output from a water meter for some period of time that exceeds the maximum likely period for water demand, and observe whether or not the pulse rate goes to zero, or nearly to zero, during that time.

  • The device will monitor pulses for some period of time, and you'd like to be able to adjust that time between 30 seconds and 2 hours.

  • If, during the monitoring period, the pulse train doesn't stop for some specific period of time, the device will activate an alarm output.First, some questions:

  • I'd think that a device like this would be useful, for a plumber. It would let him test a house for water leaks while he slept, or did honest, paying work somewhere else, without interrupting the customer's activities. Is a device that does this on the market?

  • It's not clear to me that using an interrupt is the best course for this project. I would guess that pulses from the water meter happen infrequently and last a long time, from a 16 MHz microcontroller's point of view. I would think that monitoring them directly in loop() would be sufficient. To test that notion, I'll ask: is it possible for the meter to deliver meaningful pulses that last less than, say, ten milliseconds, in either state, at the maximum flow rate? I'd expect that the answer would be, "No," and that would suggest that the microcontroller would have plenty of time to capture a pulse just by looking at it from time to time.

  • Based on the code that you posted, I would guess that this is the first time that you've attempted to use interrupts on the Arduino. I'd also guess that you've only recently started programming in C or C++. Please tell me if I'm wrong about either of those assessments.