Turning 6 inputs into a Binary Number

Hi All, I have been trying to find out how I can read 6 digital inputs and turn the results into one Binary number ie 100101 so I can change settings depending on which switches are on or off. Does anyone know where I can find this information or have any code snippets that would help ? Thanks

Have a look at the bitWrite function.

for count 0 to 5
  bitWrite(variable, count, digitalRead(count))
end of for

The real code would use an array to hold the pin numbers of the switches indexed by the count variable

Little more detail?

for ( byte count = 0, count < 5, count++ )
{
  bitWrite(variable, count, digitalRead( pin[count] ));
} // end of for count

I would expect switch/button bouncing to make occasional bugs with this code if the switches/buttons are manual.

This might be candidate for using ports?

You should take a look a look at shift in registers, basically you parallel all your digital inputs, and then it spews out (on latch request) the data to arduino, and the beauty is you can add as many as you want....

http://www.arduino.cc/en/Tutorial/ShiftIn

Here's a way that's not fancy, but easy to understand:

int BinaryThing()
{
  int Sum = 0;

  if (digitalRead(Pin0) == HIGH)
    Sum += 1;
  if (digitalRead(Pin1) == HIGH)
    Sum += 2;
  if (digitalRead(Pin2) == HIGH)
    Sum += 4;
  if (digitalRead(Pin3) == HIGH)
    Sum += 8;
  if (digitalRead(Pin4) == HIGH)
    Sum += 16;
  if (digitalRead(Pin5) == HIGH)
    Sum += 32;
  return(Sum);
}
1 Like

Now for extra credit turn that into a for loop of only four lines.
Hint you will need the shift operator <<

Actually Mike…

If you connected them up from say 2 - 9.

http://www.arduino.cc/en/Reference/PortManipulation

Port registers allow for lower-level and faster manipulation of the i/o pins of the microcontroller on an Arduino board. The chips used on the Arduino board (the ATmega8 and ATmega168) have three ports:

B (digital pin 8 to 13)
C (analog input pins)
D (digital pins 0 to 7)

    Serial.print(78, BIN) gives "1001110"
    Serial.print(78, OCT) gives "116"
    Serial.print(78, DEC) gives "78"
    Serial.print(78, HEX) gives "4E"
    Serial.println(1.23456, 0) gives "1"
    Serial.println(1.23456, 2) gives "1.23"
    Serial.println(1.23456, 4) gives "1.2346"

going by what I just tried, i don’t get 0’s or maybe because pins 0,1,2 are used (LSB) so there’s no point showing 000… for the rest of the ports?

Try this code.

void setup() {                
  pinMode(led, OUTPUT);     
  Serial.begin(9600);
}

void loop()
{
  byte ports;
  ports = PIND;
  Serial.println(ports,BIN);
  delay(1000);               // wait for a second
}

going by what I just tried, i don't get 0's

Did you post the wrong code?

try it, you'll get back 11 if you switch pin 2 high you get 111 not for ex 11100000 but 111001 should be visible

you'll get back 11 if you switch pin 2 high you get 111 not for ex 11100000 but 111001 should be visible

I have no idea what you're going on about.

pin 0 / 1 trx/recv...

so you'll see 11 depending on the state.

turn pin 2 on it reads (with respect to the pin 0/1) 111

just try the code, hard to explain

TanHadron:
Here’s a way that’s not fancy, but easy to understand:

int BinaryThing()

{
  int Sum = 0;

if (digitalRead(Pin0) == HIGH)
    Sum += 1;
  if (digitalRead(Pin1) == HIGH)
    Sum += 2;
  if (digitalRead(Pin2) == HIGH)
    Sum += 4;
  if (digitalRead(Pin3) == HIGH)
    Sum += 8;
  if (digitalRead(Pin4) == HIGH)
    Sum += 16;
  if (digitalRead(Pin5) == HIGH)
    Sum += 32;
  return(Sum);
}

Or the same method streamlined with arrays. Both your method and this method are easy to understand because they mirror the standard manual binary to decimal conversion method.

int dipPins[] = {3, 7, 4, 12, 5, 9};  //An array to receive a binary 6-bit number on not necessarily consecutive pins, ordered by LSb to MSb

void setup()
{
  //Set each pin connected to the DIP switch as an input
  for(int i = 0; i < 6; i++)
  {
    pinMode(dipPins[i], INPUT);
  }

  // Any other setup required

}

void loop()
{
  // code, code, and more code.

  int binaryNumber = parallelToByte();

  // do what ever you want with binaryNumber
  // more code, more code, and even more code.

}

/*
 * parallelToByte() - converts a binary value on 6 digital inputs to an integer.
 */
int parallelToByte()
{
  int ret = 0;  // Initialize the variable to return
  int bitValue[] = {1,2,4,8,16,32};  // bit position decimal equivalents
  for(int i = 0; i < 6; i++)  // cycle through all the pins
  {
    if(digitalRead(dipPins[i]) == HIGH)  // because all the bits are pre-loaded with zeros, only need to do something when we need to flip a bit high
    {
      ret = ret + bitValue[i];  // adding the bit position decimal equivalent flips that bit position
    }
  }
  return ret;
}

I wrote the basis of this back as one of my first modifications to CIRC-02 from ardx.org to read a value off a dip-switch. I’d probably change things now. First of all, the vast majority of my "int"s will never have a value large enough to require 2 bytes, so I’d declare them as “byte”. (Also, they would never be a negative number so the unsigned “byte” datatype fits better that way than the signed “int” datatype.) I’d also probably use INPUT_PULLUP on my input pins and change the logic in the calculation loop to look for LOWs. Also, now that I’m more familiar with things, I’d probably use either the bitWrite() http://arduino.cc/en/Reference/BitWrite or bitSet() http://arduino.cc/en/Reference/BitSet functions instead of creating and destroying an array every time the function is called.

Another technique I would probably use at the global declaration stage is declare as constants, and add another constant that calculates it’s value against the size of my dipPins array to use as the limit for the for loops. This way if I decide to add or remove an input pin, I only need to add or remove the pin number from the declaration and the rest of my script would automagically compensate. It might look like:

const byte dipPins[] = {3, 7, 4, 12, 5, 9};  //An array to receive a binary 6-bit number on not necessarily consecutive pins, ordered by LSb to MSb
const byte dipSize = sizeof(dipPins);  // Size of the dipPins[] array in number of bytes. (Remember, this is one of the few built in functions that doesn't use camel case...)

I hope this gives you something to chew on.