binary out

hi everybody i' copletely new with arduino (received mine today), i need to output the value of a pot on 6 bits and I haven't found a way to do this.can anyone help me??

I am not understanding your question.

You want to read a voltage(adjusted by a pot) then output it on 6 bits?

do you mean output it in binary on 6 different pins? Why 6?

maybe a little more context will help us help you :D

If I understand correctly, you are wanting to read in an analog value (pot), and display it in binary coded decimal format. If that is indeed the case, this method works well:

bit1 = (pot & 0x01); // LSB
bit2 = (pot>>1 & 0x01);
bit3 = (pot>>2 & 0x01);
bit4 = (pot>>3 & 0x01);
bit5 = (pot>>4 & 0x01);
bit6 = (pot>>5 & 0x01); // MSB

bit2 = (pot>>1 & 0x01);

Tweaked.. Can you explain to me what this line of code does? I get that each line comes up with the binary bit for each pin, but I don't understand what is happening. Also, I have never seen or needed hex in code before. I get that it is base 16 which is a nice multiple of 8(binary byte). But why do we use it like this? Thanks for any knowledge you may offer.

[edit]Also, wrong forum topic.[/edit]

pat

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._

P_Wood,

& 0x01 is a single bit mask, essentially we are 'anding' the value (pot) with a hexidecimal one, which results in a one or zero.

pot>>1 is a bit shift right, which shifts the bits of (pot) one to the right.

check out bit shift in the reference section, that may clear things up!

I'm wondering if the question was how to read the value of a pot connected to pin 6?

thanks everybody for your answers, and sorry for my not-clear-enough question! Focalist found what i'm looking for, i will try to work this during the week end. what i want to do is driving a relay based card to control the gain of an audio device, sometime i need to send the pot value, sometimes i need to send a "preset" value. the card has 6 relays with 1,2,4,8,16 and 32 dB of attenuation

i'll soon let you know how is it going...

Cool, glad I could help.

These little beasties are a ton of fun, just don't let a successful project stop you from breaking something else that doesn't need to be broken, or creating something for someone else to break!

Bitwise operators are the "right" way to do this, some might say.. I'd suggest you do a bit of reading on it in a general programming sense, if you'll be doing a lot of this sort of thing... it's cleaner and faster if you are doing a lot of it. On the other hand, using loops and such like I did makes for code that "reads" a little better, especially to people that don't spend a lot of time coding. If this is the extent of the project, it really doesn't matter.

There's always a number of ways to come at any problem, as long as the end result is what you need, it often doesn't matter how you got there...

as an aside, for some reason this makes me think about building an audio VU meter that displays in Binary on LED's...lol... OK, that's geeky, even for me.

hi
i’m tiying to make Focalist’s code (thanks again!) run, here is how it is now:

void setup()
{
Serial.begin(9600);
int pwr[6] = { 1, 2, 4, 8, 16, 32 };
int bits[6] = { 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=6;
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);
}
when I click on “verify” there is an error that I don’t understand:it’s about the higlited part above and says:
In function ‘void loop()’:
error: ‘i’ was not declared in this scope
could someone explain why this error appears_

change the highlighted part to i=5, since the array consists of bits[0] through bits[5]

hth, Billy

Plus you need to move

int pwr[6] = { 1, 2, 4, 8, 16, 32 };
int bits[6] = { 0, 0, 0, 0, 0, 0 };
int i,val;

Outside of setup so they're defined as global variables.

Andrew

erm, yah.. variable scope.. and the six must have been a typo. I'll blame it on blind coding while half awake ;)

When I was doing development work, I was doing Oracle/SQL and VB (client/server special purpose apps)- both of which are notorious for their variable declarations.. or should I say the lack thereof, in most cases. Code efficiency and data compactness aren't what you are looking for, as much as readability and understandability- because some poor schmuck is going to have to deal with it eventually, when they want to change the code. Way too many bad habits get ingrained when playing by the rules becomes optional. "Forced" back into a strict-syntax language like C, I sometimes pay for all the evils I got away with it seems... at least we can avoid pointer operations in Arduino.. almost guaranteed to confuse even the programmer writing the code...