bounce2 question

Hello.

I'm using 4 pushbuttons on 4 digital inputs.
I only want to know if the code below will debounce the buttons.

I followed the example of the Library "bounce2buttons"

Have you an explanation about how to use .risingEdge() and .fallingEdge().

#include <Bounce2.h>

int ButtonNb;

const int buttonPin1 = 3;
const int buttonPin2 = 4;
const int buttonPin3 = 5;
const int buttonPin4 = 6;

Bounce pushbutton1 = Bounce(buttonPin1, 10);
Bounce pushbutton2 = Bounce(buttonPin2, 10);
Bounce pushbutton3 = Bounce(buttonPin3, 10);
Bounce pushbutton4 = Bounce(buttonPin4, 10);

void setup() {

  Serial.begin(9600);

  pinMode(buttonPin1, INPUT);
  pinMode(buttonPin2, INPUT);
  pinMode(buttonPin3, INPUT);
  pinMode(buttonPin4, INPUT);

  pushbutton1.attach(buttonPin1);
  pushbutton2.attach(buttonPin2);
  pushbutton3.attach(buttonPin3);
  pushbutton4.attach(buttonPin4);

  pinMode(8 , OUTPUT);
  pinMode(9 , OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(11, OUTPUT);

}

void loop() {
  pushbutton1.update();
  pushbutton2.update();
  pushbutton3.update();
  pushbutton4.update();

  int Button1 = pushbutton1.read();
  int Button2 = pushbutton2.read();
  int Button3 = pushbutton3.read();
  int Button4 = pushbutton4.read();

  if (Button2 == 0 && Button3 == 0 && Button4 == 0) {
    if (Button1 == 1) {
      ButtonNb = 1;
      digitalWrite(8, HIGH);
      digitalWrite(9, LOW);
      digitalWrite(10, LOW);
      digitalWrite(11, LOW);
    }
  }

  if (Button1 == 0 && Button3 == 0 && Button4 == 0) {
    if (Button2 == 1) {
      ButtonNb = 2;
      digitalWrite(8, LOW);
      digitalWrite(9, HIGH);
      digitalWrite(10, LOW);
      digitalWrite(11, LOW);
    }
  }

  if (Button1 == 0 && Button2 == 0 && Button4 == 0) {
    if (Button3 == 1) {
      ButtonNb = 3;
      digitalWrite(8, LOW);
      digitalWrite(9, LOW);
      digitalWrite(10, HIGH);
      digitalWrite(11, LOW);
    }
  }

  if (Button1 == 0 && Button2 == 0 && Button3 == 0) {
    if (Button4 == 1) {
      ButtonNb = 4;
      digitalWrite(8, LOW);
      digitalWrite(9, LOW);
      digitalWrite(10, LOW);
      digitalWrite(11, HIGH);
    }
  }

  if (Button1 == 0 && Button2 == 0 && Button3 == 0 && Button4 == 0) {
    ButtonNb = 0;

    digitalWrite(8, LOW);
    digitalWrite(9, LOW);
    digitalWrite(10, LOW);
    digitalWrite(11, LOW);
  }

  Serial.println(ButtonNb);
}

I only want to know if the code below will debounce the buttons.

Yes. That is what that library is for.

Have you an explanation about how to use .risingEdge() and .fallingEdge().

risingEdge() and fallingEdge() are described as deprecated. You should not be using them.

rose() and fell() are the replacements, and what they do is described in the header file.

Mouldolas:
Have you an explanation about how to use .risingEdge() and .fallingEdge().

As PaulS explained, they have been deprecated, and in fact, if you look at Bounce2.h you will see that they simply return the value of .rose() and .fell(). That said, if your pushbutton is configured so that when "pressed" it provides a direct path for the pull-up resistor (external, I hope) to ground, the signal on the pin will transition from HIGH to LOW. This would mean your condition would look for .fell() to be true when the button is pressed. If it is configured to provide a direct source path for a pull-down resistor, the signal pin will transition from LOW to HIGH. This would mean your condition would look for .rose() to be true when the button is pressed.

Thank you for the details.

In my code i never used Rose() nor Fell().
Can you tell me if the code as it is will debouce the buttons without Rose() nor Fell()?

Excuse me for the English.

Mouldolas:
Thank you for the details.

In my code i never used Rose() nor Fell().
Can you tell me if the code as it is will debouce the buttons without Rose() nor Fell()?

Excuse me for the English.

Run your code.

Just for the fun of it, you could also try TButton from TDuino:

#include <TDuino.h>

#define BUTTON_COUNT 4
const byte BUTTON_PINS[BUTTON_COUNT] = { 3, 4, 5, 6 };
TButton buttons[BUTTON_COUNT];
byte buttonFlag = 0, idx;

void buttonPress(byte pin, int state)
{
  //Turn on the bit that represents the button:
  buttonFlag |= (1 << (pin-3));

  //Serial.print(F("Button"));
  //Serial.print(pin-3);
  //Serial.print(F(" press"));
}

void buttonRelease(byte pin, int state)
{
  //Clear the bit that represents the button:
  buttonFlag &= ~(1 << (pin-3));

  //Serial.print(F("Button"));
  //Serial.print(pin-3);
  //Serial.print(F(" release"));
}

void setup()
{
  for (idx = 0; idx < BUTTON_COUNT; idx++)
  {
    buttons[idx].attach(BUTTON_PINS[idx]);
    buttons[idx].onPress(buttonPress);
    buttons[idx].onRelease(buttonRelease);
  }
}

void loop()
{

  //Update the buttons
  for (idx = 0; idx < BUTTON_COUNT; idx++) buttons[idx].loop();

  //Check the current button state
  switch (buttonFlag) {
    case 1:
      Serial.println(F("Button1"));
      break;
    case 2:
      Serial.println(F("Button2"));
      break;
    case 4:
      Serial.println(F("Button3"));
      break;
    case 8:
      Serial.println(F("Button4"));
      break;
  }
}

The benefits of the code above is that you can check for multiple buttons pressed, eg. if button 1 and 2 are pressed, "buttonFlag" would be 3. Or if button 3 and 4 are pressed, buttonFlag would be 12 and so on.

The code is not tested, though.. :slight_smile:

Is it required to use rose() or fell() if i want to debounce my buttons?

Mouldolas:
Is it required to use rose() or fell() if i want to debounce my buttons?

NO!