Resetting a pulsecount

I've extracted the following few lines from a piece of code that I'm using to count interrupt pulses. I need a way to reset pulsecount to 0 when it reaches a certain number, in addition to ignoring further interrupts on that pin within a certain timeframe following the last pulse.

I can't think of a way to reset the value to 0, regarding ignoring another interrupt however within a certain time - maybe detach the interrupt within the interrupt itself for a predetermined amount of time, then reattach it - but i'm not sure if detachments / attachments are possible within an interrupt itself.

Any help would be greatly appreciated.

int pulsecount = 0;

void setup()
{
  attachInterrupt(0, trig, RISING); // din D2
}

void trig()
{
  pulsecount++; // adds a count to the variable pulsecount
}

I can't think of a way to reset the value to 0, regarding ignoring another interrupt however within a certain time - maybe detach the interrupt within the interrupt itself for a predetermined amount of time, then reattach it - but i'm not sure if detachments / attachments are possible within an interrupt itself.

In loop(), where you care about the number of interrupts would be where to reset the value and attach/detach the interrupt handlers.

while I haven't done it myself, I do believe you are free to detach a interrupt while inside it's ISR. But when you thing about it, it would not be possible to attach an interrupt while inside it's ISR function, as that would be a chicken/egg first problem. :smiley:

Lefty

I see - regarding doing the actual value reset though, any ideas on how to do this?

regarding doing the actual value reset though, any ideas on how to do this?

pulsecount = 0;

The pulsecount variable should be volatile, by the way.

Where is your loop() function?

My loop function was only being used to send the pulsecount over serial to the monitor, hence omitting it from this example.

What difference does volatile make? The function worked over serial?

My loop function was only being used to send the pulsecount over serial to the monitor, hence omitting it from this example.

But, it is in loop() that you will need to detect that the number of pulses/time limit has been exceeded.

What difference does volatile make?

If a variable can change inside an interrupt, it should be declared volatile, so that any code outside the interrupt knows to fetch the value every time it needs it, rather than rely on a cached value.

I understand re the inclusion of the loop and what should be included in there, I simply didn't include it on the forum. My question is, I do not know using what functions, syntax etc I should use to reset the counter.

EDIT:

What about the use of an if function...

if (pulsecounter > max number)
{
pulsecount = 0;
}

EDIT 2:

I have just tried this, the above does not reset pulse count to 0.

What about the use of an if function...

That should work. You said that there was more to check than just that the count exceeded some value, and more to do than just resetting the value, but that's a good start.

I have just tried this, the above does not reset pulse count to 0.

Maybe there are more pulses being generated than you think.

Of course, it is all guesswork without seeing all of your code and knowing what is generating the pulse, and how often they occur.

...it should do - but doesn't. Here's the whole code, but pulsecount does not reset to 0 above 50.

At the moment, I am generating the interrupt with a pull down resistor from interrupt 0 pin to gorund, then supplying 5V to the interrupt pin, each time I do it the pulsecount increments - only sometimes by more than one, I guess due to bounce.

volatile int pulsecount = 0;

void setup()
{
  Serial.begin(9600);
  attachInterrupt(0, trig, RISING); // din D2
}

void trig()
{
  pulsecount++; // adds a count to the variable pulsecount
}

void loop()
{
  Serial.println(pulsecount);
  delay(500); // wait .5s before updated serial monitor again
  if (pulsecount > 50)
  {
    pulsecount = 0;
  }
}

At the moment, I am generating the interrupt with a pull down resistor from interrupt 0 pin to gorund, then supplying 5V to the interrupt pin, each time I do it the pulsecount increments - only sometimes by more than one, I guess due to bounce.

Yes, contact bounce can always be a problem, but especially when using interrupts. It needs to be dealt with either inside the ISR with coding or externally with a proven hardware bounce filtering circuit.

Lefty

Right now, bounce isn't the problem - the hardware that will eventually be sending the pulse will not have this problem, I am simply using this setup to learn how to code interrupts.

However, my value is still going over 50, using my code it won't reset to 0 above 50.

EDIT: I seem to have come across a solution, see the below code.

volatile int pulsecount = 0;

void setup()
{
  Serial.begin(9600);
  attachInterrupt(0, trig, RISING); // din D2
}

void trig()
{
  pulsecount++; // adds a count to the variable pulsecount
}

void loop()
{
  Serial.println(pulsecount);
  delay(500); // wait .5s before updated serial monitor again
  if (pulsecount > 50)
  {
    pulsecount = pulsecount - 50;
  }
}

However, the problem isn't solved - I still need a way of setting pulsecount to 0 without subtracting the pulsecount itself, as I also need to be able to set pulsecount to 0 using another interupt, and I won't always know the value of pulsecount. Any ideas as to why my code of pulsecount = 0; does not work?

EDIT 2:

Seems I have actually found a solution, using my second interrupt and the code of pulsecount = 0; - pulsecount does indeed go to 0 when trig 2 goes high.

volatile int pulsecount = 0;

void setup()
{
  Serial.begin(9600);
  attachInterrupt(0, trig, RISING); // din D2
  attachInterrupt(1, trig 2, RISING); // din D3
}

void trig()
{
  pulsecount++; // adds a count to the variable pulsecount
}

void trig 2()
{
  pulsecount = 0; // reset pulsecount to 0 when trig 2 interrupts
}

void loop()
{
  Serial.println(pulsecount);
  delay(500); // wait .5s before updated serial monitor again
}