First time using bounce library

My project uses 2 buttons for input. Button pin 1 should acts as follows: 1 click led1 on, 2 click led2 on led1 off,3 click both on, 4 click both off. Button pin 2 should act as follows: 1 click led3 on, 2 click led3 off. This is my first time using a library in my code. I am searching for a better way to debounce multiple buttons in my program. Below is my code

/* 
DESCRIPTION
====================
Simple example of the Bounce library that switches the debug LED when 
either of 2 buttons are pressed.
*/

// Include the Bounce2 library found here :
// https://github.com/thomasfredericks/Bounce2
#include <Bounce2.h>

#define BUTTON_PIN_1 2
#define BUTTON_PIN_2 3



// Instantiate a Bounce object
Bounce debouncer1 = Bounce(); 

// Instantiate another Bounce object
Bounce debouncer2 = Bounce(); 
int COUNT1 = 0;
int COUNT2 = 0;
int LED_PIN1 = 6;
int LED_PIN2 = 7;
int LED_PIN3 = 8;

void setup() {

 // Setup the first button with an internal pull-up :
 pinMode(BUTTON_PIN_1,INPUT);
 // After setting up the button, setup the Bounce instance :
 debouncer1.attach(BUTTON_PIN_1);
 debouncer1.interval(50); // interval in ms
 
  // Setup the second button with an internal pull-up :
 pinMode(BUTTON_PIN_2,INPUT);
 // After setting up the button, setup the Bounce instance :
 debouncer2.attach(BUTTON_PIN_2);
 debouncer2.interval(50); // interval in ms


 //Setup the LED :
 pinMode(LED_PIN1,OUTPUT);
 pinMode(LED_PIN2,OUTPUT);
 pinMode(LED_PIN3,OUTPUT);

}

void loop() {
 // Update the Bounce instances :
 debouncer1.update();
 delay(50);
 debouncer2.update();
 delay(50);

 // Get the updated value :
 int value1 = debouncer1.read();
 int value2 = debouncer2.read();

 // Turn on the LED if either button is pressed :
 if ( value1 == HIGH) {
   COUNT1++;
 } 
  if ( value2 == HIGH) {
   COUNT2++;
 } 
  if (COUNT1 == 4) {
   COUNT1 = 0;
   digitalWrite(LED_PIN2, LOW);
   digitalWrite(LED_PIN1, LOW);
}
  if (COUNT1 == 1) {
   digitalWrite(LED_PIN1, HIGH);
 } 
 if (COUNT1 == 2) {
   digitalWrite(LED_PIN2, HIGH);
 digitalWrite(LED_PIN1, LOW);
 }
if (COUNT1 == 3) {
   digitalWrite(LED_PIN2, HIGH);
   digitalWrite(LED_PIN1, HIGH);
   }
 if (COUNT2 == 2) {
   COUNT2 = 0;
   digitalWrite(LED_PIN3, LOW); 
}
  if (COUNT2 == 1) {
   digitalWrite(LED_PIN3, HIGH);
 } 
else
   digitalWrite(LED_PIN3, LOW);

}

The buttons just turn on and off sporadically as it is now. I would love some instruction and help so I can better learn how to use librarys!! Or just any better way to monitor several inputs at once.

Thanks all in advance for any help.

Moderator edit:
</mark> <mark>[code]</mark> <mark>

</mark> <mark>[/code]</mark> <mark>
tags added.

bounce2buttons.ino (1.96 KB)

First, please read the sticky post, "How to use this forum," posted at the top of each topic listing in the forum. Please pay particular attention to the part about using "code" tags. Without them, the code listing can be garbled, when the forum software interprets characters in the code as tags describing how the text is to be displayed. No need to apologize - instead, please edit your post to put your code between "code" tags.

You really want to be the one to figure this out. I don't think you want someone to tell you what's wrong with this code. I'll ask this question:

What event should make variable COUNT1 increment?

// Setup the first button with an internal pull-up :
  pinMode(BUTTON_PIN_1,INPUT);

Shouldn't that be

pinMode(BUTTN_PIN_1, INPUT_PULLUP);

To match the comment?

Thanks for the advice tmd3 and yeah I just would like to be pointed in the correct direction on this program Ive just never used librarys at all so I was just wanting some pointers. Or I would like to know if anyone knows of a good way to debounce multiple switches that I may have missed? Also COUNT1 should increment when value1 goes high. value1 should go high when debouncer1 reads the switch went high.

Ground fungus sorry that was just a comment that I missed and didnt delete. I am using pulldowns on my switches on my breadboard.

Thank you for your time and info!

I've done it like this with a series of buttons allowed:

