How to make an array of buttons using JC_Button?

Maybe its too late in the day and I’m not seeing it. I’m trying to add a second button to the JC_Button on/off example code

// Arduino Button Library
// https://github.com/JChristensen/JC_Button
// Copyright (C) 2018 by Jack Christensen and licensed under
// GNU GPL v3.0, https://www.gnu.org/licenses/gpl.html
//
// Example sketch to turn an LED on and off with a tactile button switch.
// Wire the switch from the Arduino pin to ground.

#include <JC_Button.h>          // https://github.com/JChristensen/JC_Button

// pin assignments
const byte
    BUTTON_PIN(7),              // connect a button switch from this pin to ground
    LED_PIN(13);                // the standard Arduino "pin 13" LED

Button myBtn(BUTTON_PIN);       // define the button

void setup()
{
    myBtn.begin();              // initialize the button object
    pinMode(LED_PIN, OUTPUT);   // set the LED pin as an output
}

void loop()
{
    static bool ledState;       // a variable that keeps the current LED status
    myBtn.read();               // read the button

    if (myBtn.wasReleased())    // if the button was released, change the LED state
    {
        ledState = !ledState;
        digitalWrite(LED_PIN, ledState);
    }
}

But I don’t want the buttons to be myBtn and myBtn2, but instead to be myBtn[0] and myBtn[1].

How do I rewrite the line

Button myBtn(BUTTON_PIN);       // define the button

to be an array?

An example of an array of objects, servos in this case, but the principle will be the same

#include <Servo.h>

Servo servos[3];
const byte servoPins[] = {10, 11, 12};

void setup()
{
  for (int s = 0 ; s < 3; s++)
  {
    servos[s].attach(servoPins[s]);
  }
  Serial.begin(115200);
  Serial.println();
}

void loop()
{
}

@UKHeliBob, that's what I thought. But it doesn't seem to work.

When I change

Button myBtn(BUTTON_PIN);       // define the button

to

Button myBtn[2];       // define the button

and all the references from myBtn to myBtn[0]
I get the following errors

Arduino: 1.8.5 (Windows 7), Board: "Arduino/Genuino Uno"

