ArmBASIC - contact debouncing

Questions about the BASICtools and MakeItC
Post Reply
KeepItSimpleStupid
Posts: 7
Joined: Fri Nov 30, 2012 5:47 am

ArmBASIC - contact debouncing

Post by KeepItSimpleStupid » Thu Jun 13, 2019 7:43 pm

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.



basicchip
Posts: 985
Joined: Fri Oct 19, 2012 2:39 am
Location: Lake Tahoe, CA
Contact:

Re: ArmBASIC - contact debouncing

Post by basicchip » Sat Jun 15, 2019 12:45 am

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.

basicchip
Posts: 985
Joined: Fri Oct 19, 2012 2:39 am
Location: Lake Tahoe, CA
Contact:

Re: ArmBASIC - contact debouncing

Post by basicchip » Wed Jun 19, 2019 7:15 pm

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)

basicchip
Posts: 985
Joined: Fri Oct 19, 2012 2:39 am
Location: Lake Tahoe, CA
Contact:

Re: ArmBASIC - contact debouncing

Post by basicchip » Mon Oct 14, 2019 12:14 am

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
pb1.png (310.04 KiB) Viewed 751 times

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

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

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
	

basicchip
Posts: 985
Joined: Fri Oct 19, 2012 2:39 am
Location: Lake Tahoe, CA
Contact:

Re: ArmBASIC - contact debouncing

Post by basicchip » Mon Oct 14, 2019 1:53 am

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
pb2.jpg (91.27 KiB) Viewed 744 times
DS1Z_QuickPrint5.jpg
DS1Z_QuickPrint5.jpg (83.67 KiB) Viewed 744 times
DS1Z_QuickPrint6.jpg
DS1Z_QuickPrint6.jpg (82.24 KiB) Viewed 744 times

basicchip
Posts: 985
Joined: Fri Oct 19, 2012 2:39 am
Location: Lake Tahoe, CA
Contact:

Re: ArmBASIC - contact debouncing

Post by basicchip » Mon Oct 14, 2019 3:43 pm

And the code above is being added as DEBOUNCE.bas in the Examples.

basicchip
Posts: 985
Joined: Fri Oct 19, 2012 2:39 am
Location: Lake Tahoe, CA
Contact:

Re: ArmBASIC - contact debouncing

Post by basicchip » Sun Oct 20, 2019 3:49 pm

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
DS1Z_QuickPrint1.jpg (82.55 KiB) Viewed 693 times

basicchip
Posts: 985
Joined: Fri Oct 19, 2012 2:39 am
Location: Lake Tahoe, CA
Contact:

Re: ArmBASIC - contact debouncing

Post by basicchip » Mon Nov 04, 2019 2:04 pm

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.

Post Reply