Adding a digitalDebounce(pin) function

55 views
Skip to first unread message

Jacques Bellavance

unread,
Sep 25, 2017, 10:36:20 AM9/25/17
to Developers
Hi all,

I have been around the Forum for a little while now, and I noticed that a lot of newbies are not aware that a switch should be debounced. And once they are aware, they have a hard time understanding the "standard" wait a little bit before confirming a state change way of debouncing.

I understand that the whole Arduino environment is designed to help students and artists using technology for IoT and other uses. Switches are amongst the mostly used pieces of hardware in any setup to do IoT.

For those reasons, I think that having a debouncer as part of the language would be a good idea. There are a lot of debouncing algorithms available, and I can see that it would be a hard to choose which one to use. Some are blocking, some are not. some are fast, some take more time to give a positive feedback. So, basically, there will be some pros and cons. What I suggest is just a new tool to deal with the problem without having to resort to include a Library (often regarded as a no no on the Forum) or to have to code it in the sketch every time one would use a switch.

I would like to have your feedback on this.

Thank you all.

Andrew Kroll

unread,
Sep 25, 2017, 11:02:43 AM9/25/17
to Arduino Developers
Best to do in hardware, very hard to beat hardware-specific r-c or l-c debounce circuits.

--
You received this message because you are subscribed to the Google Groups "Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to developers+unsubscribe@arduino.cc.

Brian Cook

unread,
Sep 25, 2017, 11:21:20 AM9/25/17
to devel...@arduino.cc

> they have a hard time understanding the "standard" wait a little bit
> before confirming a state change way of debouncing

You have that backwards.  The wait is for the switch to stop bouncing.

For a human manipulating a switch it is a distinction without difference.

For a sensor on an internal combustion engine it is the difference
between running smoothly and knocking like a jackhammer.



Jacques Bellavance

unread,
Sep 25, 2017, 11:28:40 AM9/25/17
to Developers, bc...@rowdydogsoftware.com
Hi Brian,