C:\Program Files (x86)\Arduino\arduino-builder -dump-prefs -logger=machine -hardware C:\Program Files (x86)\Arduino\hardware -hardware C:\Users\Anthony\AppData\Local\Arduino15\packages -tools C:\Program Files (x86)\Arduino\tools-builder -tools C:\Program Files (x86)\Arduino\hardware\tools\avr -tools C:\Users\Anthony\AppData\Local\Arduino15\packages -built-in-libraries C:\Program Files (x86)\Arduino\libraries -libraries C:\Users\Anthony\Documents\Arduino\libraries -fqbn=arduino:avr:uno -ide-version=10805 -build-path C:\Users\Anthony\AppData\Local\Temp\arduino_build_273800 -warnings=all -build-cache C:\Users\Anthony\AppData\Local\Temp\arduino_cache_894386 -prefs=build.warn_data_percentage=75 -prefs=runtime.tools.arduinoOTA.path=C:\Users\Anthony\AppData\Local\Arduino15\packages\arduino\tools\arduinoOTA\1.1.1 -prefs=runtime.tools.avrdude.path=C:\Users\Anthony\AppData\Local\Arduino15\packages\arduino\tools\avrdude\6.3.0-arduino9 -prefs=runtime.tools.avr-gcc.path=C:\Users\Anthony\AppData\Local\Arduino15\packages\arduino\tools\avr-gcc\4.9.2-atmel3.5.4-arduino2 -verbose C:\Users\Anthony\AppData\Local\Temp\arduino_modified_sketch_548073\SimpleOnOff.ino
C:\Program Files (x86)\Arduino\arduino-builder -compile -logger=machine -hardware C:\Program Files (x86)\Arduino\hardware -hardware C:\Users\Anthony\AppData\Local\Arduino15\packages -tools C:\Program Files (x86)\Arduino\tools-builder -tools C:\Program Files (x86)\Arduino\hardware\tools\avr -tools C:\Users\Anthony\AppData\Local\Arduino15\packages -built-in-libraries C:\Program Files (x86)\Arduino\libraries -libraries C:\Users\Anthony\Documents\Arduino\libraries -fqbn=arduino:avr:uno -ide-version=10805 -build-path C:\Users\Anthony\AppData\Local\Temp\arduino_build_273800 -warnings=all -build-cache C:\Users\Anthony\AppData\Local\Temp\arduino_cache_894386 -prefs=build.warn_data_percentage=75 -prefs=runtime.tools.arduinoOTA.path=C:\Users\Anthony\AppData\Local\Arduino15\packages\arduino\tools\arduinoOTA\1.1.1 -prefs=runtime.tools.avrdude.path=C:\Users\Anthony\AppData\Local\Arduino15\packages\arduino\tools\avrdude\6.3.0-arduino9 -prefs=runtime.tools.avr-gcc.path=C:\Users\Anthony\AppData\Local\Arduino15\packages\arduino\tools\avr-gcc\4.9.2-atmel3.5.4-arduino2 -verbose C:\Users\Anthony\AppData\Local\Temp\arduino_modified_sketch_548073\SimpleOnOff.ino
Using board 'uno' from platform in folder: C:\Users\Anthony\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.6.21
Using core 'arduino' from platform in folder: C:\Users\Anthony\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.6.21
Detecting libraries used...
"C:\Users\Anthony\AppData\Local\Arduino15\packages\arduino\tools\avr-gcc\4.9.2-atmel3.5.4-arduino2/bin/avr-g++" -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics  -flto -w -x c++ -E -CC -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=10805 -DARDUINO_AVR_UNO -DARDUINO_ARCH_AVR   "-IC:\Users\Anthony\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.6.21\cores\arduino" "-IC:\Users\Anthony\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.6.21\variants\standard" "C:\Users\Anthony\AppData\Local\Temp\arduino_build_273800\sketch\SimpleOnOff.ino.cpp" -o "nul"
"C:\Users\Anthony\AppData\Local\Arduino15\packages\arduino\tools\avr-gcc\4.9.2-atmel3.5.4-arduino2/bin/avr-g++" -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics  -flto -w -x c++ -E -CC -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=10805 -DARDUINO_AVR_UNO -DARDUINO_ARCH_AVR   "-IC:\Users\Anthony\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.6.21\cores\arduino" "-IC:\Users\Anthony\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.6.21\variants\standard" "-IC:\Users\Anthony\Documents\Arduino\libraries\JC_Button\src" "C:\Users\Anthony\AppData\Local\Temp\arduino_build_273800\sketch\SimpleOnOff.ino.cpp" -o "nul"
"C:\Users\Anthony\AppData\Local\Arduino15\packages\arduino\tools\avr-gcc\4.9.2-atmel3.5.4-arduino2/bin/avr-g++" -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics  -flto -w -x c++ -E -CC -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=10805 -DARDUINO_AVR_UNO -DARDUINO_ARCH_AVR   "-IC:\Users\Anthony\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.6.21\cores\arduino" "-IC:\Users\Anthony\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.6.21\variants\standard" "-IC:\Users\Anthony\Documents\Arduino\libraries\JC_Button\src" "C:\Users\Anthony\Documents\Arduino\libraries\JC_Button\src\JC_Button.cpp" -o "nul"
Generating function prototypes...
"C:\Users\Anthony\AppData\Local\Arduino15\packages\arduino\tools\avr-gcc\4.9.2-atmel3.5.4-arduino2/bin/avr-g++" -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics  -flto -w -x c++ -E -CC -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=10805 -DARDUINO_AVR_UNO -DARDUINO_ARCH_AVR   "-IC:\Users\Anthony\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.6.21\cores\arduino" "-IC:\Users\Anthony\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.6.21\variants\standard" "-IC:\Users\Anthony\Documents\Arduino\libraries\JC_Button\src" "C:\Users\Anthony\AppData\Local\Temp\arduino_build_273800\sketch\SimpleOnOff.ino.cpp" -o "C:\Users\Anthony\AppData\Local\Temp\arduino_build_273800\preproc\ctags_target_for_gcc_minus_e.cpp"
"C:\Program Files (x86)\Arduino\tools-builder\ctags\5.8-arduino11/ctags" -u --language-force=c++ -f - --c++-kinds=svpf --fields=KSTtzns --line-directives "C:\Users\Anthony\AppData\Local\Temp\arduino_build_273800\preproc\ctags_target_for_gcc_minus_e.cpp"
Compiling sketch...
"C:\Users\Anthony\AppData\Local\Arduino15\packages\arduino\tools\avr-gcc\4.9.2-atmel3.5.4-arduino2/bin/avr-g++" -c -g -Os -Wall -Wextra -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -MMD -flto -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=10805 -DARDUINO_AVR_UNO -DARDUINO_ARCH_AVR   "-IC:\Users\Anthony\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.6.21\cores\arduino" "-IC:\Users\Anthony\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.6.21\variants\standard" "-IC:\Users\Anthony\Documents\Arduino\libraries\JC_Button\src" "C:\Users\Anthony\AppData\Local\Temp\arduino_build_273800\sketch\SimpleOnOff.ino.cpp" -o "C:\Users\Anthony\AppData\Local\Temp\arduino_build_273800\sketch\SimpleOnOff.ino.cpp.o"
SimpleOnOff:16: error: no matching function for call to 'Button::Button()'

 Button myBtn[2];       // define the button

               ^

C:\Users\Anthony\AppData\Local\Temp\arduino_modified_sketch_548073\SimpleOnOff.ino:16:15: note: candidates are:

In file included from C:\Users\Anthony\AppData\Local\Temp\arduino_modified_sketch_548073\SimpleOnOff.ino:9:0:

C:\Users\Anthony\Documents\Arduino\libraries\JC_Button\src/JC_Button.h:23:9: note: Button::Button(uint8_t, uint32_t, uint8_t, uint8_t)

         Button(uint8_t pin, uint32_t dbTime=25, uint8_t puEnable=true, uint8_t invert=true)

         ^

