Multiple button setup help

I am trying to create a multiple button setup but I cant get more than two pins to work. Why is c not being written.

const int pinA = 2; //A button
const int pinB = 3; //B button
const int pinC = 4;

void sendA() {
delayMicroseconds(10000); //to avoid chattering effect
int buttonA = digitalRead(pinA);
if (buttonA == 1) {
Serial.println("a"); //Send "a" to ProtoPie
}
}

void sendB() {
delayMicroseconds(10000); //to avoid chattering effect
int buttonB = digitalRead(pinB);
if (buttonB == 1) {
Serial.println("b"); //Send "b" to ProtoPie
}
}
void sendC() {
delayMicroseconds(10000); //to avoid chattering effect
int buttonC = digitalRead(pinC);
if (buttonC == 1) {
Serial.println("c"); //Send "b" to ProtoPie
}
}

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

pinMode(pinA, INPUT);
pinMode(pinB, INPUT);
pinMode(pinC, INPUT);

attachInterrupt(digitalPinToInterrupt(pinA), sendA, RISING); //Send a signal once
attachInterrupt(digitalPinToInterrupt(pinB), sendB, RISING); //Send a signal once

}

void loop() {

}

Don't use interrupts for buttons, it's not worth the hassle, and humans are very slow.

I would use buttons that close to GND and INPUT_PULLUP on the pins.

To make handling easy, I would use the Bounce2 library.


/* 
 DESCRIPTION
 ====================
 This is an example of the Bounce2::Button class. 
 When the user presses a physical button, it toggles a LED on or off.
 The Button class matches an electrical state to a physical action. 
 Use .setPressedState(LOW or HIGH) to set the detection state for when the button is pressed.


 INSCRUCTIONS
 ====================

 Set BUTTON_PIN to the pin attached to the button.
 Set LED_PIN to the pin attached to a LED.
 
 */

 // WE WILL attach() THE BUTTON TO THE FOLLOWING PIN IN setup()
#define BUTTON_PIN 2 

// DEFINE THE PIN FOR THE LED :
// 1) SOME BOARDS HAVE A DEFAULT LED (LED_BUILTIN)
//#define LED_PIN LED_BUILTIN
// 2) OTHERWISE SET YOUR OWN PIN
#define LED_PIN 13
 
// Include the Bounce2 library found here :
// https://github.com/thomasfredericks/Bounce2
#include <Bounce2.h>

// INSTANTIATE A Button OBJECT FROM THE Bounce2 NAMESPACE
Bounce2::Button button = Bounce2::Button();



// SET A VARIABLE TO STORE THE LED STATE
int ledState = LOW;

void setup() {

  // BUTTON SETUP 
  
  // SELECT ONE OF THE FOLLOWING :
  // 1) IF YOUR BUTTON HAS AN INTERNAL PULL-UP
  // button.attach( BUTTON_PIN ,  INPUT_PULLUP ); // USE INTERNAL PULL-UP
  // 2) IF YOUR BUTTON USES AN EXTERNAL PULL-UP
  button.attach( BUTTON_PIN, INPUT ); // USE EXTERNAL PULL-UP

  // DEBOUNCE INTERVAL IN MILLISECONDS
  button.interval(5); 

  // INDICATE THAT THE LOW STATE CORRESPONDS TO PHYSICALLY PRESSING THE BUTTON
  button.setPressedState(LOW); 
  
  // LED SETUP
  pinMode(LED_PIN,OUTPUT);
  digitalWrite(LED_PIN,ledState);

}

void loop() {
  // UPDATE THE BUTTON
  // YOU MUST CALL THIS EVERY LOOP
  button.update();

  // <Button>.pressed() RETURNS true IF THE STATE CHANGED
  // AND THE CURRENT STATE MATCHES <Button>.setPressedState(<HIGH or LOW>);
  if ( button.pressed() ) {
    
    // TOGGLE THE LED STATE : 
    ledState = !ledState; // SET ledState TO THE OPPOSITE OF ledState
    digitalWrite(LED_PIN,ledState); // WRITE THE NEW ledState

  }
}

Thank you I'm new to this and I appreciate the reply. I'm going to give it a try.

If you intend to use multiple buttons, an array of those objects works pretty well,
so you don't have to duplicate code for updating or checking the buttons,
but you can update or check them in a for loop.

What would the code look like between three buttons? I need twelve. I tried going the analog route and the individual pin route and have not been able to crack it.

Only compiled, not tested.

#include <Bounce2.h>

const uint8_t pins[] = { 2, 3, 4 };
Bounce2::Button buttons[sizeof(pins)];

void setup() {
  Serial.begin(115200);
  for (uint8_t i = 0; i < sizeof(pins); i++) {
    buttons[i].attach(pins[i], INPUT_PULLUP);
    buttons[i].setPressedState(LOW);
  }
}

void loop() {
  for (uint8_t i = 0; i < sizeof(pins); i++) {
    if (buttons[i].update()) {
      if (buttons[i].pressed()) {
        Serial.print(F("button "));
        Serial.println(i + 1);
      }
    }
  }
}

You can refer to this thread: For beginners: The simple way to program for multiple buttons [code example]

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