Debounce Contact

Folks
I'm truing to get a debounce timer to work correctly.
There are 2 buttons that need the debounce , gear_R++ and gear_R-
With out the debnounce the code works fine. With not so much. Can any provide some guidance?

The code is as follows

(defined at the beginning)
unsigned long lastDebounceTime = 0;
unsigned long debounceDelay = 50;

void loop() {

if(digitalRead(B_R)==HIGH)
{
// read the state of the switch into a local variable:
int reading = digitalRead(B_G_F);

if (reading != lastButtonState) {
// reset the debouncing timer
lastDebounceTime = millis();
}

if ((millis() - lastDebounceTime) > debounceDelay) {

if (reading != buttonState) {
buttonState = reading;
if(((buttonState)==HIGH)&&(gear_R<4))
{
gear_R++;
}
else if((buttonState==HIGH)&&(gear_R>0))
{
gear_R--;
}
}
}
lastButtonState = reading;

Denouncing
https://forum.arduino.cc/index.php?topic=719995.0

Please use code tags </> for your code.

Thanks

Have a look at the code in my DebouncedSwitch library, tutorial here.

drmpf
How do i debounce 2 switch contacts?

PerryBebbington:
Denouncing
Buttons and other electro-mechanical inputs (introduction) - General Electronics - Arduino Forum

Please use code tags </> for your code.

Thanks

Bad contact, you are a bad contact, consider yourself denounced . . .

:frowning:

You can install Bounce2 using the Arduino IDE's Library Manager. It works very well and includes examples.

#include <DebouncedSwitch.h>
DebouncedSwitch sw4(4); // monitor a switch on input D4
DebouncedSwitch sw5(5); // monitor a switch on input D5
void loop() {
  sw4.update(); // call this every loop to update switch state
  sw5.update(); // call this every loop to update switch state
// etc
  if (sw4.isDown()) {
     // do something
   }
  if (sw5.isChanged()) { // debounced switch changed state Up or Down
    // isChanged() is only true for one loop(), cleared when update() called again
    if (!sw5.isDown()) { // switch was just released
      // do something first time switch becomes down
    }
  }

Simple and effective debouncing can be accomplished by scanning switches every 50ms.

Use change in state handling for your switches.

larryd:
Bad contact, you are a bad contact, consider yourself denounced . . .

++Karma; // LMAO ! ! !
I'm sure you look out for my silly mistakes :-[

PerryBebbington:
++Karma; // LMAO ! ! !
I'm sure you look out for my silly mistakes :-[

No, just having fun in this time of Covid.


Mistakes, I’ve made some whoppers in the past and sure more are to come :drooling_face:

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.