pushbutton logic


hi, -

i'm trying figure out how create sketch keeps track of 12 switches. i'm having little trouble wrapping head around logic, though.

i'm fine concept of assigning different function each button--that's no problem. i've figured out how keep function going long given button held down. can't figure out how trigger yet function if more 1 button held down @ given time.

any ideas? pseudo code fine - i'm looking understand how conceptually.

here's i'm hoping in short:

check button states.
if 1 button being held, play audio file associated button.
if more 1 button held, stop audio altogether.

thanks!

i use for() loop cycle through buttons , count how many pushed.
code: [select]
int buttonarray[] = {2,3,4,5,6,7,8}; //use 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
  }
}


Arduino Forum > Using Arduino > Programming Questions > pushbutton logic


arduino

Comments

Popular posts from this blog

Help with Missing Filename Error?

more info - Joomla! Forum - community, help and support

Using phone battery to power a plane.