if (millis() - APresstime >=400){  // debounce - enough time since the last press?
    if (digitalRead (buttonA) == 0){ // pressed again?
        APresstime = millis(); // store time of the press
        Serial.print ("ButtonA pressed"); // perform the button press action
      }
 }
if (millis() - BPresstime >=400){  // debounce - enough time since the last press?
    if (digitalRead (buttonB) == 0){ // pressed again?
        BPresstime = millis(); // store time of the press
        Serial.print ("ButtonB pressed"); // perform the button press action
      }
 }
if (millis() - CPresstime >=400){  // debounce - enough time since the last press?
    if (digitalRead (buttonC) == 0){ // pressed again?
        CPresstime = millis(); // store time of the press
        Serial.print ("ButtonC pressed"); // perform the button press action
      }
 }

This also supports Press & Hold actions, with an update allowed every debounce time.

ahhh now this seems to be a great way to do this. Just monitor each button and then I can count and do what I need to do based on each of those "if" functions. This is exactly what I needed! I should be able to get something to work based off of that. Thank you very much for your knowledge!

The issue with your sketch has nothing to do with whether or not you use a library. It's a simple and common problem. When you grasp it, you might slap your forehead.

Kevinm11: ... COUNT1 should increment when value1 goes high. value1 should go high when debouncer1 reads the switch went high.

It sounds like you want to increment COUNT1 when you see a low-to-high transition on the switch. That sounds right to me. But, you're actually incrementing whenever you find the switch high, as opposed to when you see a transition. read() returns the current state of the debounced switch. For each activation of the switch, loop() executes many times; each time, it finds the switch high, increments the counters, and changes the state of the LEDs. The LEDs will change very quickly as long as the switch is depressed. I don't think that's what you want.

You have two ways to fix it. One is to save the previous state of each switch, and increment when you see the current state high and the previous state low. That will capture the transition.

The other way is to use the function rose() in the Bounce2 library. rose() returns a boolean, true when the last call to update() detected that the switch had made a low-to-high transition since the previous time update() was executed. Try changing to this:

  int value1 = debouncer1.rose();
  int value2 = debouncer2.rose();
void loop() {
 // Update the Bounce instances :
 debouncer1.update();
 delay(50);
 debouncer2.update();
 delay(50);

Why on earth would use use a library that was designed specifically for debouncing switches and then fart away a bunch of time on EVERY pass through loop()? The crappiest switch ever made is not going to bounce for 1/10th of a second.

That is great info tmd3 I will definitely give that a try because id like to save my sketch and make it work and help me learn. I was unaware of the rose() function in the bounce2 library. I think I need to do some more research on librarys and how they work exactly.

PaulS the only reason it is at delay(50) is because at first it looked to me that the delay(5) was to short and I thought it was the culprit making my LED's turn on an off sporadically. So I cranked it up to 50ms to rule that out as being my problem.

Thanks for your knowledge all

Well, information about what's available in Bounce2 isn't easy to find. The Playground page for Bounce links to a documentation page for Bounce2, but the documentation doesn't show the transition functions provided with Bounce2. That page may be out of date. The Plaground page also links to a forum post, where rose() is described in the third reply. Finding that info is a bit of a chase.

I found it by opening Bounce2.h in the library, where I found this prototype for rose():

   // Returns the rising pin state
    bool rose();

That suggests that rose() returns true when the switch state has a rising edge, but it doesn't clearly say so. Examination of the code for rose() in Bounce2.cpp shows that it's the logical and of the switch state and a change flag.

Some libraries aren't all that well-documented, and sometimes the documentation hasn't kept pace with development. Sometimes, you just have to look at the code.

ok cool thanks for the links! I will give that rose() a try this evening.

So in crossroads example

if (millis() - APresstime >=400){  // debounce - enough time since the last press?
    if (digitalRead (buttonA) == 0){ // pressed again?
        APresstime = millis(); // store time of the press
        Serial.print ("ButtonA pressed"); // perform the button press action
      }

can someone explain why you would want buttonA to equal 0? Would this mean that its reading the falling edge of the button press? or that he is using pullups on his switch? Since I am using pulldowns would I want that portion to ==1?

can someone explain why you would want buttonA to equal 0?

That is not what the code is testing. It is comparing the value read from buttonA to 0.

Would this mean that its reading the falling edge of the button press?

Depends on how the switch is wired.

Since I am using pulldowns would I want that portion to ==1?

Why not try it and see? You’ll remember more if you involve your fingers, eyes, and brain in the investigation.

Yeah so isnt it saying if buttonA is equal to 0 then Apresstime = millis? I will try this stuff this evening I am arduinoless at the moment lol. Ive already gotten one program to control 1 input button perfectly how I want it I am just trying to explore other options of controlling many buttons other than copy and paste my previous code a bunch of times and have a high number of int's and a very long code.