I was reffering to Brian Ouellet Frederick' debounce Library:
int Debounce::update()
{
uint8_t newState = digitalRead(pin);
if (state != newState ) {
  if (millis() - previous_millis >= interval_millis) {
  previous_millis = millis();
  state = newState;
  return 1;
}
  }


It waits for interval_millis, It does not wait for the the switch to stop bouncing

Brian Cook

unread,
Sep 25, 2017, 11:36:39 AM9/25/17
to devel...@arduino.cc

I was reffering to Brian Ouellet Frederick' debounce Library:
int Debounce::update()
{
uint8_t newState = digitalRead(pin);
if (state != newState ) {
  if (millis() - previous_millis >= interval_millis) {
  previous_millis = millis();
  state = newState;
  return 1;
}
  }


It waits for interval_millis, It does not wait for the the switch to stop bouncing

If that's the actual code it has an obvious bug.

And, it delays after the first transition which, as I suggested, wreaks havoc for some applications.


Jacques Bellavance

unread,
Sep 25, 2017, 11:44:12 AM9/25/17
to Developers, bc...@rowdydogsoftware.com


On Monday, September 25, 2017 at 11:36:39 AM UTC-4, Brian Cook wrote:

If that's the actual code it has an obvious bug.

And, it delays after the first transition which, as I suggested, wreaks havoc for some applications.

This is what newbies are told to do on the Forum...

Gabriel Staples

unread,
Sep 25, 2017, 1:04:34 PM9/25/17
to devel...@arduino.cc, bc...@rowdydogsoftware.com
Here's my implementation of it: 



I'm sure it could be improved or simplified, but it fits my needs well. I think it's important to not just make a library that reads and debounces a button on a digital pin by the way, but anything, really. For example: what if you are using a resistor ladder to read 10 buttons on a single analog pin...you'll need a more versatile solution like what I've implemented here instead of just a button debounce library that does the reading inside of it. My library expects the user to pass in the bouncing value and the library will debounce it using millis(). It is completely non-blocking.


Thank you!

Sincerely, 

Gabriel Staples
Electric RC Aircraft Guy, LLC


--

Alex Albino

unread,
Sep 25, 2017, 1:15:41 PM9/25/17
to devel...@arduino.cc, bc...@rowdydogsoftware.com
... mind if I test it on an SAM D21E board? (Arduino Zero derivative)

I'll report back shortly.

Gabriel Staples

unread,
Sep 25, 2017, 1:20:14 PM9/25/17
to devel...@arduino.cc
Sounds good. I look forward to the results. Please note that the comments in the example are not up-to-date. They pertained to an older implementation I had, so read them with that in mind. Some may not fully make sense at the moment. 


Thank you!

Sincerely, 

Gabriel Staples
Electric RC Aircraft Guy, LLC


Jacques Bellavance

unread,
Sep 25, 2017, 2:05:33 PM9/25/17
to Developers, bc...@rowdydogsoftware.com
 For example: what if you are using a resistor ladder to read 10 buttons on a single analog pin...you'll need a more versatile solution like what I've implemented here instead of just a button debounce library that does the reading inside of it. My library expects the user to pass in the bouncing value and the library will debounce it using millis().

That is an interesting point.

I am just asking if it would be feasible to add digitalDebounce(pin) the the Arduino' function. If it is possible, then we could then look at algorithms to implement it. and even make it analogDebounce(pin) like you do it.

Jacques

Paul Stoffregen

unread,
Sep 25, 2017, 2:13:56 PM9/25/17
to devel...@arduino.cc
I've long believed Frederick's Bounce2 library (which supersedes the old Debounce library) should be one of the libraries bundled with the Arduino IDE.  For years, my installer for Teensy has just put a copy of this library into the IDE.  Most of the examples I've written in the last few years use it.  Handling mechanical chatter and even just properly retaining prior state and detecting changes are repetitive tasks perfect for a library.  This library really does help beginners avoid those common pitfalls and get their project started on solid footing, at least for user input by pushbuttons & switches.
--
You received this message because you are subscribed to the Google Groups "Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to developers+...@arduino.cc.

Gabriel Staples

unread,
Sep 25, 2017, 2:14:37 PM9/25/17
to devel...@arduino.cc
Clarification: "My library expects the user to pass in the bouncing [*state*, not value] and the library will debounce it using millis()."

In other words, if there was something more complicated like an analogRead thing going on to read a resistor ladder with many buttons, the user would decode that themselves then just pass in a boolean state to the debouncer as: "button1.readEvent(buttonState);". Then the user can call "button1.getDebouncedAction();" to see if the button was just PRESSED or just RELEASED, and they can call "button1.getDebouncedState();" to see if the button is currently in a pressed state or in a not_pressed state. 

Ex:

  byte buttonState = digitalRead(buttonPin);
  button1.readEvent(buttonState);
  
  int8_t button_action = button1.getDebouncedAction();
  boolean button_state = button1.getDebouncedState();

....I should probably call it "debounceState()" instead of "readEvent()" but you get the idea...

I suppose the downside of debouncing buttons (in a non-blocking way) in the Arduino core, instead of via an external library, is that it would add a bunch of boolean memory values which take up memory in order to track the last known state of each pin. I'd say make it a library that is part of the Arduino core, but not a core function like digitalRead() is a core function. 



Thank you!

Sincerely, 

Gabriel Staples
Electric RC Aircraft Guy, LLC


--

Alex Albino

unread,
Sep 25, 2017, 2:25:42 PM9/25/17
to devel...@arduino.cc
Paul, would you mind posting a link to the library? Thanks in advance.

To unsubscribe from this group and stop receiving emails from it, send an email to developers+unsubscribe@arduino.cc.

--
You received this message because you are subscribed to the Google Groups "Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to developers+unsubscribe@arduino.cc.

Paul Stoffregen

unread,
Sep 25, 2017, 2:26:53 PM9/25/17
to devel...@arduino.cc
On 09/25/2017 11:25 AM, Alex Albino wrote:
Paul, would you mind posting a link to the library? Thanks in advance.

To unsubscribe from this group and stop receiving emails from it, send an email to developers+...@arduino.cc.

jas...@earthlink.net

unread,
Sep 25, 2017, 3:16:45 PM9/25/17
to devel...@arduino.cc

May be there should be some broken examples that force the newbies  to deal with this problem of figuring out  why a switch needs to be denounced switch .

 

I have know this from college and have done my fair share of coding for denouncing switches.

 

  It's too easy to program applications where don't have worry about all the got you that you would other wise have to face if you had code from scratch.

 

 

Another  problem we have to deal with is Hysteresis which most don't under stand until you have to deal with it. I was part of project that was our key to

completing a project that other threw the towel in unknown to us at the time.

 

which -----Original Message-----
From: Jacques Bellavance
Sent: Sep 25, 2017 7:34 AM
To: Developers
Subject: [Developers] Adding a digitalDebounce(pin) function

Hi all,

I have been around the Forum for a little while now, and I noticed that a lot of newbies are not aware that a switch should be debounced. And once they are aware, they have a hard time understanding the "standard" wait a little bit before confirming a state change way of debouncing.

I understand that the whole Arduino environment is designed to help students and artists using technology for IoT and other uses. Switches are amongst the mostly used pieces of hardware in any setup to do IoT.

For those reasons, I think that having a debouncer as part of the language would be a good idea. There are a lot of debouncing algorithms available, and I can see that it would be a hard to choose which one to use. Some are blocking, some are not. some are fast, some take more time to give a positive feedback. So, basically, there will be some pros and cons. What I suggest is just a new tool to deal with the problem without having to resort to include a Library (often regarded as a no no on the Forum) or to have to code it in the sketch every time one would use a switch.

I would like to have your feedback on this.

Thank you all.

--

Paul Stoffregen

unread,
Sep 25, 2017, 3:43:20 PM9/25/17
to devel...@arduino.cc
On 09/25/2017 12:16 PM, jas...@earthlink.net wrote:
>
>   It's too easy to program applications where don't have worry about
> all the got you that you would other wise have to face if you had code
> from scratch.
>

I would beg to differ.  Arduino's tremendous success is due to the ways
it makes things easier for people.

But I believe there is still quite a lot of room for improvement.  I
have personally answered many user questions where their code became a
tangled mess after they had successfully debounced 1 button using ad-hoc
approaches involving delay, and then they tried to expand to several
buttons.  Often this work happens at a critical early stage in the
learning process, where too much frustration without enough apparent
value from the Arduino platform causes people to give up and abandon
their project (and any future use of Arduino).

Forcing people to do more coding from scratch hardly seems like a
direction appropriate for Arduino.

Jacques Bellavance

unread,
Sep 25, 2017, 3:53:25 PM9/25/17
to Developers, jas...@earthlink.net
Hi, It all depends on the definition of newbie. If the newbie is a college student that is in an engineering program, then yes, he/she should be aware of bouncing and thought how to deal with it. If the newbie is a kid, artist, enthousiast, hobbyist... then, bouncing is just an anoying extra hurdle to pass before thay can see those leds/servos/steppers... respond correctly to their switches. It is obvious that if a generic digitalDebounce(pin) is ever included in the IDE, it will be imperfect for some specific setups or environments. That is why digitalRead() is there for (amongst other things). A flat screwdriver is not usefull for Robertson screws. Neither fo Phillips, Hex, Torx... But having a Robertson screwdriver in your toolbox is not necessarely a bad thing. I am tryng to see it with the eyes of someone that does not persue the goal of earning a living coding. Jus the average Joe (or Jo-Ann).

When I talk about a switch, I am talking about human operated ones. the ones that are pressed, clicked, flipped by a human hand (or elbow...). Pressure switches will generate hysteresis, but I consider those as sensors. 

Andrew Kroll

unread,
Sep 25, 2017, 3:59:02 PM9/25/17
to Arduino Developers
Why not ask the community what they want?


To unsubscribe from this group and stop receiving emails from it, send an email to developers+unsubscribe@arduino.cc.

Alex Albino

unread,
Sep 25, 2017, 4:22:59 PM9/25/17
to devel...@arduino.cc
It's commonly agreed: If it's difficult to use, people turn away.

Perhaps it would be much more appropriate to have Button debouncing as part of a blog post with a problem, walk through, and solution.

That's not something that we need to plan out in this thread however. We are going over button libraries after all. :-)

- Alex

Jacques Bellavance

unread,
Sep 25, 2017, 5:24:10 PM9/25/17
to Developers
Actually, I was not talking about a library, I was asking if digitalDebounce(pin) could be added as one of the standard Arduino functions, the same as digitalRead(pin) and digitalWrite(pin, STATUS). There are already too many debounce libraries out there. A cat could lose it's kittens.

If that is not foreseable, then a complete tutorial on why and how to debounce a switch (a solution that would have to be very simple and short), with a link to it directly in the digitalRead() page in Arduino's Reference would be a last resort solution in my eyes. And in this case, you are right, this thread (Suggestions for the Arduino Project) is not the right place to post that question.
To unsubscribe from this group and stop receiving emails from it, send an email to developers+...@arduino.cc.

Alex Albino

unread,
Sep 25, 2017, 5:27:34 PM9/25/17
to devel...@arduino.cc
Ah ok, I understand. Adding a standard Arduino digital debounce function is of course, a wonderful idea.

To unsubscribe from this group and stop receiving emails from it, send an email to developers+unsubscribe@arduino.cc.

Andrew Kroll

unread,
Sep 25, 2017, 5:35:00 PM9/25/17
to Arduino Developers
Here's my solution, works every time...
Enjoy the ASCII-ART schematic ;-)

                 1K
Ground <-o--/\/\/\/\--,
                      |      .1uf
                      +-------||-------+----> to pin (INPUT_PULLUP)
                      |                |
                      |       /        |           
                      `------O o-------'


To unsubscribe from this group and stop receiving emails from it, send an email to developers+unsubscribe@arduino.cc.



--
Visit my github for awesome Arduino code @ https://github.com/xxxajk

Jacques Bellavance

unread,
Sep 25, 2017, 5:39:09 PM9/25/17
to devel...@arduino.cc

I agree, hardware debounce is the best.

 

De : Andrew Kroll [mailto:xxx...@gmail.com]
Envoyé : 25 septembre 2017 17:35
À : Arduino Developers
Objet : Re: [Developers] Adding a digitalDebounce(pin) function

 

Here's my solution, works every time...

Enjoy the ASCII-ART schematic ;-)

 

                 1K

Ground <-o--/\/\/\/\--,

                      |      .1uf

                      +-------||-------+----> to pin (INPUT_PULLUP)

                      |                |
                      |       /        |           

                      `------O o-------'

 

Visit my github for awesome Arduino code @ https://github.com/xxxajk

--

Andrew Kroll

unread,
Sep 25, 2017, 5:47:28 PM9/25/17
to Arduino Developers
Yes, so, why not push more of the electronic theory _WITH_ the digital filtering theory?

To unsubscribe from this group and stop receiving emails from it, send an email to developers+unsubscribe@arduino.cc.




--

Visit my github for awesome Arduino code @ https://github.com/xxxajk

--

You received this message because you are subscribed to the Google Groups "Developers" group.

To unsubscribe from this group and stop receiving emails from it, send an email to developers+unsubscribe@arduino.cc.

--
You received this message because you are subscribed to the Google Groups "Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to developers+unsubscribe@arduino.cc.

Alex Albino

unread,
Sep 25, 2017, 5:55:23 PM9/25/17
to devel...@arduino.cc
Hardware debouncing is the ideal way to do it... but keep in mind there are good reasons to support software debouncing, such as in designs where someone forgot to add hardware debouncing. :o)


