de- Bounce procedure

Hi all given many people's schemas purely bank on wait time for debouncing I have given quite a lot of unqualified though to other methods.

1] Why not use an interrupt SR., to determine that the switch has been depressed?

2] Why not use a CD4043B or CD4069B to latch a switch very quickly and with very little current 300 nano amps I think. These two chips are mentiuoned because I've used them often in analogue stuff but if there are others; better, then I'd be pleased to know.

For interrupts: I realise that on some chips the quantity of available pins are very small but hey can't that be managed, the financial overhead is zilch and the requisite hardware for off chip interrupting or latched switching is not present.

For latched switching well its dead fast, 2nd I imagine to on board interrupts, but still very quick positive and bang secure with the first bounced blip over 300nA's doing the business.

There's also RC., circuiting but its messy carries a hardware clumsiness and of course an almost fixed but calculable delay.

I know I could have written a real load of nonsense but forgive that and tell me whats best please.

So come on you guys that really know....................What do you consider the best debounce method.

Confused of Cheshire,
ChrisPSR

Depending on the use of the button...jut use a small capacitor to soak up the bounces?
Is this not an option?

I just time it out in software, then Press & Hold can be done also with updates at a controlled rate. This allows 5 per second for example:

  //Check to see if button "A" is pressed.
  if (millis() - APresstime >=200){  // allow button read after 200mS debounce time
    if (digitalRead(RemoteA) == LOW)  { // button pin with internal pullup, button press connects to Gnd.
      APresstime = millis(); // set up time for when next press is allowed
      Serial.println("A pressed"); // debug statement, repllace with your action
    }
  }

I really like Mr. Gammon's switch manager.
http://gammon.com.au/Arduino/SwitchManager.zip

Found on this page at the bottom of reply #1

ChrisPSR:
So come on you guys that really know....................What do you consider the best debounce method.

First, there are two different issues that are often confused. Switch bounce is where the mechanics of the switch create several on-off-on-off signals for what we humans think should be a single ON (or off). A short time interval between successive digitalRead()s solves that.

Then there is the business of detecting the change in state of a switch - i.e. acting on the switch when the state changes from (say) OFF to ON and ignoring continuous periods of ON and the transition from ON to OFF. This requires saving the value from the previous reading and comparing it with the new reading.

But before thinking of a code solution you should be certain there is actually a problem that needs to be solved. It is silly to waste time (or code) on a non-existent problem. Code to deal with bouncing and/or state-change detection is probably only needed when one button is required to do several (or repeated) tasks - such as incrementing a counter.

The code in Several Things at a Time uses a simple time interval to avoid bounce. The code in Planning and Implementing a Program does not need any debounce code.

...R

CrossRoads:
I just time it out in software, then Press & Hold can be done also with updates at a controlled rate. This allows 5 per second for example:

  //Check to see if button "A" is pressed.

if (millis() - APresstime >=200){  // allow button read after 200mS debounce time
    if (digitalRead(RemoteA) == LOW)  { // button pin with internal pullup, button press connects to Gnd.
      APresstime = millis(); // set up time for when next press is allowed
      Serial.println(“A pressed”); // debug statement, repllace with your action
    }
  }

Thank you CrossRoads,

That really must be the shortest algorithm I’ve come across. I have for some time been using a time delay of 5mS’s in between determining that the last switch state has changed from low to high but I think that 5mS’s can be foreshortened. I have been playing with the TI., MSP430G2 Arduino like “LaunchPad” and I came across an example of switching recognition in pure C which utilised and Interrupt SR., which seemed to me very simple, positive and probably almost [parts of a mS’s] instantaneous, the example was built in MSVC 2008.

My reason for raising the question was to find out why switching by Interrupt or an external latching switch was not offered up by anyone (that I can determine) in my favorite land of Arduino. Is there some reason why interrupts are perhaps a No-No in Arduino’ese for switching?

Once more thanks for your help, which is very much appreciated.
Chris S.

ChrisPSR:
My reason for raising the question was to find out why switching by Interrupt or an external latching switch was not offered up by anyone (that I can determine) in my favorite land of Arduino. Is there some reason why interrupts are perhaps a No-No in Arduino'ese for switching?

I can think of two reasons, but I would not go as far as calling it a no-no.

I like to think ahead. Interrupts are necessary for some purposes, so I reserve them for those. On a machine with limited resources, it's wise to leave them available for things that require them, not for things that are just able to use them.

The other is complexity. Why make your life more difficult than it already is?

By the way, latching circuits can only support one switch, which has to be a double throw switch. Polling software debounce can easily support a switch matrix.

aarg:
I can think of two reasons, but I would not go as far as calling it a no-no.

I like to think ahead. Interrupts are necessary for some purposes, so I reserve them for those. On a machine with limited resources, it's wise to leave them available for things that require them, not for things that are just able to use them.

The other is complexity. Why make your life more difficult than it already is?

By the way, latching circuits can only support one switch, which has to be a double throw switch. Polling software debounce can easily support a switch matrix.

Hi thanks for your input.

