# How do I ask as soon as possible 511 switch positions?

I would like to retrieve 511 different positions via nine switches. 511 If queries are a bit much work. How to do this faster? The respective position 1 ... 511 is to be stored in a variable. Thanks for help!

How do the 511 possibilities relate to the different switches? Would you be OK with an int where bit 0 was the position of the first switch, bit 1 is the second switch all the way to bit 8 being the 9th switch? You could just shift in the digitalReads as you take them.

Assuming the pin numbers for the switches are in an array called pins:

``````int val = 0;
for(int i = 8; i>=0; i--){
val = (val << 1) | digitalRead(pins[i];
}
``````

Each switch has its own pin on the Arduino. To be counted in binary.
Switch 1 = 1, switch 2 = 2, switch 1 and switch 2 = 3, switch 4 = 4, switches 1 and 4 = 5, and so on.

Then you don’t have 511 possibilities since some are redundant. switch 2 and switch 3 would be 5 but so is switch 1 and switch 4.

If you want them as binary then you’ll have to count by powers of 2 and have switch 1, 2, 4, 8, 16, 32, 64, 128, and 256. In that case the solution provided in the first reply gets what you want.

To get what you have written in reply #2 you only need a small change.

Again assuming the pin numbers are in an array called pins:

``````int val = 0;
for(int i = 0; i < 9; i++){
val += i;
}
}
``````

Are the switches all connected to 2 ports?
Then you could use PINx to read in whole port, and combine the 2 values to make one number.
Using PORTC (37 to 30) and PORTA (22 to 29):
lowBits = PINC; // D30 = MSB, D37 = LSB
highBits = PINA; // D29 = MSB, D22 = LSB
int resultingNumber = (highBits <<8 ) | lowBits;

No, switch1 = pin 2, switch2 = pin 3, switch3 = pin 4....switch9 = pin 10

mat21his:
No, switch1 = pin 2, switch2 = pin 3, switch3 = pin 4....switch9 = pin 10

On what sort of Arduino? If that's an UNO then you're all in PORTD and PORTB. A little shifting and you could do what @Crossroads suggests.

Can you answer the question about whether or not there are redundant combinations? Is switch 2 AND 3 the same as 1 AND 4? Or are those two combinations different?

Like making it hard on yourself, eh?
Then Delta_G's approach is best.

``````// pins to pins are LSB to MSB
``````

or do it in a loop, but it will take a little longer.

It is an Arduino UNO Rev. 3rd
It is not redundant, because switch 3 (value 4) and switch 2 (value 2) resulting value 6. Switch 4 (value 8 ) and switch 1 (value 1) give a value. 9

Switch 1 (pin 2) = value 1
Switch 2 (pin 3) = value 2
Switch 3 (pin 4) = Value 4
Switch 4 (pin 5) = value 8
Switch 5 (pin 6) = value 16
Switch 6 (pin 7) = value 32
Switch 7 (pin 8 ) = value 64
Switch 8 (pin 9) = value 128
Switch 9 (pin 10) = 256.

So that are 511 different switch positions. (Its a DIP Switch)
Each position has a numerical value. This value is to be stored in a variable.

OK, well you've got at least three good solutions to your problem now. You can read the whole port and use bitmath to get the parts you need (Reply #4). You can read them all in a big line and multiply them by their appropriate values and add (Reply #7). Or you can shift them through an int as you read them (reply #2).

Then what I said originally would work. Something had me thinking Mega.

``````// lowBits are on 7-6-5-4-3-2-x-x of Port D, mask of 2 lower bits
// highBits are on x-x-x-x-x-10-9-8 of Port B, mask off 5 upper bits
// shift upper bits into position, combine with lower bits, shift all into proper position
int result =( ( (0x07 & PINB)<< 8) | (0xFC & PIND) )>> 2;
``````

9 bits make 512 combinations, 0-511.

Like making it hard on yourself, eh?
Then Delta_G’s approach is best.

``````// pins to pins are LSB to MSB
``````

``````