Andrew Kroll

unread,
Sep 25, 2017, 6:12:45 PM9/25/17
to Arduino Developers
Absolutely, but, then again it is still easy to add onto the physical switch.

That aside, each has an up and down side, and these are the two main concerns, which just about anyone in the know, already knows.

Hardware debounce is simplistic, is faster, more accurate, and can easily span a variety of switch quality. Problem is in the R-C values, but once worked out, just works.
With software you will have to wait some how, or use an interrupt that rechecks later. Complex, but works, and depending on implementation, adds delays *sometimes* depending on implementation.

Hardware debounce can clue you in to when your switch is no longer within tolerances and needs replacement. This isn't as easily done in software, but can be done.
Adaptive software debounce, in some cases can allow a really horrible switch to work longer, and sometimes allow it to work at all, by waiting for the contacts to actually settle. Very long delays indicate that your switch needs replacement, thus the application response suffers over time.

There is also a combination of the two as well, where you use a balance of hardware and software together, but that is beyond the scope of someone just beginning. It is also usually only employed in rotary switches, and in order to use one of those properly you really want those to be optical anyway. Mechanical as inputs for rotary is fine if you move very very slow.


Since Arduino is a teaching tool, so, let's teach!
Now you have something to post about switches and can paste it onto the Arduino site.



