Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add array of struct ezButtonExample #22

Open
drf5n opened this issue Aug 8, 2024 · 0 comments
Open

Add array of struct ezButtonExample #22

drf5n opened this issue Aug 8, 2024 · 0 comments

Comments

@drf5n
Copy link

drf5n commented Aug 8, 2024

Expanding on the examples in https://forum.arduino.cc/t/for-beginners-the-simple-way-to-program-for-multiple-buttons-code-example/629651 and https://github.com/ArduinoGetStarted/button/blob/master/examples/07.ButtonArray/07.ButtonArray.ino , I wrote an example using an array of structs with ezButtons as elements. It might be nice to add as an example.

/* an ezButton demo using an array of structures
  Build from https://forum.arduino.cc/t/for-beginners-the-simple-way-to-program-for-multiple-buttons-code-example/629651
  and its array of buttons example from
  https://github.com/ArduinoGetStarted/button/blob/master/examples/07.ButtonArray/07.ButtonArray.ino

  Wokwi demo: https://wokwi.com/projects/405606656376410113
*/

#include <ezButton.h> // https://github.com/ArduinoGetStarted/button

struct MyButtonsStruct {
  ezButton ezb;
  byte pin;
  char * name;
  char * desc;
  char * pressMessage;
  char * releaseMessage;
};

MyButtonsStruct myButtons[] = {
  { ezButton(2), 2, "Green/2", "Start the device", "starting...", "started."},
  { ezButton(3), 3, "Red/3", "Stop the device", "stopping...", "stopped"},
  { ezButton(4), 4, "Blue/4", "Increment", "filling...", "filled"},
  { ezButton(5), 5, "Yellow/5", "Decrement", "draining...", "drained"},
  { ezButton(6), 6, "Gray/6", "Accept", "Submitting...", "accepted"}
};

const int BUTTON_NUM = sizeof(myButtons) / sizeof(myButtons[0]);

void setup() {
  Serial.begin(115200);

  for (byte i = 0; i < BUTTON_NUM; i++) {
    Serial.print("Enabling ");
    Serial.print(myButtons[i].name);
    Serial.print(" as ");
    Serial.println(myButtons[i].desc);
    myButtons[i].ezb.setDebounceTime(50); // set debounce time to 50 milliseconds
  }
}

void loop() {
  for (byte i = 0; i < BUTTON_NUM; i++) { // read all the input buttons
    myButtons[i].ezb.loop(); // MUST call the loop() function first
  }

  for (byte i = 0; i < BUTTON_NUM; i++) { // process all the buttons
    if (myButtons[i].ezb.isPressed()) {
      Serial.print("The ");
      Serial.print(myButtons[i].name);
      Serial.print(" button ");
      Serial.print(myButtons[i].pin);
      Serial.print(" is pressed. ");
      Serial.println(myButtons[i].pressMessage);
    }
    if (myButtons[i].ezb.isReleased()) {
      Serial.print("The ");
      Serial.print(myButtons[i].name);
      Serial.print(" button ");
      Serial.print(myButtons[i].pin);
      Serial.print(" is released. ");
      Serial.println(myButtons[i].releaseMessage);
    }
  }
}

There is a live demo here:

https://wokwi.com/projects/405606656376410113

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant