bcd switch and switch case coding

I am taking a 4 bit binary code (parallel not serial) from an existing circuit and connecting to 4 input pins on the arduino.
I want to use “switch case” to operate a function according to the binary code being read.
i.e. if 0101 is the code then I select switch case 4.

0001 - case 0, then function 1,
0010 - case 1, then function 2,
0011 - case 2, then function 3,
0100 - case 3, then function 4 etc etc.

I don’t know how to get from reading the pins to selecting a switch case, can anyone please help?
I have attached the code I have got so far.

#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
const byte bcdInputA0 = 7; // 10k pull down resistor
const byte bcdInputA1 = 8; // 10k pull down resistor
const byte bcdInputA2 = 9; // 10k pull down resistor
const byte bcdInputA3 = 10; // 10k pull down resistor
int solenoid = 6; // output to mosfet
static int Sequence = 0;
int A0state = 0;
int A1state = 0;
int A2state = 0;
int A3state = 0;

void setup() 
{
  // put your setup code here, to run once:
     pinMode(bcdInputA0, INPUT);
     pinMode(bcdInputA1, INPUT);
     pinMode(bcdInputA2, INPUT);
     pinMode(bcdInputA3, INPUT);
     pinMode(solenoid, OUTPUT);
     lcd.begin(16, 2);
     lcd.print("Hello");
     lcd.setCursor(0, 1);
     lcd.print("select sequence");
} // end of void setup

void loop()

{
  // put your main code here, to run repeatedly:

     A0state = digitalRead(bcdInputA0); // LSB
     A1state = digitalRead(bcdInputA1);
     A2state = digitalRead(bcdInputA2);
     A3state = digitalRead(bcdInputA3); // MSB
  // lots more code here!!!
  
  // e.g. if A0 =1, A1=0, A2=1, A3=0, that is 0101 (5) in binary
  // that will select switch case 5 and so on...
  // only codes available from external circuit are
  // 0000, 0001, 0010, 0011, 0100, 0101, 0110, 0111, 1000, 1001
  


   switch (Sequence)
   {
      case 0: 
      // code to display sequence 1 on lcd
      lcd.print("sequence 1");
      digitalWrite(solenoid, LOW);
      break;
   
      case 1: 
      // code to display sequence 2 on lcd
      lcd.print("sequence 2");
      digitalWrite(solenoid, LOW);
      break;
      
      case 2: 
      // code to display sequence 3 on lcd
      lcd.print("sequence 3");
      digitalWrite(solenoid, HIGH);
      break;
      
      case 3: 
      // code to display sequence 4 on lcd
      lcd.print("sequence 4");
      digitalWrite(solenoid, LOW);
      break;
      
      case 4: 
      // code to display sequence 5 on lcd
      lcd.print("sequence 5");
      digitalWrite(solenoid, HIGH);
      break;
 // etc etc
 
   } // end of case

} //  end of void loop

check out the bitWrite function bitWrite. You can read each switch into its corresponding bit in a variable to use in the switch.

All you have to do is assemble A0state...A3state into a single variable.
If you're not happy with C bit manipulations, try bitWrite.

     unsigned char value = 0; // value from 0 to 15 based on BCD input
     value += digitalRead(bcdInputA0); // LSB
     value += 2 * digitalRead(bcdInputA1);
     value += 4 * digitalRead(bcdInputA2);
     value += 8 * digitalRead(bcdInputA3); // MSB

Thank you very much for replies.
You will have to excuse my lack of knowledge in programming, I have only been experimenting with Arduino for just over a week!!!

I don’t quite understand how to apply bitWrite but I did use the other code, here is my latest.

#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
const byte bcdInputA0 = 7; // 10k pull down resistor
const byte bcdInputA1 = 8; // 10k pull down resistor
const byte bcdInputA2 = 9; // 10k pull down resistor
const byte bcdInputA3 = 10; // 10k pull down resistor
int solenoid = 6; // output to mosfet
int A0state = 0;
int A1state = 0;
int A2state = 0;
int A3state = 0;

