How to create 1 variable from 3 input switches?

So I am doing a project for school in which there are 3 input switches that control 8 LED lights. Each binary combination of the 3 switches will do something different (000 lights up all of them, 001 lights up every other one, etc). Is there a way to string these three binary values together into one binary number which can also be stored as a decimal integer? I tried using the bitWrite, but I can't get it to work correctly. I tried using a switch loop on the variable created out of the binary inputs (case 0, case 1, case 2, case 3.... case eight ) but that didn't work correctly. I know I could get it to function other ways, such as treating each binary input as a separate variable, but I want to make this as efficient as possible.

//Lab 8

//Pin Initialization
int sw1 = 13;	//Switches 1-3
int sw2 = 12;
int sw3 = 11;
int r1 = 3;		//Right blinkers
int r2 = 2;
int r3 = 1;
int r4 = 0;
int l1 = 4;		//Left blinkers
int l2 = 5;
int l3 = 6;
int l4 = 7;


void setup() {
  pinMode(r1, OUTPUT);	//Right and left blinkers
  pinMode(r2, OUTPUT);	//Are outputs (blue wires)
  pinMode(r3, OUTPUT);
  pinMode(r4, OUTPUT);
  pinMode(l1, OUTPUT);
  pinMode(l2, OUTPUT);
  pinMode(l3, OUTPUT);
  pinMode(l4, OUTPUT);
  pinMode(sw1, INPUT);	//Switches are inputs
  pinMode(sw2, INPUT);	//(green wires)
  pinMode(sw3, INPUT);
}

void loop() {
  int input;
  bitWrite(input,0,digitalRead(sw1));	//Set bit values for
  bitWrite(input,1,digitalRead(sw2));	//input starting with LSB
  bitWrite(input,2,digitalRead(sw3));	//(input,bit,value from sw1
  
      	
      	
}

Sure, put them all in one port, say 10-9-8, which is PORTB bits 2-1-0 on an Uno.
Then you can read all 3 and get one value out:

//use INPUT_PULLUP here in setup(), wire the pins to connect to Gnd when switch is closed
  pinMode(sw1, INPUT);	//Switches are inputs
  pinMode(sw2, INPUT);	//(green wires)
  pinMode(sw3, INPUT);
void loop(){
// To read the 3 switches together:
switches = PORTB & 0b0000111;
switch(switches){
case 0:
// whatever
break;
case 1:
// whatever
break:
:
:
case 7:
// whatever
break;
} // end switch:case
} // end loop

Another approach to generating that single value from the switches:

void loop() {
byte input; //using a byte because we're only using 3 bits here, so no need to use a whole 2-byte int
input=(digitalRead(sw1)&(digitalRead(sw2)<<1)&(digitalRead(sw3)<<2));
}

Alright guys, thanks for the help so far. I tried both suggestions but I couldn't get either to work. This is my code right now, and every time I run it, it defaults to case(0) and never changes, even when the switches are thrown. Any idea what's going on with the code? It's as if PORTB is always 00000000. I will post a picture of the updated circuit as well.

//Pin Initialization
int sw1 = 8;	//Switches 1-3
int sw2 = 9;
int sw3 = 10;
int r1 = 3;		//Right blinkers
int r2 = 2;
int r3 = 1;
int r4 = 0;
int l1 = 4;		//Left blinkers
int l2 = 5;
int l3 = 6;
int l4 = 7;


void setup() {
  pinMode(r1, OUTPUT);	//Right and left blinkers
  pinMode(r2, OUTPUT);	//Are outputs (blue wires)
  pinMode(r3, OUTPUT);
  pinMode(r4, OUTPUT);
  pinMode(l1, OUTPUT);
  pinMode(l2, OUTPUT);
  pinMode(l3, OUTPUT);
  pinMode(l4, OUTPUT);
  pinMode(sw1, INPUT);	//Switches are inputs
  pinMode(sw2, INPUT);	//(green wires)
  pinMode(sw3, INPUT);
}

