How to convert HIGH/LOW values to Digital Bits

Hello All,
I am new to the arduino and have a fairly straight-forward question, but I have been unable to find a straight-forward answer while browsing the forum and help/tutorial sections.

I have 3 IR inputs connected to 3 digital inputs on the arduino. My goal is to convert the three inputs to a binary value, for example, if the inputs are HIGH/HIGH/LOW on the three channels, I want to convert that info into a binary value, B110, so I can use it in my script. Is there an easy way of doing this?

Also, a more general question, is the HIGH and LOW returned by DigitalRead() a string when imported? Does it have other values associtated with it? For example, if I use the int() conversion function, will int(HIGH) return 1.

Secondly, if I define, say, the following:

int a=1; int b=1; int c=0; int d=0;

Would it work to create a binary number defining it as:

byte e=Babcd;

being analogous to originally writing:

byte e=B1100

Thanks for your help!
-NICK

Also, a more general question, is the HIGH and LOW returned by DigitalRead() a string when imported?

Imported?
A string - no.

Why not read the referencematerila here:

byte e=Babcd;

Where is "Babcd"" defined?
You need to slow down and learn the basics.
Take a look at bitRead/bitWrite.

int a=1; int b=1; int c=0; int d=0;

Would it work to create a binary number defining it as:

byte e=Babcd;

being analogous to originally writing:

byte e=B1100

That won't work and won't even compile as it's not valid code.

There are ways to combine bits into bytes, they usually involve shifting and ORing the seperate values into a single value.

My goal is to convert the three inputs to a binary value, for example, if the inputs are HIGH/HIGH/LOW on the three channels, I want to convert that info into a binary value, B110, so I can use it in my script. Is there an easy way of doing this?

Something like this

a = digitalread (somepin_1) << 2;
b = digitalread (somepin_2) << 1;
c = digitalread (somepin_3);

value = a | b | c;

or

value = (digitalread (somepin_1) << 2) | 
     (digitalread (somepin_2) << 1) | 
     digitalread (somepin_3);

I'd hit the books and examples to learn more about C.


Rob

Thanks Rob.

I have read through the reference material; the byte variable in my example explanation was defined analogous to the example in the reference material:

"byte

Description
A byte stores an 8-bit unsigned number, from 0 to 255.
Example
byte b = B10010; // "B" is the binary formatter (B10010 = 18 decimal) "

a,b,c,d were defined as integer values 1,1,0,0 respectively. Thus my question concerns the nature of the constituent values when defining a byte, as above in the reference material example.

One could infer that byte b=Babcde is analagous to byte b=B10010 with a=1;b=0;c=0;d=1;e=0. I take it from your reply that this is not recognized by the compiler, and a valid designation in C. Thanks.

And your example code answers my first question. If you can apply a bitshift to the value read from a digital pin, then that means that the value read from that pin can be treated as a binary or integer value of 1 high or 0 low. This was my assumption, but all the examples I flipped through did not use a binary or integer (1 or 0) value to write to a digital pin, rather "HIGH" and "LOW".
Thanks.

I haven't looked but I'm pretty sure that in a header file somewhere you'll find

#define HIGH 1
#define LOW 0

So you are really using 1 and 0, it's just been done like that to make things more readable. Of course it could be argued that this makes things less readable in the sense that you really don't know what values you're dealing with.

Still it's normally considered good practice to hide "magic numbers" behind a descriptive word.


Rob

wonderful, thanks. I had assumed that HIGH and LOW would be so defined, but I hadn't seen it explicitly stated anywhere, and it is certainly significant when manipulating values. And agreed, designations such as these do make scripts more readable and intuitive, but similarly in many ways less comprehensive.