void setup() 
{
  // put your setup code here, to run once:
     pinMode(bcdInputA0, INPUT);
     pinMode(bcdInputA1, INPUT);
     pinMode(bcdInputA2, INPUT);
     pinMode(bcdInputA3, INPUT);
     pinMode(solenoid, OUTPUT);
     lcd.begin(16, 2);
     lcd.print("Hello");
     
} // end of void setup

void loop()

{
  // put your main code here, to run repeatedly:

     A0state = digitalRead(bcdInputA0); // LSB
     A1state = digitalRead(bcdInputA1);
     A2state = digitalRead(bcdInputA2);
     A3state = digitalRead(bcdInputA3); // MSB
  // lots more code here!!!
  
  // e.g. if A0 =1, A1=0, A2=1, A3=0, that is 0101 (5) in binary
  // that will select switch case 5 and so on...
  // only codes available from external circuit are
  // 0000, 0001, 0010, 0011, 0100, 0101, 0110, 0111, 1000, 1001
  
     unsigned char value = 0; // value from 0 to 15 based on BCD input
     value += digitalRead(bcdInputA0); // LSB
     value += 2 * digitalRead(bcdInputA1);
     value += 4 * digitalRead(bcdInputA2);
     value += 8 * digitalRead(bcdInputA3); // MSB
     
   switch (value)
   {
      case 0: 
      // code to display blank on lcd
      lcd.print("");
      digitalWrite(solenoid, LOW);
      break;
   
      case 1: 
      // code to display sequence 1 on lcd
      lcd.setCursor(0,1);
      lcd.print("sequence 1");
      digitalWrite(solenoid, LOW);
      break;
      
      case 2: 
      // code to display sequence 2 on lcd
      lcd.setCursor(0,1);
      lcd.print("sequence 2");
      digitalWrite(solenoid, HIGH);
      break;
      
      case 3: 
      // code to display sequence 3 on lcd
      lcd.setCursor(0,1);
      lcd.print("sequence 3");
      digitalWrite(solenoid, LOW);
      break;
      
      case 4: 
      // code to display sequence 4 on lcd
      lcd.setCursor(0,1);
      lcd.print("sequence 4");
      digitalWrite(solenoid, HIGH);
      break;
      
      case 5: 
      // code to display sequence 5 on lcd
      lcd.setCursor(0,1);
      lcd.print("sequence 5");
      digitalWrite(solenoid, HIGH);
      break;
      
      case 6: 
      // code to display sequence 6 on lcd
      lcd.setCursor(0,1);
      lcd.print("sequence 6");
      digitalWrite(solenoid, HIGH);
      break;
      
      // etc etc
 
   } // end of case

} //  end of void loop

I like John's method better, too.

I have since found another problem. I have added all the other switch cases and now go upto 10. If i select case 10 and display shows 10, and then select say 8 display shows 80! I think the 0 was left behind. I have now added lcd.clear into each case but I get a lot of flickering. So I added a delay(350) just before the switch case which sorts out the problem. The longer the delay the less flickering but the longer time it takes for the switch to change. A bit of a compromise.
Anyone got any better ideas?

#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
const byte bcdInputA0 = 7; // 10k pull down resistor
const byte bcdInputA1 = 8; // 10k pull down resistor
const byte bcdInputA2 = 9; // 10k pull down resistor
const byte bcdInputA3 = 10; // 10k pull down resistor
int solenoid = 6; // output to mosfet
int A0state = 0;
int A1state = 0;
int A2state = 0;
int A3state = 0;

void setup() 
{
  // put your setup code here, to run once:
     pinMode(bcdInputA0, INPUT);
     pinMode(bcdInputA1, INPUT);
     pinMode(bcdInputA2, INPUT);
     pinMode(bcdInputA3, INPUT);
     pinMode(solenoid, OUTPUT);
     lcd.begin(16, 2);
     lcd.print("Hello");
     
} // end of void setup

void loop()

