Need Advice on C programming ... Button Function

Hi all. I am using the Button2 library found here: GitHub - LennartHennigs/Button2: Arduino/ESP button library that provides callback functions to track single, double, triple and long clicks. It also takes care of debouncing.. I am on Windows10 using PlatformIO in arduino framework. I know everyone has their favorite buttons library, but this one seems to work. The issue I am having is my poor understanding of C and I know many of you are awesome at C ( I'm new to it so please be kind)

So the issue is I have 4 buttons on my project and there are certain instances when I want to change their behavior individually and other instances when I want to change them all at once to the same thing. so what I tried to do was use the constructor to create 4 buttons:

Button2 buttonUp, buttonEnter, buttonDown, buttonBack;

then create an array of buttons containing these 4 buttons

Button2 allButtons[] = {buttonUp, buttonEnter, buttonDown, buttonBack};

I thought for instances when I wanted to address these buttons as a group, I could use the allButtons array and address them that way e.g.

for (int i=0; i < 4; i++){
  allButtons[i].setDebounceTime(10);
  }

but this doesn't work. I think when I populate the array allButtons[] I am creating a new instance of buttons but accessing allButtons[0] is not the same as accessing buttonUp. Is my understanding of why this does not work correct? I could have only defined the array and done it that way I guess, but I wanted to give each individual button a name to use when I assign the individual behaviors rather than referring to them by their array names e.g. allButtons[1]. So how does one give each object in an array of objects a unique name without creating new objects?

Thanks!
Fish

Hello markbeeman

Post your sketch, well formated, with well-tempered comments and in so called code tags "</>" to see how we can help.

1 Like

you could use an array of pointers to the existing button instances. Don't create new instances

#include <Button2.h>

Button2 buttonUp, buttonEnter, buttonDown, buttonBack;
Button2* allButtons[] = {&buttonUp, &buttonEnter, &buttonDown, &buttonBack};
const byte buttonCount = sizeof allButtons / sizeof * allButtons;
const byte pin[buttonCount] = {2, 3, 4, 5};

void setup() {
  for (byte i = 0; i < buttonCount; i++) {
    allButtons[i]->begin(pin[i]);
    allButtons[i]->setDebounceTime(10);
  }
}

void loop() {
}

as it's an array of pointers you use -> and not the dot

You are soooo smart! This is exactly what I was looking for. So to make sure I understand

Button2* allButtons[] = {&buttonUp, &buttonEnter, &buttonDown, &buttonBack};

is an array of pointers to button objects. Then

allButtons[i]->setDebounceTime(10);

the -> operator allows you to access the member function setDebounceTime(10) of the ith object? (actually its the ith pointer ).

Yeah, no way was I coming up with this on my own. Thanks, Mr. Jackson!

Fish

You got it right :wink:

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.