The latching switch family I have proffered carry such a specification, provide 4 SR & E's. Is there something I don't understand that limits these to just one of those switches.
Isn't the amount of work involved in using the interrupt procedure in 'C' minuscule compared with any other surely, or once more is there something I don't understand. On the chip I was using the interrupts could be used on any pin eg. P1IE |= BIT3 // Code: [P1IE refers to all Ports on the P1 Register and IE= InterruptEnable] [NotEqual used as all ports are set to INPUT by default] [BIT3 refers to Port 3 of the PORT1 register] and so on for the other 6 PORT1 Ports. I respect that this is may not be applicable to all IC's but hey whose head is buried in a particular chip [manufacturer] when another offers a better specification for your particular shema and I ask you to remember that many of the TI Low powered IC's now can be operated almost exactly as the AVR's can on an Arduino board and in the Arduinoese 'C' language?

How exactly do you propose to use a SPST switch with a SR latch?

Robin2:
First, there are two different issues that are often confused. Switch bounce is where the mechanics of the switch create several on-off-on-off signals for what we humans think should be a single ON (or off). A short time interval between successive digitalRead()s solves that.

Then there is the business of detecting the change in state of a switch - i.e. acting on the switch when the state changes from (say) OFF to ON and ignoring continuous periods of ON and the transition from ON to OFF. This requires saving the value from the previous reading and comparing it with the new reading.

But before thinking of a code solution you should be certain there is actually a problem that needs to be solved. It is silly to waste time (or code) on a non-existent problem. Code to deal with bouncing and/or state-change detection is probably only needed when one button is required to do several (or repeated) tasks - such as incrementing a counter.

The code in Several Things at a Time uses a simple time interval to avoid bounce. The code in Planning and Implementing a Program does not need any debounce code.

...R

Surely if you merely providing enough time to make a cup of tea in between depressing a switch and carrrying out its pupose then you might just as well do this in one simple line "delay = BrewUp; return;". Now if this is the case then why have thousands and thousands of electronics engineers spent many many lines on the subject of debounce? Don't believe that this reply suggests that your time delay is nonsence as I do in fact concur. The only really effective alternative for a positive switch on is by way of interrupt and is terribly easy to implement in pure 'C'. Why not?

Chris S

aarg:
How exactly do you propose to use a SPST switch with a SR latch?

Press a switch; so long as it imits 3nA's then the switch will 'set' one selected and 'reset' any others that have been selected. Else: disable the enable circuit and kill any switching. QED.,?

ChrisPSR

ChrisPSR:
Press a switch; so long as it imits 3nA's then the switch will 'set' one selected and 'reset' any others that have been selected. Else: disable the enable circuit and kill any switching. QED.,?

ChrisPSR

I have no idea what you're talking about. An S/R has two inputs. An SPST switch can only operate one of them.

Also, you need to explain why you believe that using an interrupt will, by or in virtue of itself, debounce a switch. Because that is contrary to common engineering knowledge.

ChrisPSR:
The only really effective alternative for a positive switch on is by way of interrupt and is terribly easy to implement in pure 'C'. Why not?

I suspect you are over-thinking the problem.

Suppose I want to press a button and have a servo move to the left and press a different button and have the servo move to right. I don't care how many times the switch bounces. I just want to act on the first time the button press is detected. By the time the servo has moved the bounces will be over.

...R

Robin2:
I suspect you are over-thinking the problem.

Suppose I want to press a button and have a servo move to the left and press a different button and have the servo move to right. I don't care how many times the switch bounces. I just want to act on the first time the button press is detected. By the time the servo has moved the bounces will be over.

...R

Yes, but a very simple situation can require debouncing. For example, if I want to toggle a lamp on or off, every time a button is pushed. So some thought is always required.

aarg:
Yes, but a very simple situation can require debouncing. For example, if I want to toggle a lamp on or off, every time a button is pushed. So some thought is always required.

But why do you want a user to be able to turn a lamp on and off manually at 10Hz?

ChrisPSR:
1] Why not use an interrupt SR., to determine that the switch has been depressed?

2] Why not use a CD4043B or CD4069B to latch a switch very quickly...?

Many people choose to trade off simpler hardware for slightly more complex software. You could turn the question around: why use a latch when you can add a few lines of code instead?

An interrupt can work for initial detection. If the processor is asleep you'll need to use an interrupt. But in typical situations it isn't necessary.

aarg:
Yes, but a very simple situation can require debouncing. For example, if I want to toggle a lamp on or off, every time a button is pushed. So some thought is always required.

I did deal with that in Reply #4.

...R

Johnny010:
But why do you want a user to be able to turn a lamp on and off manually at 10Hz?

I didn't say 10Hz. What value would you use to make it do the job I specified, without any unexpected behaviour? If there was some point that you were trying to make, what is it?

I'm not sure how fast I can press a repeatedly button, probably not quite 10Hz. But I want that button to be able to respond as fast as I can push it.

Interrupts: I actually use two interrupts for buttons in one project. The first interrupt is to detect the initial press, the second is a timer interrupt which fires once the press (or release) debounce time has elapsed. But the only reason I do it that way is because the processor sleeps all the time. I don't want it to sit there spinning for 10ms or whatever just to debounce a button. It sleeps as it debounces.