It might be a good idea to discuss contact debouncing in ARMBASIC.pdf with an example.
e.g.
Use interrupts
Detection of a long and short press.
It's also too basic of a function to not be included. Maybe something like:
e.g. On BUTTON(port, rise/fall, duration) goto <label>
or even
ON BUTTON (port, rise|fall, duration(500,5000,10000)) goto labels(SHORTPRESS, LONGPRESS, REALLYLONG PRESS)
Times are in mS; max of 3 terms, minimum of one term.
Or maybe be able to define a port as a BUTTON
I didn't deal with HOLD.
ArmBASIC - contact debouncing
Re: ArmBASIC - contact debouncing
Rather than put everything in firmware (which would require updates all the time), it is better to build BASIC libraries to do that.
Debounce time depends on the type of switch and it can vary a lot.
In our pong example we basically check the button every 15 ms.
viewtopic.php?f=4&t=672&p=2145&hilit=pong
You can interrupt on every change of a switch, or just look for change, then ignore the state for a certain period of time.
Debounce time depends on the type of switch and it can vary a lot.
In our pong example we basically check the button every 15 ms.
viewtopic.php?f=4&t=672&p=2145&hilit=pong
You can interrupt on every change of a switch, or just look for change, then ignore the state for a certain period of time.
Re: ArmBASIC - contact debouncing
We have moved away from the builtin ON type statements, as they are too limiting.
In the blog and forum are now examples of multitasking that can easily handle button debounce.
Another new example in the blog and forum shows an example of generating interrupts on a pin transition.
For a debounce for example a falling edge can generate an interrupt, and then the interrupt can changed to rising edge, but for the debounce time, transitions would be ignored.
Are you looking to debounce a simple button, or something more complex like a scanned keyboard?
PS - glad to hear from you again (KISS)
In the blog and forum are now examples of multitasking that can easily handle button debounce.
Another new example in the blog and forum shows an example of generating interrupts on a pin transition.
For a debounce for example a falling edge can generate an interrupt, and then the interrupt can changed to rising edge, but for the debounce time, transitions would be ignored.
Are you looking to debounce a simple button, or something more complex like a scanned keyboard?
PS - glad to hear from you again (KISS)
Re: ArmBASIC - contact debouncing
Buttons and switches bounce, and you have to take that into account. The easiest way is to look for a change in the button then ignore all other changes for a certain debounce time. That can vary a lot depending on the switch. I set up a simple test case on an ARMstamp with a push button between 2 pins. One pin driven low, the other pulled high by the internal resistor.
Then some scope pix show the push is pretty clean, but release is takes about 1/2 millisecond to settle
And here is a program to read the debounced state of the button and display that on the LED
Then some scope pix show the push is pretty clean, but release is takes about 1/2 millisecond to settle
And here is a program to read the debounced state of the button and display that on the LED
Code: Select all
#define LED_PIN 1
main:
IO(61)=0 ' drive one side of the switch low
DEBOUNCED_PB = IN(45) ' set the default state
MAYBE_BOUNCING = 0
while 1
nextIN = IN(45)
if DEBOUNCED_PB <> nextIN then
if MAYBE_BOUNCING = 0 then
DEBOUNCED_PB = nextIN ' record the change
MAYBE_BOUNCING = 1
DEBOUNCE_WAIT = TIMER + 1000 ' wait 1 millisecond
print "."; ' show the change -- prove to yourself its working
else
if TIMER > DEBOUNCE_WAIT then
MAYBE_BOUNCING = 0
end if
end if
end if
IO(LED_PIN) = DEBOUNCED_PB
loop
Re: ArmBASIC - contact debouncing
That was a small pushbutton, big toggle switches may bounce longer. If you have a scope you can take a look. If not it you see multiple . printed you need to increase the time.
So here is bigger push button, which is normally a good debounce, but if you do half or slow push/release you can get some ugly pictures lasting up to 30 msec.
So here is bigger push button, which is normally a good debounce, but if you do half or slow push/release you can get some ugly pictures lasting up to 30 msec.
Re: ArmBASIC - contact debouncing
And the code above is being added as DEBOUNCE.bas in the Examples.
Re: ArmBASIC - contact debouncing
And for reference I did some fast clicking on the small button.
Seems like about 100 msec is about average for a quick click and a double click occurs in about 300 msec (100 low 100 high 100 low)
So for that little push button a debouce time of 20-50 msec would see a double click. Bigger buttons longer travel times, so you might need to adjust.
For my thermostat project, I will be implementing an interrupt driven version for 2 buttons, and will post that code here when done. For that the CPU will be sleeping to save power, but a button click will wake it up as well as timer interrupt to make temp measurements.
double click --
Seems like about 100 msec is about average for a quick click and a double click occurs in about 300 msec (100 low 100 high 100 low)
So for that little push button a debouce time of 20-50 msec would see a double click. Bigger buttons longer travel times, so you might need to adjust.
For my thermostat project, I will be implementing an interrupt driven version for 2 buttons, and will post that code here when done. For that the CPU will be sleeping to save power, but a button click will wake it up as well as timer interrupt to make temp measurements.
double click --
Re: ArmBASIC - contact debouncing
For my thermostat project, I wrote some interrupt code to debounce buttons.
viewtopic.php?f=4&t=995&start=30#p4423
Works great for 1 button, the other button was bad, will have to replace it, that bad button was changing state while I was holding it down.
viewtopic.php?f=4&t=995&start=30#p4423
Works great for 1 button, the other button was bad, will have to replace it, that bad button was changing state while I was holding it down.