Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 6 of 6

Thread: Bounce libray :use of constructor and setup()

  1. #1

    Bounce libray :use of constructor and setup()

    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

  2. #2
    Senior Member+ MichaelMeissner's Avatar
    Join Date
    Nov 2012
    Location
    Ayer Massachussetts
    Posts
    2,991
    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.

  3. #3
    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.

  4. #4
    Senior Member+ MichaelMeissner's Avatar
    Join Date
    Nov 2012
    Location
    Ayer Massachussetts
    Posts
    2,991
    Quote Originally Posted by AlainD View Post
    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.

  5. #5
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    20,166
    Quote Originally Posted by AlainD View Post
    prefers extra begin() and end() functions for hardware related stuff.
    This is actually from Arduino.

    https://www.arduino.cc/en/Reference/APIStyleGuide

  6. #6
    Quote Originally Posted by PaulStoffregen View Post
    Paul

    I'm I interpreting this correct as a "Style Guide" and less as a hardware "dictated" (to strong words) way of doing things.

    I don't mind calling "begin()", but do mind it if parameters are placed on two locations in the code as in the "Bounce example".


    Alain

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •