pushbutton logic

Hi, all -

I'm trying to figure out how to create a sketch that keeps track of 12 switches. I'm having a little trouble wrapping my head around the logic, though.

I'm fine with the concept of assigning a different function to each button—that's no problem. I've also figured out how to keep that function going for as long as a given button is held down. What I can't figure out is how to trigger yet another function if more than one button is held down at any given time.

Any ideas? Pseudo code is fine - I'm really just looking for some help to understand how to do this conceptually.

Here's what I'm hoping to do in short:

Check button states.
if only ONE button is being held, play audio file associated with button.
if MORE THAN ONE button is held, stop audio altogether.

thanks!

I would use a for() loop to cycle through all the buttons and count how many are pushed.

int buttonArray[] = {2,3,4,5,6,7,8}; //use your own pin assignments here
void setup(){
  for(int i=0; i<sizeof(buttonArray)/sizeof(buttonArray[0]; i++) {
    pinMode(buttonArray[i], INPUT_PULLUP);
  }
}
void loop(){
  int buttonPressed;
  int countButtonPressed = 0;
  for(int i=0; i<sizeof(buttonArray)/sizeof(buttonArray[0]; i++) {
    if(digitalRead(buttonArray[i]) == LOW) {
      buttonPressed = i;
      countButtonPressed++;
  }
  if(countButtonPressed == 0){
    //do nothing
  } else if(countButtonPressed == 1){
    //do something
  } else {
    //emergency stop
  }
}

I agree with MorganS approach.

Multiple things at same time, here is my understanding

  1. Each button press is associated to a function, to execute some thing.
  2. Dont use delay in those functions, this will delay other function's execution.
  3. As per my knowledge, each function call execute sequentially, even though multiple button pressed simultaneously. That means Button 1 function executes, once it completes button 2 function execute and so on. So any delay in Button1 function will delay Button2 function execution.

You can also try this eg. code, not tested.

typedef void (*Action)(void);

struct ButtonConfig{
  int pin;
  Action action;
  void init(){
    pinMode(pin,INPUT);
  }

  bool isPressed(){
    if(digitalRead(pin)==HIGH)
      return true;
    else 
      return false;
  }
};

ButtonConfig buttons[2]; //declare how many buttons you need.

void setup() {
  initButtons();
}

void initButtons(){
  ButtonConfig button1 = {1, button1Press}; //assign the pin and the respective function when pressed
  ButtonConfig button2 = {2, button1Press}; //assign the pin and the respective function when pressed
  button1.init(); //set the pin mode
  button2.init(); //set the pin mode
  
  buttons[0]=button1;
  buttons[1]=button2;
}

void loop() {
  for(int i=0;i<2;i++){
    if(buttons[i].isPressed())
      buttons[i].action();
  }
}

void button1Press(void){
  //button 1 action
}

void button2Press(void){
  //button 2 action
}

@sarouje - that will not hold the code if 2 buttons are pressed, will execute the actions in sequence

@morganS that would work I think, just a typo - missing ‘)’ in

for(int i=0; i<sizeof(buttonArray)/sizeof(buttonArray[0][b][color=red])[/color][/b]; i++) {

@JML, I think I misunderstood the requirement, sorry for that. I was thinking OP wants to execute different function for each button. Yes my code will do execution in sequence.

No pb - what matters is willing to help!