void loop() {
  byte input = PORTB & B00000111;
  switch (input)
  	{
    	case(0):
    		PORTD = B10101010;
    		break;
    	case(1):
    		PORTD = B00000001;
    		break;
    	case(2):
    		PORTD = B00000010;
    		break;
    	case(3):
    		PORTD = B00000011;
    		break;
    	case(4):
    		PORTD = B00000100;
    		break;
    	default:
    		PORTD = B00000000;
    		break;
  }
}

CrossRoads:
Sure, put them all in one port, say 10-9-8, which is PORTB bits 2-1-0 on an Uno.
Then you can read all 3 and get one value out:

//use INPUT_PULLUP here in setup(), wire the pins to connect to Gnd when switch is closed

pinMode(sw1, INPUT); //Switches are inputs
  pinMode(sw2, INPUT); //(green wires)
  pinMode(sw3, INPUT);
void loop(){
// To read the 3 switches together:
switches = PORTB & 0b0000111;
switch(switches){
case 0:
// whatever
break;
case 1:
// whatever
break:
:
:
case 7:
// whatever
break;
} // end switch:case
} // end loop

First, get it working with digitalWrite() and its friends. That way it works reliably on any Arduino and a huge number of Arduino-like things. Only go to the PORTB manipulation when you are totally squeezed for speed and you know* you will never be asked to use a different Arduino at the last moment.

Does DrAzzy's solution make sense to you? Did you try that?

*You can't know this. In fact it is usually more beneficial to act as if this is false.

int input;
  bitWrite(input,0,digitalRead(sw1));	//Set bit values for
  bitWrite(input,1,digitalRead(sw2));	//input starting with LSB
  bitWrite(input,2,digitalRead(sw3));	//(input,bit,value from sw1

If things are wired correctly, and the switches are working, this original code should be working. You would be better off to explicitly initialize input = 0.

Hi,
I am learning programming the Arduino and read the forum to learn from gurus how to do thing in Arduino.
Here he is trying to read the PORB doing as follow switches = PORTB & 0b0000111. I think it should be as switches = PINB & 0b0000111. Remembered I am learning

cattledog:

int input;

bitWrite(input,0,digitalRead(sw1)); //Set bit values for
  bitWrite(input,1,digitalRead(sw2)); //input starting with LSB
  bitWrite(input,2,digitalRead(sw3)); //(input,bit,value from sw1




If things are wired correctly, and the switches are working, this original code should be working. You would be better off to explicitly initialize input = 0.

Okay, so that worked this time. I have rearranged the circuit a little bit and some of my code before may have been wrong whenever I originally wrote this, but now it's working! Thanks so much for all the help guys.

MorganS:
First, get it working with digitalWrite() and its friends. That way it works reliably on any Arduino and a huge number of Arduino-like things. Only go to the PORTB manipulation when you are totally squeezed for speed and you know* you will never be asked to use a different Arduino at the last moment.

Does DrAzzy's solution make sense to you? Did you try that?

*You can't know this. In fact it is usually more beneficial to act as if this is false.

By the way, I don't have a problem using digitalWrite(), but I chose to use port manipulation for two reasons: 1) Our lab manual specifically asked us to use port manipulation (I'm in a microcontroller class for EE, and we've been using port manipulation on other microcontrollers like the 68HC12 using assembly [thank God that's over with] so we understand what's actually happening instead of using Arduino library syntax).
2)

