Case statement for multiple inputs/multiple outputs

I am trying to monitor the values of 5 input pins (left, right, up, down, enter) and print to my LCD which button is pressed. I only desire to have 1 button push recognized at a time, and if no buttons are pressed, clear the LCD. Seems simple enough, but I have run into a situation where I would need to write 3126 separate “If” statements to handle every possible scenario of buttons being pressed & the proper display for each combination. I looked at the reference page regarding the switch case command but seems I would still need heaps of “if” statements to generate the variable for the switch case to generate an output. There’s got to be an efficient way to do this. The code I’m currently working with, which doesn’t work BTW. with no buttons pressed, it reads nothing on line 0 and on line 1 it reads"LEFTRIGHTENTERNT." I put pulldown resistors to my button inputs to try to make it go away, but nothing.

/*
  LiquidCrystal Library - fail menu
 
This is a modified version of the "hello world" LCD example, trying to eventually make a scrollable directory-style menu with variable entry
 
  The circuit:
 * LCD RS pin to digital pin 12
 * LCD Enable pin to digital pin 11
 * LCD D4 pin to digital pin 5
 * LCD D5 pin to digital pin 4
 * LCD D6 pin to digital pin 3
 * LCD D7 pin to digital pin 2
 * LCD R/W pin to ground
 * 10K resistor:
 * ends to +5V and ground
 * wiper to LCD VO pin (pin 3)
 
 Library originally added 18 Apr 2008
 by David A. Mellis
 library modified 5 Jul 2009
 by Limor Fried (http://www.ladyada.net)
 example added 9 Jul 2009
 by Tom Igoe
 modified 22 Nov 2010
 by Tom Igoe
 Butchered 2/21/11
 by strantor
 This example code is in the public domain.

 http://www.arduino.cc/en/Tutorial/LiquidCrystal
 */

// include the library code:
#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
const int upPin = 13;     // the number of the up button pin
const int dwnPin = 8;     // the number of the down button pin
const int lPin = 9;     // the number of the left button pin
const int rPin = 10;     // the number of the rightbutton pin
const int entPin = 7;     // the number of the enter button pin
void setup() {
    pinMode(upPin, INPUT);  
    pinMode(dwnPin, INPUT);  
    pinMode(lPin, INPUT);  
    pinMode(rPin, INPUT);  
    pinMode(entPin, INPUT);  
  // set up the LCD's number of columns and rows: 
  lcd.begin(16, 2);

}

void loop() {
  // set the cursor to column 0, line 1
  // (note: line 1 is the second row, since counting begins with 0):
  lcd.setCursor(0, 1);
  //read all of the buttons & print which one is pressed to the LCD. If none pressed, clear the screen
  if (digitalRead(upPin) == HIGH){
    lcd.print("UP");
  }
  if (digitalRead(dwnPin) == HIGH){
    lcd.print("DOWN");
  }
  if (digitalRead(lPin) == HIGH){
    lcd.print("LEFT");
  }
  if (digitalRead(rPin) == HIGH){
    lcd.print("RIGHT");
  }
  if (digitalRead(entPin) == HIGH){
    lcd.print("ENTER");
  }
  if ((digitalRead(entPin) == LOW) && (digitalRead(rPin) == LOW) && (digitalRead(lPin) == LOW) && (digitalRead(upPin) == LOW) && (digitalRead(dwnPin) == LOW)) {
    lcd.clear();
  }
}

DOH! figured out why my inputs are always high. I had the buttons sideways in the breadboard, but still need help with the code

Thanks

Hi

Seems simple enough, but I have run into a situation where I would need to write 3126 separate "If" statements to handle every possible scenario of buttons being pressed & the proper display for each combination.

Not true. Each input has 2 states and you have 5 inputs. To cover all permultations you have 2^5 = 32 different combinations.

-Fletcher

Thats assuming the operator chooses to only press one button at a time. I need to include handling of if the operator press 2 or more buttons at the same time. my math was as follows: 5^5 for all possible combinations of inputs + 1 for no inputs

oh, crap. no, you're right. but still it seems like there's a better way than writing 32 if statements

32 case statements?

The 32 cases would cover all combinations of switch presses, from 0 to all 5 being pressed.

I only desire to have 1 button push recognized at a time

This can be handled with just 6 statements: if(digitalRead(switchPin1) == HIGH) // do something (left?) else if(digitalRead(switchPin2) == HIGH) // do something (right?) else if(digitalRead(switchPin3) == HIGH) // do something (up?) else if(digitalRead(switchPin4) == HIGH) // do something (down?) else if(digitalRead(switchPin5) == HIGH) // do something (enter?) else // Clear the screen

now we're cooking, thanks PaulS!

IMHO there is a better solution, use “for” operator.
You store status of all buttons in array[5],
and process whenever you like it using “for” again + only one “if” inside the loop.
check on a link:

http://arduino.cc/forum/index.php/topic,53011.0/topicseen.html

In order it works smooth and easy, try to put input pins in a sequence,
(7, 8, 9, 10, 11).

for (i = 7; i < 12; i++) {
             if(digitalRead(switchPin[i]) == HIGH)
                   do something;
                else
                   do different, or nothing;
 
}

May be you even don’t need to store it first, just processed on the “fly”