Go Down

Topic: Resetting a pulsecount (Read 595 times) previous topic - next topic

jtw11

#13
Oct 15, 2012, 06:03 pm Last Edit: Oct 15, 2012, 06:58 pm by jtw11 Reason: 1
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: [Select]

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: [Select]

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
}

retrolefty

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

jtw11

...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: [Select]
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;
 }
}

PaulS

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.

PaulS

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.

Go Up