Pages: [1]   Go Down
Author Topic: Resetting a pulsecount  (Read 565 times)
0 Members and 1 Guest are viewing this topic.
UK
Offline Offline
God Member
*****
Karma: 1
Posts: 530
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Code:
int pulsecount = 0;

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

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

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 610
Posts: 49040
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.
Logged

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 361
Posts: 17292
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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-grin

Lefty
Logged

UK
Offline Offline
God Member
*****
Karma: 1
Posts: 530
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 610
Posts: 49040
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
regarding doing the actual value reset though, any ideas on how to do this?
Code:
pulsecount = 0;

The pulsecount variable should be volatile, by the way.

Where is your loop() function?
Logged

UK
Offline Offline
God Member
*****
Karma: 1
Posts: 530
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 610
Posts: 49040
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.

Quote
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.
Logged

UK
Offline Offline
God Member
*****
Karma: 1
Posts: 530
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
« Last Edit: October 15, 2012, 10:44:01 am by jtw11 » Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 610
Posts: 49040
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 610
Posts: 49040
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.
Logged

UK
Offline Offline
God Member
*****
Karma: 1
Posts: 530
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

...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.

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 = 0;
  }
}
Logged

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 361
Posts: 17292
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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
Logged

UK
Offline Offline
God Member
*****
Karma: 1
Posts: 530
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

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.

Code:
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
}
« Last Edit: October 15, 2012, 11:58:59 am by jtw11 » Logged

Pages: [1]   Go Up
Jump to: