Bounce libray :use of constructor and setup()

Status
Not open for further replies.

AlainD

Well-known member
Hi

The example for the Bounce library needs constructor parameters and an extra call in setup:

---
#include <Bounce.h>

const int buttonPin = 12;
Bounce pushbutton = Bounce(buttonPin, 10); // 10 ms debounce

void setup() {
pinMode(buttonPin, INPUT_PULLUP);
}
---

Is there a reason that the pinMode can't be called from the constructor?
I would like to use the Bouncer object inside another class and pinMode call inside setup is awkward and a possible cause of problems.

Alain
 
I imagine given it is an Arduino library, that you have to ask the original author, possibly on the Arduino forum. Note, the original Bounce library hasn't been modified for years. The author has rewritten the library (Bounce2), and has updated the github repository 7 days ago (to add support for the ARDUINO_STM_NUCLEO_F103RB and ARDUINO_GENERIC_STM32F103C systems). If you look in Bounce2, you will see that the attach function does call pinMode in the attach class function.

Note, Bounce2 rearranges things, so you will need to rewrite your code to use the new methods and names. Here is the 'support' thread on the arduino forum about Bounce/Bounce2:

That is one of the problems of open software, is you can be left with old code that the author has abandoned, and perhaps nobody feels strongly to delete or update it. It also depends on the skill level of the people writing the libraries. Not everybody is up on classes, etc.
 
Thanks.

Today I also found a mention of Paul that he doesn't like "hardware related" instructions inside constructors and prefers extra begin() and end() functions for hardware related stuff.
 
Thanks.

Today I also found a mention of Paul that he doesn't like "hardware related" instructions inside constructors and prefers extra begin() and end() functions for hardware related stuff.

Yes, in complex setups, it can be tricky to order constructors, and also you might want to have a switch that disbles the device all together.
 
Status
Not open for further replies.
Back
Top