Using Bounce2 library not working properly

Hello everyone, I'm using the bounce example sketch slightly modified to turn off/on my desk lamp using a 5V Relay Module KY-019 a push button and Arduino Uno.
Push button has one leg connected to GND and the other leg connected to pin 2 using the pull up resistor. The relay is connected to 5V, GND and signal to pin 6.

The issue that I'm having is that sometimes I push the button and the led on the relay slightly dims and nothing happens. It seems like maybe is not receiving enough current? I then tested my circuit with the Debounce example sketch and it works perfectly. So I'm not sure what's going on.

/*
  DESCRIPTION
  ====================
  Simple example of the Bounce library that switches the debug LED when a button is pressed.
*/
// Include the Bounce2 library found here :
// https://github.com/thomasfredericks/Bounce2
#include <Bounce2.h>

#define BUTTON_PIN 2
#define LED_PIN 6
bool light_on = true;
// Instantiate a Bounce object
Bounce debouncer = Bounce();

void setup() {

  // Setup the button with an internal pull-up :
  pinMode(BUTTON_PIN, INPUT_PULLUP);

  // After setting up the button, setup the Bounce instance :
  debouncer.attach(BUTTON_PIN);
  debouncer.interval(5); // interval in ms
  //Setup the LED :
  pinMode(LED_PIN, OUTPUT);

}

void loop() {
  // Update the Bounce instance :
  debouncer.update();

  // Get the updated value :
  int value = debouncer.read();

  // Turn on or off the LED as determined by the state :
  if ( value == LOW ) {
    light_on = !light_on;
    digitalWrite(LED_PIN, light_on );
  }

}
/*
  Debounce


  http://www.arduino.cc/en/Tutorial/Debounce
*/

// constants won't change. They're used here to set pin numbers:
const int buttonPin = 2;    // the number of the pushbutton pin
const int ledPin = 6;      // the number of the LED pin

// Variables will change:
int ledState = HIGH;         // the current state of the output pin
int buttonState;             // the current reading from the input pin
int lastButtonState = HIGH;   // the previous reading from the input pin

// the following variables are unsigned longs because the time, measured in
// milliseconds, will quickly become a bigger number than can be stored in an int.
unsigned long lastDebounceTime = 0;  // the last time the output pin was toggled
unsigned long debounceDelay = 50;    // the debounce time; increase if the output flickers

void setup() {
  pinMode(buttonPin, INPUT_PULLUP);
  pinMode(ledPin, OUTPUT);

  // set initial LED state
  digitalWrite(ledPin, ledState);
}

void loop() {
  // read the state of the switch into a local variable:
  int reading = digitalRead(buttonPin);


  // If the switch changed, due to noise or pressing:
  if (reading != lastButtonState) {
    // reset the debouncing timer
    lastDebounceTime = millis();
  }

  if ((millis() - lastDebounceTime) > debounceDelay) {
    // whatever the reading is at, it's been there for longer than the debounce
    // delay, so take it as the actual current state:

    // if the button state has changed:
    if (reading != buttonState) {
      buttonState = reading;

      // only toggle the LED if the new button state is HIGH
      if (buttonState == LOW) {
        ledState = !ledState;
      }
    }
  }

  // set the LED:
  digitalWrite(ledPin, ledState);

  // save the reading. Next time through the loop, it'll be the lastButtonState:
  lastButtonState = reading;
}

In your bounce2 code, you are not looking at state changes like with your debounce code. debouncer.read() is like digitalRead(). For your button wiring, you need to use debouncer.fell().

/*
  DESCRIPTION
  ====================
  Simple example of the Bounce library that switches the debug LED when a button is pressed.
*/
// Include the Bounce2 library found here :
// https://github.com/thomasfredericks/Bounce2
#include <Bounce2.h>

#define BUTTON_PIN 2
#define LED_PIN 6
bool light_on = true;
// Instantiate a Bounce object
Bounce debouncer = Bounce();

void setup() {

  // Setup the button with an internal pull-up :
  pinMode(BUTTON_PIN, INPUT_PULLUP);

  // After setting up the button, setup the Bounce instance :
  debouncer.attach(BUTTON_PIN);
  debouncer.interval(5); // interval in ms
  //Setup the LED :
  pinMode(LED_PIN, OUTPUT);

}

void loop() {
  // Update the Bounce instance :
  debouncer.update();

  // Get the updated value :
  //int value = debouncer.read();

  // Turn on or off the LED as determined by the state :
  //if ( value == LOW ) {
  if (debouncer.fell() ) {
    light_on = !light_on;
    digitalWrite(LED_PIN, light_on );
  }

}

You could also use TButton from TDuino like this:

#include <TDuino.h>

#define LED_PIN 6
#define BUTTON_PIN 2

void buttonPress(byte pin, int state)
{
  digitalWrite(LED_PIN, digitalRead(LED_PIN) ^ 1); //Invert state
}

TButton button;

void setup()
{
  button.attach(BUTTON_PIN);
  button.onPress(buttonPress);
}

void loop()
{
  button.loop();
}

Thank you cattledog and Danois90 for your responses. I used debounce.fell() and it worked like it should, thank you!

if debouncer.read() is like digitalRead() shouldn't it be the same thing as debouncer.fell() since I'm waiting for the int value go from HIGH to LOW and then turn on my lamp?

if debouncer.read() is like digitalRead() shouldn't it be the same thing as debouncer.fell() since I'm waiting for the int value go from HIGH to LOW and then turn on my lamp?

Not in the example you posted. If the button were held down for more than a loop() and debounce response cycle, the output would toggle. .fell() picks up the transition like the debounced state change example.

cattledog:
Not in the example you posted. If the button were held down for more than a loop() and debounce response cycle, the output would toggle. .fell() picks up the transition like the debounced state change example.

thank you