Gabriel Staples

unread,
Sep 25, 2017, 6:17:58 PM9/25/17
to devel...@arduino.cc
If we do this, here's an example: https://www.arduino.cc/en/Tutorial/Debounce

But...we need to either #include a library for it, making each button an object so it can store "lastButtonState" and "lastDebounceTime" into the object's private variables, or we statically allocate arrays of these values for every button on the Arduino, in the core code, right?

I'm a proponent of the former...no need to take up more statically allocated memory in the core when we are already starving for it on some boards, right? Or am I mistaken about how it would be implemented into the core code?


Thank you!

Sincerely, 

Gabriel Staples
Electric RC Aircraft Guy, LLC


To unsubscribe from this group and stop receiving emails from it, send an email to developers+unsubscribe@arduino.cc.

Jacques Bellavance

unread,
Sep 25, 2017, 6:18:59 PM9/25/17
to devel...@arduino.cc

I got a bit overwhelmed with hardware debouncing when I read that article : http://www.ganssle.com/debouncing-pt2.htm

 

It was more complicated than the 1K resistor coupled with a .1uF capacitor and there you go son, have fun.

 

De : Alex Albino [mailto:aal...@femtoduino.com]
Envoyé : 25 septembre 2017 17:55
À : devel...@arduino.cc


