Page 1 of 1

ArmBASIC - contact debouncing

Posted: Thu Jun 13, 2019 7:43 pm
by KeepItSimpleStupid
It might be a good idea to discuss contact debouncing in ARMBASIC.pdf with an example.

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.

Re: ArmBASIC - contact debouncing

Posted: Sat Jun 15, 2019 12:45 am
by basicchip
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.


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

Posted: Wed Jun 19, 2019 7:15 pm
by basicchip
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)

Re: ArmBASIC - contact debouncing

Posted: Mon Oct 14, 2019 12:14 am
by basicchip
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.

pb1.png (310.04 KiB) Viewed 6527 times

Then some scope pix show the push is pretty clean, but release is takes about 1/2 millisecond to settle

DS1Z_QuickPrint2.jpg (85.91 KiB) Viewed 6527 times
DS1Z_QuickPrint4.jpg (84.28 KiB) Viewed 6527 times
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


IO(61)=0		' drive one side of the switch low

DEBOUNCED_PB = IN(45)		' set the default state

while 1
	nextIN = IN(45)
	if DEBOUNCED_PB <> nextIN then
		if MAYBE_BOUNCING = 0 then
			DEBOUNCED_PB = nextIN			' record the change
			DEBOUNCE_WAIT = TIMER + 1000	' wait 1 millisecond
			print ".";          ' show the change -- prove to yourself its working
			end if
		end if
	end if

Re: ArmBASIC - contact debouncing

Posted: Mon Oct 14, 2019 1:53 am
by basicchip
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.

pb2.jpg (91.27 KiB) Viewed 6520 times
DS1Z_QuickPrint5.jpg (83.67 KiB) Viewed 6520 times
DS1Z_QuickPrint6.jpg (82.24 KiB) Viewed 6520 times

Re: ArmBASIC - contact debouncing

Posted: Mon Oct 14, 2019 3:43 pm
by basicchip
And the code above is being added as DEBOUNCE.bas in the Examples.

Re: ArmBASIC - contact debouncing

Posted: Sun Oct 20, 2019 3:49 pm
by basicchip
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 --

DS1Z_QuickPrint1.jpg (82.55 KiB) Viewed 6469 times

Re: ArmBASIC - contact debouncing

Posted: Mon Nov 04, 2019 2:04 pm
by basicchip
For my thermostat project, I wrote some interrupt code to debounce buttons.


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.