# Is there a parsimonious way to map button inputs?

I'm programming a 6 button remote and I need to ensure I can call subroutines for each button and remove conflicts. For example Steer Left but not Steer Right. Is there a more elegant way than using an IF statement for each input triggering each subroutine, and eliminating conflicts? I can see this getting very large and very messy with eliminating multiple conflicts. Example below.

``````if (Button1 == 1 && Button2 == 0)
{
SteerRight
}

if (Button2 ==1 && Button1 ==0)
{
SteerLeft
}
``````

Any help?

Is there a real risk that two conflicting buttons might be pressed at the same time?

A possible mechanism is to assign powers-of-2 values to each button - for example button1 produces a 1 when pressed, button2 produces a 2, button3 produces a 4 etc.

Then add all the values together. The resulting number will be unique for every combination of buttons. For the example you posted you could do (pseudo code)

``````switch buttonTotal
case 1
steerRight()
break
case 2
steerLeft()
break
``````

...R

Robin2:
Is there a real risk that two conflicting buttons might be pressed at the same time?

Yes. I'm being forced to use a tiny remote with an awkward layout and I have big thumbs. I don't want to accidentally hit left/right at the same time or shift gear up and shift gear down at the same time.

Robin2:
A possible mechanism is to assign powers-of-2 values to each button - for example button1 produces a 1 when pressed, button2 produces a 2, button3 produces a 4 etc.

Then add all the values together. The resulting number will be unique for every combination of buttons.

I thought about doing that but since I have 6 buttons with 7 possible inputs (holding #6) that's a lot of possibilities.

Sketco:
I thought about doing that but since I have 6 buttons with 7 possible inputs (holding #6) that's a lot of possibilities.

Not really, if I'm understanding @Robin2 correctly. Each switch generates its own unique *binary weighted * number. Assume: switch #1 generates a 1 and switch #2 generates 2 and switch #3 generates a 4. If #1 and #2 are pressed simultaneously their sum would produce the number 3, which you can define as an invalid state. Same for switches #2 and #3. Together they sum as six, again defined as invalid.

You only will have 7 cases in your switch; you do not have to fill in the others.

``````switch(buttons)
{
case 0x01:  // btn1
...
...
break;
case 0x02:  // btn2
...
...
break;
case 0x04:
...
...
break;
case 0x08:
...
...
break;
case 0x10:
...
...
break;
case 0x20:  // btn6
...
...
break;
case 0x40:  // btn6, long press
...
...
break;
default
// all unhandled cases
break;
}
``````

You can even leave the default out.

dougp:
Not really, if I'm understanding @Robin2 correctly.

You are.

...R