# Debounced Interrupt Failing counter, Help needed

**URL:** <https://forum.arduino.cc/t/debounced-interrupt-failing-counter-help-needed/458653>\
**Category:** Programming\
**Created:** [May 14, 2017, 1:07pm UTC](https://forum.arduino.cc/t/debounced-interrupt-failing-counter-help-needed/458653 "2017-05-14T13:07:09Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rustie0125](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/rustie0125/32/215000_2.png) [@Rustie0125](https://forum.arduino.cc/u/Rustie0125)\
**Post date:** [May 14, 2017, 1:07pm UTC](https://forum.arduino.cc/t/debounced-interrupt-failing-counter-help-needed/458653/1 "2017-05-14T13:07:09Z")

</div>

Hi Guys. I have an interesting project im working on and I cant seem to to get the concept working. I have a flow meter that produces 5-10ms pulses which represents flow rate. I want to measure the pulses with a failing interrupt but make sure its accurate so im trying to use denounced counter to see if it will work.

Setup.  
I have a stand alone UNO running blink sketch 5ms on 5ms off out off pin two. Then i have another uno running the following code. The two uno's pin 2s and grounds are connected to each other.

```cpp
 const byte ledPin = 13;
const byte interruptPin = 2;
volatile byte state = LOW;
volatile int counter;

void setup() {
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT);
  pinMode(interruptPin, INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(interruptPin), blink, FALLING);
}

void loop() {
  digitalWrite(ledPin, state);
  my_interrupt_handler();
  Serial.println(counter);
}

void blink() {
  state = !state;
}
void my_interrupt_handler()
{
 static unsigned long last_interrupt_time = 0;
 unsigned long interrupt_time = micros();
 int time_Value = interrupt_time - last_interrupt_time;
 if ( time_Value > 3500 &&  time_Value <20000 ) 
 {
  counter++;
 }
 last_interrupt_time = interrupt_time;
 
}

```

Its not pretty but its just a test. what im expecting to see in the serial window is the counter counting up reliably but all i get is Zero.

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [May 14, 2017, 1:09pm UTC](https://forum.arduino.cc/t/debounced-interrupt-failing-counter-help-needed/458653/2 "2017-05-14T13:09:55Z")

</div>

Declare counter as volatile

---

<div class="post-metadata">

**Author:** ![Rustie0125](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/rustie0125/32/215000_2.png) [@Rustie0125](https://forum.arduino.cc/u/Rustie0125)\
**Post date:** [May 14, 2017, 1:13pm UTC](https://forum.arduino.cc/t/debounced-interrupt-failing-counter-help-needed/458653/3 "2017-05-14T13:13:38Z")

</div>

Hello

declared in volatile same result

---

<div class="post-metadata">

**Author:** ![anon57585045](https://avatars.discourse-cdn.com/v4/letter/a/ed8c4c/32.png) [@anon57585045](https://forum.arduino.cc/u/anon57585045)\
**Post date:** [May 14, 2017, 1:18pm UTC](https://forum.arduino.cc/t/debounced-interrupt-failing-counter-help-needed/458653/4 "2017-05-14T13:18:21Z")

</div>

Your "interrupt handler" just tests the loop time, not the interrupt time.

---

<div class="post-metadata">

**Author:** ![Rustie0125](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/rustie0125/32/215000_2.png) [@Rustie0125](https://forum.arduino.cc/u/Rustie0125)\
**Post date:** [May 14, 2017, 1:35pm UTC](https://forum.arduino.cc/t/debounced-interrupt-failing-counter-help-needed/458653/5 "2017-05-14T13:35:32Z")

</div>

> aarg:  
> Your "interrupt handler" just tests the loop time, not the interrupt time.

I see what you mean, but im not sure how to fix it.

---

<div class="post-metadata">

**Author:** ![anon57585045](https://avatars.discourse-cdn.com/v4/letter/a/ed8c4c/32.png) [@anon57585045](https://forum.arduino.cc/u/anon57585045)\
**Post date:** [May 14, 2017, 1:44pm UTC](https://forum.arduino.cc/t/debounced-interrupt-failing-counter-help-needed/458653/6 "2017-05-14T13:44:56Z")

</div>

> Rustie0125:  
> I see what you mean, but im not sure how to fix it.

You have to keep track of time in the ISR code.

---

<div class="post-metadata">

**Author:** ![anon94801460](https://avatars.discourse-cdn.com/v4/letter/a/278dde/32.png) [@anon94801460](https://forum.arduino.cc/u/anon94801460)\
**Post date:** [May 14, 2017, 1:45pm UTC](https://forum.arduino.cc/t/debounced-interrupt-failing-counter-help-needed/458653/7 "2017-05-14T13:45:57Z")

</div>

Your "interrupt\_handler" does not handle interrupts.

The actual interrupt handler is named blink, and doesn't blink or do anything else useful (like check the time or count).

Why are you confusing yourself with such a strange approach to counting?

---

<div class="post-metadata">

**Author:** ![Rustie0125](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/rustie0125/32/215000_2.png) [@Rustie0125](https://forum.arduino.cc/u/Rustie0125)\
**Post date:** [May 14, 2017, 1:56pm UTC](https://forum.arduino.cc/t/debounced-interrupt-failing-counter-help-needed/458653/8 "2017-05-14T13:56:27Z")

</div>

I tried to use the interrupt example on the arduino reference page.

does this look better ?

```cpp
void setup() {
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT);
  pinMode(interruptPin, INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(interruptPin), my_interrupt_handler, FALLING);
}

void loop() {
  digitalWrite(ledPin, state);
  Serial.println(counter);
}

void blink() {
  state = !state;
}
void my_interrupt_handler()
{
 static unsigned long last_interrupt_time = 0;
 unsigned long interrupt_time = micros();
 int time_Value = interrupt_time - last_interrupt_time;
 if ( time_Value > 3500 &&  time_Value <20000 ) 
 {
  counter++;
 }
 last_interrupt_time = interrupt_time;
}

```

---

<div class="post-metadata">

**Author:** ![anon57585045](https://avatars.discourse-cdn.com/v4/letter/a/ed8c4c/32.png) [@anon57585045](https://forum.arduino.cc/u/anon57585045)\
**Post date:** [May 14, 2017, 1:58pm UTC](https://forum.arduino.cc/t/debounced-interrupt-failing-counter-help-needed/458653/9 "2017-05-14T13:58:17Z")

</div>

> Rustie0125:  
> does this look better ?

It's closer to your stated intent. Does it work? You didn't post the complete program.

---

<div class="post-metadata">

**Author:** ![Rustie0125](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/rustie0125/32/215000_2.png) [@Rustie0125](https://forum.arduino.cc/u/Rustie0125)\
**Post date:** [May 14, 2017, 2:36pm UTC](https://forum.arduino.cc/t/debounced-interrupt-failing-counter-help-needed/458653/10 "2017-05-14T14:36:30Z")

</div>

No does work.

here is the complete code. Im confusing myself now. 😕

```cpp
const byte ledPin = 13;
const byte interruptPin = 2;
volatile byte state = LOW;
volatile int counter;

void setup() {
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT);
  pinMode(interruptPin, INPUT_PULLUP);
  attachInterrupt(2, my_interrupt_handler, FALLING);
}

void loop() {
  digitalWrite(ledPin, state);
  Serial.println(counter);
}

void my_interrupt_handler()
{
 static unsigned long last_interrupt_time = 0;
 unsigned long interrupt_time = micros();
 int time_Value = interrupt_time - last_interrupt_time;
 if ( time_Value > 3500 &&  time_Value <20000 ) 
 {
  counter++;
 }
 last_interrupt_time = interrupt_time;
}

```

---

<div class="post-metadata">

**Author:** ![BulldogLowell](https://avatars.discourse-cdn.com/v4/letter/b/22d042/32.png) [@BulldogLowell](https://forum.arduino.cc/u/BulldogLowell)\
**Post date:** [May 14, 2017, 2:47pm UTC](https://forum.arduino.cc/t/debounced-interrupt-failing-counter-help-needed/458653/11 "2017-05-14T14:47:21Z")

</div>

```cpp
if ( time_Value > 3500 &&  time_Value <20000 )

```

will ignore pulses with a frequency of greater than 20 seconds...

is that what you really want?

three and a half seconds is an eon in a microcontroller. it seems like you really want something like this:

```cpp
void my_interrupt_handler()
{
 static unsigned long lastInterruptTime = 0;  // use camel case
 unsigned long interruptTime = micros();
 if ( interruptTime - lastInterruptTime > 50) // start here with 50 milliseconds
 {
  counter++;
  lastInterruptTime = interruptTime;  // reset the timer only if you incremented counter
 }
}

```

---

<div class="post-metadata">

**Author:** ![Rustie0125](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/rustie0125/32/215000_2.png) [@Rustie0125](https://forum.arduino.cc/u/Rustie0125)\
**Post date:** [May 14, 2017, 2:53pm UTC](https://forum.arduino.cc/t/debounced-interrupt-failing-counter-help-needed/458653/12 "2017-05-14T14:53:37Z")

</div>

mmm im working in micros not millis so 3500 is not 3,5seconds.

---

<div class="post-metadata">

**Author:** ![BulldogLowell](https://avatars.discourse-cdn.com/v4/letter/b/22d042/32.png) [@BulldogLowell](https://forum.arduino.cc/u/BulldogLowell)\
**Post date:** [May 14, 2017, 2:57pm UTC](https://forum.arduino.cc/t/debounced-interrupt-failing-counter-help-needed/458653/13 "2017-05-14T14:57:32Z")

</div>

> Rustie0125:  
> mmm im working in micros not millis so 3500 is not 3,5seconds.

yeah, my mistake!

you still should not put the upper limit on the sensor

---

<div class="post-metadata">

**Author:** ![Rustie0125](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/rustie0125/32/215000_2.png) [@Rustie0125](https://forum.arduino.cc/u/Rustie0125)\
**Post date:** [May 14, 2017, 3:00pm UTC](https://forum.arduino.cc/t/debounced-interrupt-failing-counter-help-needed/458653/14 "2017-05-14T15:00:03Z")

</div>

> BulldogLowell:
> 
> ```arduino
> if ( time_Value > 3500 &&  time_Value <20000 )
> 
> ```
> 
> will ignore pulses with a frequency of greater than 20 seconds...
> 
> is that what you really want?
> 
> three and a half seconds is an eon in a microcontroller. it seems like you really want something like this:
> 
> ```arduino
> void my_interrupt_handler()
> 
> ```

{  
static unsigned long lastInterruptTime = 0; &nbsp;// use camel case  
unsigned long interruptTime = micros();  
if ( interruptTime - lastInterruptTime \> 50) // start here with 50 milliseconds  
{  
&nbsp; counter++;  
&nbsp; lastInterruptTime = interruptTime; &nbsp;// reset the timer only if you incremented counter  
}  
}

> ```arduino
> 
> ```

The pulses to be counted is 5-10ms in width. so i want to make sure i only catch pulses between 4 to 11ms in width, thats why im working in micros and the 20000 was just to test.

---

<div class="post-metadata">

**Author:** ![Rustie0125](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/rustie0125/32/215000_2.png) [@Rustie0125](https://forum.arduino.cc/u/Rustie0125)\
**Post date:** [May 14, 2017, 3:09pm UTC](https://forum.arduino.cc/t/debounced-interrupt-failing-counter-help-needed/458653/15 "2017-05-14T15:09:07Z")

</div>

So just trying the code now as suggested by bulldog, and changed the timing to 10ms but im still getting no response from the counter. stays "0" and i know my pulses coming from the other uno is 5ms

```cpp
const byte ledPin = 13;
const byte interruptPin = 2;
volatile byte state = LOW;
volatile int counter;

void setup() {
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT);
  pinMode(interruptPin, INPUT_PULLUP);
  attachInterrupt(2, my_interrupt_handler, FALLING);
}

void loop() {
  digitalWrite(ledPin, state);
  Serial.println(counter);
}

void my_interrupt_handler()
{
 static unsigned long lastInterruptTime = 0;  // use camel case
 unsigned long interruptTime = micros();
 if ( interruptTime - lastInterruptTime < 10000) // start here with 50 milliseconds
 {
  counter++;
  lastInterruptTime = interruptTime;  // reset the timer only if you incremented counter
 }
}

```

---

<div class="post-metadata">

**Author:** ![BulldogLowell](https://avatars.discourse-cdn.com/v4/letter/b/22d042/32.png) [@BulldogLowell](https://forum.arduino.cc/u/BulldogLowell)\
**Post date:** [May 14, 2017, 3:12pm UTC](https://forum.arduino.cc/t/debounced-interrupt-failing-counter-help-needed/458653/16 "2017-05-14T15:12:53Z")

</div>

you reversed the test:

```cpp
if ( interruptTime - lastInterruptTime < 10000) // start here with 50 milliseconds

```

should be:

```cpp
if ( interruptTime - lastInterruptTime > 10000) // now 10 milliseconds

```

right? you want to ignore any signal that comes in before 10milliseconds have elapsed since the last trigger.

---

<div class="post-metadata">

**Author:** ![Rustie0125](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/rustie0125/32/215000_2.png) [@Rustie0125](https://forum.arduino.cc/u/Rustie0125)\
**Post date:** [May 14, 2017, 3:17pm UTC](https://forum.arduino.cc/t/debounced-interrupt-failing-counter-help-needed/458653/17 "2017-05-14T15:17:36Z")

</div>

No I want to catch all signal between 5ms and 10ms window. for now i want to catch all pulses shorter then 10ms.

---

<div class="post-metadata">

**Author:** ![BulldogLowell](https://avatars.discourse-cdn.com/v4/letter/b/22d042/32.png) [@BulldogLowell](https://forum.arduino.cc/u/BulldogLowell)\
**Post date:** [May 14, 2017, 3:24pm UTC](https://forum.arduino.cc/t/debounced-interrupt-failing-counter-help-needed/458653/18 "2017-05-14T15:24:28Z")

</div>

> Rustie0125:  
> No I want to catch all signal between 5ms and 10ms window. for now i want to catch all pulses shorter then 10ms.

your ISR is detecting the FALLING edge. so, you are measuring the frequency of the pulse, not the length of the pulse.

Is this a continuous signal that simply varies the pulse width?

if so, you ought to consider using [pulseIn()](https://www.arduino.cc/en/Reference/pulseIn) and polling the sensor on a regular interval.

---

<div class="post-metadata">

**Author:** ![Rustie0125](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/rustie0125/32/215000_2.png) [@Rustie0125](https://forum.arduino.cc/u/Rustie0125)\
**Post date:** [May 14, 2017, 3:32pm UTC](https://forum.arduino.cc/t/debounced-interrupt-failing-counter-help-needed/458653/19 "2017-05-14T15:32:50Z")

</div>

Let me give you the application.

The flow sensor gives me pulses the pulse change in frequency the faster the flow. every pulse is x cubic meters of water and each pulse is 5ms in width. So i want to count all the pulses and make sure that there is no missed or false pulses. o and maximum output of the senor is 100hz

---

<div class="post-metadata">

**Author:** ![BulldogLowell](https://avatars.discourse-cdn.com/v4/letter/b/22d042/32.png) [@BulldogLowell](https://forum.arduino.cc/u/BulldogLowell)\
**Post date:** [May 14, 2017, 3:36pm UTC](https://forum.arduino.cc/t/debounced-interrupt-failing-counter-help-needed/458653/20 "2017-05-14T15:36:19Z")

</div>

> Rustie0125:  
> Let me give you the application.
> 
> The flow sensor gives me pulses the pulse change in frequency the faster the flow. every pulse is x cubic meters of water and each pulse is 5ms in width. So i want to count all the pulses and make sure that there is no missed or false pulses.

ok, then you want to capture and count every pulse (you can use the rising or falling edge). My experience says that these do not behave like switches and you don't need to debounce. did you try just incrementing your counter on the falling edge?

```arduino
void my_interrupt_handler()
{
  counter++;
}

```

what happens?

[Next page](https://forum.arduino.cc/t/debounced-interrupt-failing-counter-help-needed/458653.md?page=2)