Objet : Re: [Developers] Adding a digitalDebounce(pin) function

 

Hardware debouncing is the ideal way to do it... but keep in mind there are good reasons to support software debouncing, such as in designs where someone forgot to add hardware debouncing. :o)

 

 

 

To unsubscribe from this group and stop receiving emails from it, send an email to developers+...@arduino.cc.




--

Visit my github for awesome Arduino code @ https://github.com/xxxajk

--
You received this message because you are subscribed to the Google Groups "Developers" group.

To unsubscribe from this group and stop receiving emails from it, send an email to developers+...@arduino.cc.

--
You received this message because you are subscribed to the Google Groups "Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to developers+...@arduino.cc.




--

Visit my github for awesome Arduino code @ https://github.com/xxxajk

--
You received this message because you are subscribed to the Google Groups "Developers" group.

To unsubscribe from this group and stop receiving emails from it, send an email to developers+...@arduino.cc.

Gabriel Staples

unread,
Sep 25, 2017, 6:19:47 PM9/25/17
to devel...@arduino.cc
Correction in green below.


Thank you!

Sincerely, 

Gabriel Staples
Electric RC Aircraft Guy, LLC


On Mon, Sep 25, 2017 at 6:17 PM, Gabriel Staples <panth...@gmail.com> wrote:
If we do this, here's an example: https://www.arduino.cc/en/Tutorial/Debounce