void loop() {
  byte input = 0;
  int i, j;
  bitWrite(input,0,digitalRead(sw1));	//Set bit values for
  bitWrite(input,1,digitalRead(sw2));	//input starting with LSB
  bitWrite(input,2,digitalRead(sw3));	//(input,bit,value from sw1)
  switch (input)
  	{
    	case(0):						//Right sequential
    		for(i=3;i>=0;i--)			//blinker
            	{
              	digitalWrite(i,HIGH);
              	delay(500);
            	}
    		break;
    
    	case(1):						//Left sequential
    		for(i=4;i<=7;i++)			//blinker
            	{
              	digitalWrite(i,HIGH);
              	delay(500);
            	}
    		break;
    
    	case(2):						//Scanning sequence
    		for(i=3;i>=0;i=i)			//(from inner to outer
            	{
              	for(j=4;j<=7;j++)
                	{
                  	digitalWrite(i,HIGH);
                  	digitalWrite(j,HIGH);
                  	delay(500);
                  	digitalWrite(i,LOW);
                  	digitalWrite(j,LOW);
                  	i--;
                	}
            	}
    		break;
    
    	case(3):						//Flashing lights
    		PORTD = B11111111;
    		delay(500);
    		PORTD = B00000000;
    		delay(500);
    		break;
    
    	case(4):						//Hazard lights
    		PORTD = B10000001;
    		delay(500);
    		PORTD = B00011000;
    		delay(500);
    		break;
   
    	case(5):						//Back off lights
    		PORTD = B10101010;
    		delay(500);
    		PORTD = B01010101;
    		delay(500);
    		break;
    
    	case(6):						//Brake lights
    		PORTD = B11111111;
    		delay(500);
    		break;
    
    	case(7):						//My sequence
    		break;
    
    	default:
    		PORTD = B00000000;
    		break;
  }
  PORTD = B00000000;					//Sets all LED's back
}										//to 0 before starting new sequence

As you can see that's just a bunch of arbitrary sequences that would take forever to digitalWrite each individual LED or even to loop every sequence.

  1. Our lab manual specifically asked us to use port manipulation

Then you might as well use port manipulation on the input as well as the output. tauro0221 was correct in reply #6. PINX is the input state register, and PORTX is the output state register.

input = PINB & 0b0000111;

Hi,
I think I will do it in this way.

switch (int input_switch = PINB & 0b0000111) {
case 1:
//do something when var equals 1
PORTD = PORTD || input_switch; break; //OR with PORTD to keep the last output condtion
case 2:
//do something when var equals 2
PORTD = PORTD || input_switch; break;
break;
case 3:
//do something when var equals 3
PORTD = PORTD || input_switch; break;
break;
default:
// if nothing else matches, do the default
// default is optional
break;
}
}

Hey @peterpanman
There is the exact solution i did, very efficient and actually i found this out for researching a little bit on Serial Communication by Digital signals, and then this one came out. Well i did this for 4 inputs, and that's why there are total 16 possibilities and you can make smaller or bigger as you want to.

int a, b, c, d, e;

void setup()
{
Serial.begin(9600);

pinMode(A1, INPUT);
pinMode(A2, INPUT);
pinMode(A3, INPUT);
pinMode(A4, INPUT);
Serial.println("All Pins are initialised.");
}

void loop()
{
a = digitalRead(A1);
b = digitalRead(A2);
c = digitalRead(A3);
d = digitalRead(A4);

Serial.println(a);
Serial.println(b);
Serial.println(c);
Serial.println(d);
Serial.println("");
Serial.println("");
delay(500);

e = a8+b4+c*2+d;
switch(e)
{
case 0:
Serial.println("a");
break;

case 1:
Serial.println("b");
break;

case 2:
Serial.println("c");
break;

case 3:
Serial.println("d");
break;

case 4:
Serial.println("e");
break;

case 5:
Serial.println("f");
break;

case 6:
Serial.println("g");
break;

case 7:
Serial.println("h");
break;

case 8:
Serial.println("i");
break;

case 9:
Serial.println("j");
break;

case 10:
Serial.println("k");
break;

case 11:
Serial.println("l");
break;

case 12:
Serial.println("m");
break;

case 13:
Serial.println("n");
break;

case 14:
Serial.println("o");
break;

case 15:
Serial.println("p");
break;
}

}