C:\Users\Anthony\Documents\Arduino\libraries\JC_Button\src/JC_Button.h:23:9: note:   candidate expects 4 arguments, 0 provided

C:\Users\Anthony\Documents\Arduino\libraries\JC_Button\src/JC_Button.h:11:7: note: constexpr Button::Button(const Button&)

 class Button

       ^

C:\Users\Anthony\Documents\Arduino\libraries\JC_Button\src/JC_Button.h:11:7: note:   candidate expects 1 argument, 0 provided

C:\Users\Anthony\Documents\Arduino\libraries\JC_Button\src/JC_Button.h:11:7: note: constexpr Button::Button(Button&&)

C:\Users\Anthony\Documents\Arduino\libraries\JC_Button\src/JC_Button.h:11:7: note:   candidate expects 1 argument, 0 provided

Using library JC_Button at version 2.0.1 in folder: C:\Users\Anthony\Documents\Arduino\libraries\JC_Button 
exit status 1
no matching function for call to 'Button::Button()'

Please post you code

The before is in the OP. Here is the after.

// Arduino Button Library
// https://github.com/JChristensen/JC_Button
// Copyright (C) 2018 by Jack Christensen and licensed under
// GNU GPL v3.0, https://www.gnu.org/licenses/gpl.html
//
// Example sketch to turn an LED on and off with a tactile button switch.
// Wire the switch from the Arduino pin to ground.

#include <JC_Button.h>          // https://github.com/JChristensen/JC_Button

// pin assignments
const byte
    BUTTON_PIN(7),              // connect a button switch from this pin to ground
    LED_PIN(13);                // the standard Arduino "pin 13" LED

Button myBtn[2];       // define the button

void setup()
{
    myBtn[0].begin();              // initialize the button object
    pinMode(LED_PIN, OUTPUT);   // set the LED pin as an output
}

void loop()
{
    static bool ledState;       // a variable that keeps the current LED status
    myBtn[0].read();               // read the button

    if (myBtn[0].wasReleased())    // if the button was released, change the LED state
    {
        ledState = !ledState;
        digitalWrite(LED_PIN, ledState);
    }
}

Looking at the library source I can't see how to create an array of button objects but that does not mean that it is not possible

OK. I tried exactly what you suggested, before I posted. But tried it again in case I did it wrong. Glad we're on the same page. Thanks.

It would be nice if it is possible.

You cannot do that statically, you need dynamic allocation:

#define NUM_BUTTONS 2
const byte BUTTON_PINS[NUM_BUTTONS] = { 2, 3 };
Button *myBtn[2];

void setup()
{
  for (byte i = 0; i < NUM_BUTTONS; i++) myBtn[i] = new Button(BUTTON_PINS[i]);
}

void loop()
{
  for (byte i = 0; i < NUM_BUTTONS; i++)
  {
    myBtn[i]->read();
    if (myBtn[i]->wasPressed())
    {
      Serial.print("Button ");
      Serial.print(i);
      Serial.print(" was pressed");
    }
  }
}

If you want it static, use another library.

It should be pretty straight-forward. The code below compiles, but is untested.

#include <JC_Button.h>

const uint8_t button1Pin = 7, button2Pin = 8;
Button myBtns[] = {button1Pin, button2Pin};
const uint8_t numButtons = sizeof(myBtns) / sizeof(myBtns[0]);

void setup()
{
  Serial.begin(115200);
  delay(1000);
  for (uint8_t i = 0; i < numButtons; i++) {
    myBtns[i].begin();
  }
}

void loop()
{
  for (uint8_t i = 0; i < numButtons; i++) {
    myBtns[i].read();
  }

  for (uint8_t i = 0; i < numButtons; i++) {
    if (myBtns[i].wasReleased()) {
      Serial.print("Button # ");
      Serial.print(i);
      Serial.println(" was just released");
    }
  }
}

@Danios90, you code needed some additions, but does work in this form

#include <JC_Button.h>

#define NUM_BUTTONS 2
const byte BUTTON_PINS[NUM_BUTTONS] = { 4, 5 };
Button *myBtn[2];

void setup()
{
  Serial.begin(115200);
  for (byte i = 0; i < NUM_BUTTONS; i++)
   {
    myBtn[i] = new Button(BUTTON_PINS[i]);
    myBtn[i]->begin();
   } 
}

void loop()
{
  for (byte i = 0; i < NUM_BUTTONS; i++)
  {
    myBtn[i]->read();
    if (myBtn[i]->wasPressed())
    {
      Serial.print("Button ");
      Serial.print(i);
      Serial.println(" was pressed");
    }
  }
}

@gfvalvo, congrats, yours worked out the box (other than I had to change the pin numbers).

Question though.

How is

Button myBtns[] = {button1Pin, button2Pin};

equivalent to

Button myBtn(BUTTON_PIN);

It's the required syntax for statically instantiating an array of objects whose constructor takes a value. Here's a more sophisticated example:
https://www.includehelp.com/cpp-tutorial/array-of-objects-initialization-with-constructors-in-cpp.aspx

Ahh. A C++ thing. I'm far better at standard C than C++.