But...we need to either #include a library for it, making each button an object so it can store "lastButtonState" and "lastDebounceTime" into the object's private variables, or we statically allocate arrays of these values for every button pin on the Arduino, in the core code, right?

Andrew Kroll

unread,
Sep 25, 2017, 6:27:42 PM9/25/17
to Arduino Developers
 Yes, R/S flip-flop is one of the more complex ones on the hardware end, but you really don't need it. Same for the one that inverts, you don't need to do that if you simply add a pull-down on the input and change ground d to V+....

Seen these before, and I don't really recommend using the R/S flip-flop at all. More complex than it needs to actually be.

I always have fun attempting to explain to clients why mechanical switches suck, and always recommend use of optical, even if we have to fabricate them.

As far as the "son" comment, LOL. I'm a bit older than you think ;-)
I've been doing this sort of thing for 4 decades, well before Arduino existed.
No offense taken, I found it quite funny being called "son". Next time use "Sonny", be cause I'm so bright (Sunny)! LOL!

Cheers :-)


To unsubscribe from this group and stop receiving emails from it, send an email to developers+unsubscribe@arduino.cc.




--

Visit my github for awesome Arduino code @ https://github.com/xxxajk

--
You received this message because you are subscribed to the Google Groups "Developers" group.

To unsubscribe from this group and stop receiving emails from it, send an email to developers+unsubscribe@arduino.cc.

--
You received this message because you are subscribed to the Google Groups "Developers" group.

To unsubscribe from this group and stop receiving emails from it, send an email to developers+unsubscribe@arduino.cc.




--

Visit my github for awesome Arduino code @ https://github.com/xxxajk

--
You received this message because you are subscribed to the Google Groups "Developers" group.

To unsubscribe from this group and stop receiving emails from it, send an email to developers+unsubscribe@arduino.cc.

--
You received this message because you are subscribed to the Google Groups "Developers" group.

To unsubscribe from this group and stop receiving emails from it, send an email to developers+unsubscribe@arduino.cc.

--
You received this message because you are subscribed to the Google Groups "Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to developers+unsubscribe@arduino.cc.

Jacques Bellavance

unread,
Sep 25, 2017, 6:27:45 PM9/25/17
to devel...@arduino.cc

That (multiple objects) does not need to be the case. Take a look at this Library: https://github.com/j-bellavance/EdgeDebounceLite

 

 

 

De : Gabriel Staples [mailto:panth...@gmail.com]
Envoyé : 25 septembre 2017 18:20
À : devel...@arduino.cc
Objet : Re: [Developers] Adding a digitalDebounce(pin) function

 

Correction in green below.


 

Thank you!

 

Sincerely, 

 

Gabriel Staples

Electric RC Aircraft Guy, LLC

 

 

On Mon, Sep 25, 2017 at 6:17 PM, Gabriel Staples <panth...@gmail.com> wrote:

If we do this, here's an example: https://www.arduino.cc/en/Tutorial/Debounce

 

But...we need to either #include a library for it, making each button an object so it can store "lastButtonState" and "lastDebounceTime" into the object's private variables, or we statically allocate arrays of these values for every button pin on the Arduino, in the core code, right?

Mikael Patel

unread,
Sep 25, 2017, 6:49:30 PM9/25/17
to Developers
Some inspiration could be gained by a quick study of the hardware support available in SAM/SAMD PIO. This includes a low frequency sampling of the input pins to implement debouncing. A possible solution (for AVR) would be to 1) introduce a new input pin mode, for instance INPUT_DEBOUNCE, 2) add a 50-60 ms pin register sampler to the millis-timer interrupt handler, 3) modify digitalRead() to check the new pin mode and in the case of INPUT_DEBOUNCE mode get the value from the pin sample (one for each physical register). The SAM implementation would use the available hardware support.

