Increment At every button push
Snyggis
Posts: 14
I am struggling to come up with code that will increment a variable with every push of a button (or signal from a sensor).
My sensor can signal at roughly 2000, pulses per second. I cant seem to do it with a push button switch much less at a rate like that.
I would rally appreciate any help here!
My sensor can signal at roughly 2000, pulses per second. I cant seem to do it with a push button switch much less at a rate like that.
I would rally appreciate any help here!
Comments
http://www.mpja.com/download/18028op.pdf
Here is my complete Program:
Really the only important part for the counter is the LEdge (leading edge) method:
I was also trying something like this, but cant seem to get either one to work well:
Anyone have any ideas? Really all I want to do is count every time the interupt is tripped. I can count how many times this happens in a minute and calculate the RPM. There has to be an easy way to do that?
As Mike mentioned, pushbuttons and a sensor like the tachometer are quite different in terms of contact bounce or the lack thereof respectively. The cog counters will count every single bounce of a pushbutton
For a loop running in another cog, your method sets IDX=0 every time around the loop, so it will continue to count up so long as the pin remains high, rather than just at the 0-->1 transition. You can probably save the logic by working on it a bit more.
Here are a couple on alternative methods for your consideration. (neither of which have debouncing)
You could use the Propeller's built in counters to count the pulses. Ariba wrote some code to count pulses coming from another Propeller. You may be able to adapt his code for your needs.
There's a section in the Propeller Education Kit (available from the help menu of the Propeller Tool) on counters. If you want to understand Ariba's code you could read up on counters there.
Another alternative is to use a PASM loop to count the pulses. I bet there are several objects already available to do this but if you wanted to write your own you should read JonnyMac's Spin Zone article "Spinning It Up with Encoders" (or something like that). There's a link to the Spin Zone articles in post #3 of my index (see signature).
At the top of post #1 of my signature is a link to a Rich's search page to search the Parallax and Savage Circuits forums. You may want to see how others have solved this issue (I'm sure it's been solved many times before).
Edit: Or better yet, take Tracy's advice.
Would this code appear to be a good way to debounce?
This snippet debounces the rising edge only, then uses a simple waitpeq for the falling edge. You could add a waitcnt into the inner repeat loop, 100 microseconds say, to make it less sensitive on the Spin timing. That would be for a mechanical pushbutton, and use debounce=20 for a total of about 20 milliseconds of debounce time. However, as Duane noted above, at 2000Hz you don't have time for a lot of Spin instructions. About 0.5 millisecond for the whole loop.
About the logic...
auxcnt & -ina[inpin] ' when pin is zero, ANDs 0 with auxcnt, resets auxcnt to zero
' when pin is one, ANDs $ffffffff with auxcnt, auxcnt passes through.
0 & 0 = 0
0 & -1 = 0
Am I missing something here? Thanks again!
"&" is a bitwise and.
Page 164 of Propeller Manual (v1.2) does a good job explaining it so I won't repeat what the manual says here.