## Incrementing a value after each time a button is pushed

10 posts / 0 new
Author
Message
```//TRIGGER-----------------------------------------------------------------------------------------
ADMUX &= ~(1 << MUX0);					//Selects PIN 1 (TRIGGER)
ADMUX &= ~(1 << MUX1);					//Selects PIN 1 (TRIGGER)
if(ADCH > 127)						//PIN1 > 2.50v?
{
Trigger=1;
Jcount = Jcount + 1;
}
else
{
Trigger=0;
}```

With the code above on an ATTINY85, and this part of the code is inside of a continuous loop so it can detect changes to Trigger in real time. But what I want to do with Jcount is add 1 to it each time the trigger goes high only once instead of continually adding 1 to Jcount while Trigger=1.

So the following sequence is what I want to do but am unsure of how to do it:
If Trigger=1 then add 1 to Jcount only once(like a one shot)
Trigger will then return to 0 but at a random amount of time as trigger may be held at 1 for a while.
If Trigger=1 again, add 1 again to Jcount only once.

So essentially I have a button that will be pushed, I want to add 1 to Jcount each time the button is pushed, but the button may be held for an indeterminate amount of time each time its pushed. If I push it and hold it I want to add 1 to Jcount only once, then it must be released and pushed again to add another 1 to Jcount.

Last Edited: Mon. May 4, 2009 - 03:23 PM

Well, this is a standard debouncing situation, where your debounce sequence delivers at least rising edges.

Why would you want to do A/D conversion on what is an on/off input? Why not just read the logical pin state directly?

You could, of course, enter the "hit" or "no hit" threshold value into the debounce sequence as a 1/0. I've done it on rare occasions (Mega8 ADC6/ADC7) but not as a normal diet.

Lee

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

Also, you have to keep track of the previous state to know when it has changed. This will eliminate the constant incrementing. You also need to "de-bounce" as Lee said previously.

Even if I read the logical pin state directly, as high and low, it still needs to be monitored in real time, so if I press and hold the button and it sees a high, is it not going to keep adding 1 to Jcount as long as that condition is true? Or does it have to false then true again before it will add another 1 to Jcount?

As I said, this is standard debouncing methodology. If you understand that (forum searches for "debounce" and "debouncing" will get you going) then you will find that any decent sequence will have provisions for determining "new edges".

Lee

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

Search out Peter Danegger's posts with debouncing algorithms. He has posted several versions of his excellent debouncing code, and in one of them he has an auto-repeat function that just might fill your "constant monitoring" need. Peters username here is "danni".

As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here

No guarantees, but if we don't report problems they won't get much of  a chance to be fixed! Details/discussions at link given just above.

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

simple example but without debouncing involved (to make sure you get the basic idea):
2) compare current button state to previously read button state
3) if previous state is not pressed and current state is pressed, then a push edge is detected and you can increment your value
4) store current state as previous state.

sometimes you may find other states important as well, like when a button is released. you already seem to know when it is pressed or released.

now for debouncing:
1) never believe button state until it has been stable for long enough.
2) count how long a button has been stable
3) only when stable long enough, change debounced button state
4) compare to previous debounced state (you get the idea)

Hi,
you can easily debounce at the Adc with a RC between your button and the input. Then expand your code by

`if (!Trigger) {Jcount++; }`

but have your Trigger = 1 line behind it.

Quote:

you can easily debounce at the Adc with a RC between your button and the input.

Sounds like a solution in search of a problem to me (but some swear by the hardware debouncing--I always wonder how they get good results on fast [flowmeter pulse] inputs and slow [contactor, relay] inputs all with the same hardware, or do they change components for each channel?)

Now, if you really need to clean up a fast input from the real world such as quadrature, then RC + a Schmitt trigger stage or two does indeed make sense.

In either case, or in any case, why do an A/D on it? (Exception noted when the only pin left is an ADC. ;) )

How does setting Trigger to 1 after the count-up suppress further counts after the next ADC read?

[I tried posting some fragments for demonstration but the site is rejecting them.]

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

Last Edited: Mon. May 4, 2009 - 07:06 PM