Might as well take the opportunity to link to an ultra high performance GPIO library and Debounce Input Pin (Button) support.
https://github.com/mikaelpatel/Arduino-GPIO
https://mikaelpatel.github.io/Arduino-GPIO/
https://mikaelpatel.github.io/Arduino-GPIO/d1/d19/classButton.html

Cheers!
 

Jacques Bellavance

unread,
Sep 25, 2017, 7:04:02 PM9/25/17
to devel...@arduino.cc

Actually, what I had in mind is me talking to my son. (I am 62 and he is 29).

 

De : Andrew Kroll [mailto:xxx...@gmail.com]
Envoyé : 25 septembre 2017 18:28


À : Arduino Developers
Objet : Re: [Developers] Adding a digitalDebounce(pin) function

 

 Yes, R/S flip-flop is one of the more complex ones on the hardware end, but you really don't need it. Same for the one that inverts, you don't need to do that if you simply add a pull-down on the input and change ground d to V+....

Seen these before, and I don't really recommend using the R/S flip-flop at all. More complex than it needs to actually be.

I always have fun attempting to explain to clients why mechanical switches suck, and always recommend use of optical, even if we have to fabricate them.

As far as the "son" comment, LOL. I'm a bit older than you think ;-)

I've been doing this sort of thing for 4 decades, well before Arduino existed.

No offense taken, I found it quite funny being called "son". Next time use "Sonny", be cause I'm so bright (Sunny)! LOL!

Cheers :-)

 

William Westfield

unread,
Sep 28, 2017, 2:45:37 AM9/28/17
to devel...@arduino.cc

> … hardware support available in SAM/SAMD PIO ... includes a low frequency sampling of the input pins to implement debouncing.

It does? The SAM3 had some things like that, but I thought the SAMD has simplified GPIO and gotten rid of the "feature.” I can’t find anything in the SAMD datasheet.

BillW/WestfW

Mikael Patel

unread,
Sep 28, 2017, 4:57:10 AM9/28/17
to Developers
No it is the SAM3 that has the feature. The SAMD reference is all wrong. Sorry about that.

Back to the issue: My implementation proposal is that the SAM3 feature could be used as an inspiration. This implies adding a simple low frequency port sampler to the millis() timer ISR, adding a new pin mode (for instance, INPUT_DEBOUNCED) and reading the sampled port in digitalRead() when the new input mode is set. This gives a very small change to the Arduino API.

Hope we can get this discussion back on track.

Cheers!

Jacques Bellavance

unread,
Sep 28, 2017, 9:37:23 AM9/28/17
to Developers
That would be perfect. Simple, efficient... PREFECT!

Jacques

Jacques Bellavance

unread,
Sep 28, 2017, 9:43:05 AM9/28/17
to Developers
Will it work on all Arduinos?

Mikael Patel

unread,
Sep 28, 2017, 3:28:17 PM9/28/17
to Developers
I think that we can assume that not all Arduino cores would be updated. And so far there seems to be limited interest from the "Arduino Team" to your feature request. A library approach is maybe the only short-term route forward. A function/procedural approach would be:

debounceUpdate(ms) to be run in loop to sample the ports. The ms parameter could be default 50 ms.
debounceRead(pin) would return the latest sampled state of the given pin.

which is much like your original proposal. The memory requirement would be a byte (SRAM) per port (AVR) and a possible pin-sample-byte mapping table (PROGMEM).

Cheers!

Jacques Bellavance

unread,
Sep 28, 2017, 4:04:52 PM9/28/17
to Developers
Ok,

I will see in the Forum if there is a prefered way of going about it, either using a Library or proposing an ad hoc Function that could read multiple pins.

Thanks for your insights.

Jacques
Reply all
Reply to author
Forward
0 new messages