binary out

Hoping I got this right... so here goes:

You want to express in six bits- so your range is 2^6, or 64, which we'll return as a value ranging from 0-63. I'll show code below for both serial port binary our as well as using ports. Pick the right one and cut the code and there you go. If parallel binary is what's needed, you'll find it on pins digital 5-10. Note that you can change the output pins to whatever you want, I just arbitrarily picked those. Probably makes more sense to use 3-8 (skipping 1&2 just cuz I usually like leaving those lines alone unless I have a good reason)

void setup()
{
Serial.begin(9600);
int pwr[5] = ( 1, 2, 4, 8, 16, 32 );
int bits[5] = ( 0, 0, 0, 0, 0, 0 );
int i, val;
pinMode(5,OUTPUT);
pinMode(6,OUTPUT);
pinMode(7,OUTPUT);
pinMode(8,OUTPUT);
pinMode(9,OUTPUT);
pinMode(10,OUTPUT);
};

void loop()
{
\ This code samples and converts to a binary array
i=5;
val = analogRead(0); \ read the potentiometer and use map() to scale it.
val = map(val, 0, 1023, 0, 63); \ convert to our translated number
while (i>-1) \ to binary. Just loop the powers
{ \ of two, subtract and set bits.
bits*=0;*
_ if (val>=pwr*)_
_
{_
_ val=val-pwr;
bits=1;
};
i=i-1;
};
\ Now we have our binary array. Let's send it.
i=5;
while (i>-1)
{_

_serial.Print(bits);_
_if bits=1*
{
digitalWrite(10-i, HIGH);
};
i=i-1;
};
Serial.println(" ");
delay(100);
}
I know I could do it "neater", but the longer code may be helpful in showing what's going on.... let's hope that me coding blind at 4:30am while not able to sleep produces workable code :wink:
Hmm..also had to come back and edit once I remembered there's no exponentiation operator... then just decided the array makes it even easier to read. Excuse any coding errors, I'm not running this in a parser or the IDE- fix syntax as needed.
The output code outputs as MSB first. Change the loop counter to change to LSB...
One other way would be to build a string, then output that to serial, but I think we're getting a little silly now._