Hi all,
a little while ago I posted an article concerning Arduino timer interrupts and provided a very helpful link to a web site that generates the required code and configs for the interrupt frequency sought and the timer required. Anyway, I don't quite know how, but the post morphed into an on going discussion about switch debounce. So, rather than add to that post, I thought I would start a new post to put forward a software solution to deal with switch debounce, thereby making it a little more visible and not buried within the discussion chain relating to Arduino timer interrupts.
My attached sketch provides a self contained set of functions and a structure to deal with a wide variety of switches and different wiring schemes and which has a unique feature (as far as I am aware) that allows a switch to be linked to other output pins, even interrupt pins.
The sketch has been tested for Arduino boards but not for ESP boards and provides features that will deal with:
- button switches
- toggle switches
- different wiring schemes
- variable debounce period
- detecting switches in transition (if this is feature that would be useful)
- multiple concurrent switch configs and processing of either switch type and of either wiring scheme
- switch, wiring scheme and linking configuration via a simple data structure
- the ability to link any switch to another output, even a none connected interrupt pin
- code for a skeleton ISR, triggered on a linked switch to an interrupt pin
- any number of switches linked to a common ISR
- ... and more!
The sketch is principally the ez_switch_lib library articulated as a sketch. You can follow up on the library's capabilities and functionality (and therefore the sketch) by reference to this Arduino Project Hub link. The article provides a full pdf User Guide and Crib Sheet.
The sketch is heavily commented, provides advice throughout and indicates where your own code should be added for the particular application you have in mind.
The sketch is offered for your 'enjoyment' and exploration, butcher it as you need or use it out the box.
Finally, software debounce v. hardware debounce - I am not advocating a purely software approach to debouncing of switches - it ultimately depends on what you want to achieve so 'you pays your money and makes your choice'.
Cheers
Ron B
PS
you may also wish to see another article concerning switches (Understanding & Using Button Switches).
Ron Bentley
Creativity is an input to innovation and change is the output from innovation. Braden Kelley
A computer is a machine for constructing mappings from input to output. Michael Kirby
Through great input you get great output. RZA
Gauss is great but Euler rocks!!
butcher it as you need
😀 whilst I'm good for a bit of butchery your good sketch is a very worthwhile addition to my snippets folder without needing any sharp knives. Thanks for this excellent contribution.
Just one small thing to mention, and only for rank beginners, for an ESP board don't connect one side of the pin to +5v, but find a 3.2volt out. (re comments lines 7 - 10)
Hi,
thank you for your post and pleased to read that you now have another tool in your tool box.
As soon as I read the post I had a 'doh' moment - I knew that the ESP VCC level should be 3.3v and not 5v!!! The problem is, I am principally an Arduiner and live in a +5v world most of the time!
I will have to move to the 'dark-side' and get into ESP.
Regards
Ron B
Ron Bentley
Creativity is an input to innovation and change is the output from innovation. Braden Kelley
A computer is a machine for constructing mappings from input to output. Michael Kirby
Through great input you get great output. RZA
Gauss is great but Euler rocks!!
@ronbentley1 Yup, it's where all us wizards hang out.
First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, 360, fairly knowledge in PC plus numerous MPU's & MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
My personal scorecard is now 1 PC hardware fix (circa 1982), 1 open source fix (at age 82), and 2 zero day bugs in a major OS.
my ESP32 arrives this Thursday, so expect many questions moving forward!
Not so much as a betrayal to Arduino, more a big brother joining the family!
Ron Bentley
Creativity is an input to innovation and change is the output from innovation. Braden Kelley
A computer is a machine for constructing mappings from input to output. Michael Kirby
Through great input you get great output. RZA
Gauss is great but Euler rocks!!
@ronbentley1 Which one did you get?
Remember the Arduino IDE works with Arduino and clones, several Espressif and clones, and now even Raspberry Pi Pico. They all have a place.
First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, 360, fairly knowledge in PC plus numerous MPU's & MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
My personal scorecard is now 1 PC hardware fix (circa 1982), 1 open source fix (at age 82), and 2 zero day bugs in a major OS.
Hi Ron,
I went for:
JZK ESP-32S ESP32 Development Board 2.4GHz WiFi + Bluetooth Dual Mode Antenna Module with Ultra Low Power
I cant believe how cheap these boards are!!
Looking forward to some new challenges.
Btw, do you know of or have any good EPS/linux API documentation?
Working inside an operating system will be a treat. I have some unix experience somewhere in the distant past, so move to linux should be relatively pain free! Ha, ha!
Ron B
Ron Bentley
Creativity is an input to innovation and change is the output from innovation. Braden Kelley
A computer is a machine for constructing mappings from input to output. Michael Kirby
Through great input you get great output. RZA
Gauss is great but Euler rocks!!
@ronbentley1 Never heard of that board, AI-Thinker or WROOM or NodeMCU. If you are in 'learn' mode, using the recommended board of the instructor is good judgement. I just learned that the hard way and had to delay my project by a few weeks while a 'correct' board is shipped. By correct, I mean the pins are where they are supposed to be and labelled correctly, my OLD (no longer on the corp website) board is different and I would rather not spend my time trying to map the pins. I don't know what you bought, but good luck with the clone of a clone.
No idea what EPS is, Linux API should just google but why? ESP32's are FreeRTOS OS, more like multics than unix. I spent over a decade using a multics derivative for global market data and held the world speed record at one point on really old slow chips, MC68000 detuned to 5mHz (time frame is early 80's). I accomplished that by using direct kernel API saving a few milliseconds or microseconds per call. That really adds up fast. I wrote my own queueing, tasking, and we didn't write to disk until the market traffic was slow enough, everything in memory.
I started to look at the ESP32 API for interrupts, tasking, CPU affinity, priority but decided to spend my resource elsewhere. Keep in mind, I am building a WiFi capable relay with 3 push buttons that have an almost 0 chance of more than 1 being pushed at once, 2 people, 2 PB's in one room, 1 in the other. Better odds at the lottery. I was only pursuing the performance route with debounce because that's my thing and old habits die hard. Now I will probably just delay a few 10's of ms, no need for anything more fancy.
Have fun digging into RTOS, and keep us posted. There are existing libraries BTW. See attached pic for a start.
First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, 360, fairly knowledge in PC plus numerous MPU's & MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
My personal scorecard is now 1 PC hardware fix (circa 1982), 1 open source fix (at age 82), and 2 zero day bugs in a major OS.