{
  // put your main code here, to run repeatedly:

     A0state = digitalRead(bcdInputA0); // LSB
     A1state = digitalRead(bcdInputA1);
     A2state = digitalRead(bcdInputA2);
     A3state = digitalRead(bcdInputA3); // MSB
  // lots more code here!!!
  
  // e.g. if A0 =1, A1=0, A2=1, A3=0, that is 0101 (5) in binary
  // that will select switch case 5 and so on...
  // only codes available from external circuit are
  // 0000, 0001, 0010, 0011, 0100, 0101, 0110, 0111, 1000, 1001
  
     unsigned char value = 0; // value from 0 to 15 based on BCD input
     value += digitalRead(bcdInputA0); // LSB
     value += 2 * digitalRead(bcdInputA1);
     value += 4 * digitalRead(bcdInputA2);
     value += 8 * digitalRead(bcdInputA3); // MSB
     delay(350);
   switch (value)
   {
      case 0: 
      // code to display blank on lcd
      lcd.print("");
      digitalWrite(solenoid, LOW);
      break;
   
      case 1: 
      // code to display sequence 1 on lcd
      lcd.clear();
      lcd.setCursor(0,0);
      lcd.print("Hello");
      lcd.setCursor(0,1);
      lcd.print("sequence 1");
      digitalWrite(solenoid, LOW);
      break;
      
      case 2: 
      // code to display sequence 2 on lcd
      lcd.clear();
      lcd.setCursor(0,0);
      lcd.print("Hello");
      lcd.setCursor(0,1);
      lcd.print("sequence 2");
      digitalWrite(solenoid, HIGH);
      break;
      
      case 3: 
      // code to display sequence 3 on lcd
      lcd.clear();
      lcd.setCursor(0,0);
      lcd.print("Hello");
      lcd.setCursor(0,1);
      lcd.print("sequence 3");
      digitalWrite(solenoid, LOW);
      break;
      
      case 4: 
      // code to display sequence 4 on lcd
      lcd.clear();
      lcd.setCursor(0,0);
      lcd.print("Hello");
      lcd.setCursor(0,1);
      lcd.print("sequence 4");
      digitalWrite(solenoid, LOW);
      break;
      
      case 5: 
      // code to display sequence 5 on lcd
      lcd.clear();
      lcd.setCursor(0,0);
      lcd.print("Hello");
      lcd.setCursor(0,1);
      lcd.print("sequence 5");
      digitalWrite(solenoid, HIGH);
      break;
      
      case 6: 
      // code to display sequence 6 on lcd
      lcd.clear();
      lcd.setCursor(0,0);
      lcd.print("Hello");
      lcd.setCursor(0,1);
      lcd.print("sequence 6");
      digitalWrite(solenoid, LOW);
      break;
      
      case 7: 
      // code to display sequence 7 on lcd
      lcd.clear();
      lcd.setCursor(0,0);
      lcd.print("Hello");
      lcd.setCursor(0,1);
      lcd.print("sequence 7");
      digitalWrite(solenoid, HIGH);
      break;
      
      case 8: 
      // code to display sequence 8 on lcd
      lcd.clear();
      lcd.setCursor(0,0);
      lcd.print("Hello");
      lcd.setCursor(0,1);
      lcd.print("sequence 8");
      digitalWrite(solenoid, LOW);
      break;
      
      case 9: 
      // code to display sequence 9 on lcd
      lcd.clear();
      lcd.setCursor(0,0);
      lcd.print("Hello");
      lcd.setCursor(0,1);
      lcd.print("sequence 9");
      digitalWrite(solenoid, HIGH);
      break;
      
      case 10: 
      // code to display sequence 10 on lcd
      lcd.clear();
      lcd.setCursor(0,0);
      lcd.print("Hello");
      lcd.setCursor(0,1);
      lcd.print("sequence 10");
      digitalWrite(solenoid, LOW);
      break;
      // etc etc
 
   } // end of case

} //  end of void loop
case 1: 
      // code to display sequence 1 on lcd
     //  lcd.clear();
      lcd.setCursor(0,0);
      lcd.print("Hello");
      lcd.setCursor(0,1);
      lcd.print("                   ");   //clear the line with enough spaces to overwrite old data
      lcd.setCursor(0,1);             //move the cursor back
      lcd.print("sequence 1");
      digitalWrite(solenoid, LOW);
      break;

Clearing the line with spaces and resetting the cursor takes a lot less time than clearing the screen.

OK, I have got rid of all the lcd.clear s and added lcd.print(" ")
It seems to be faster. I still need a delay or the message is dimm if switches held down, and flickers if delay much less than 250.