Convert digital inputs to byte

Hello to everybody.

First of all, my name is Francisco and I'm new user in this forum.

I'm starting with Arduino and have a problem in my first project. I need to convert eight inputs from digital pins ( that cames from eight switches) to one byte. The case is that I want to detect and avoid most of one press at time, and I've tought that a good way is search more that one "1" in a 8 bit word.
But, how can I convert 8 digital inputs to one byte word?

Big hug for all.

Have a look at "bitWrite"

So something like this:

pin0 = 1
pin1 = 1
pin2 = 1
pin3 = 0
pin4 = 1
pin5 = 0
pin6 = 1
pin7 = 1
pin8 = 0

Into something like 111010110?

You can use bitWrite(). http://arduino.cc/en/Reference/BitWrite

Just note that it goes from right to left. So the first "1" from the example is bit 7, and the last "0" is bit 0.

Do you want 'easy' or do you want 'fast'?

If you want 'fast', consider using direct port manipulation:

I would say that this is an 'intermediate' way of using the programming language.

It is made slightly more complex by the fact that as standard on a 328-based Arduino, there isn't a full 8 bits available on a single port. Hence, you have to OR two port reads, each of which is ANDed with its own bitmask.

If you don't understand it, then make sure you only READ the PIN registers for the ports you are using. Don't fiddle with anything else!

For instance, say your inputs are on Arduino pins 2-9. These translate into PIND bits 2-7 and PINB bits 0 and 1.

Hence, if you say byte temp=PIND; that will put six bits of PORTD that you do want into temp. Unfortunately, it will also put two bits you don't want in there as well. Hence, you need to blank them off. Thus temp &= B11111100; now puts what you do want back into temp.

Now do the same for PINB. byte temp1=PINB; temp1 &= B00000011; now temp 1 contains the other two bits you're interested in.

Finally, temp |= temp1; puts the full byte into temp.

Written compactly, this becomes:

byte temp = (PIND & 0xFC) | (PINB & 0x03);

(Edited to correct stupid mistake)

Or better still,

byte temp = (PIND & 0xFC) | (PINB & 0x03);

AWOL:
Or better still

Thanks for correcting my mistake. I have edited the original so it's correct.

DJC

Thank you for the really good and complete description of gathering up pin inputs on an arduino. It will be valuable to those who can find it. I have been programming microcontrollers for years and I know everything reasonable, is possible. I would give a lot for an actual printed atmel manual.

JAC

I would give a lot for an actual printed atmel manual.

Download and print the one for the Atmel chip you have. Your only cost will be for paper, ink/toner, and bandwidth. Do it at work, and your cost will be 0. 8)

Hello again and thank's all for answering.

Dear djc, as you well say, being new to this, I am studying your proposal to make no mistake. As soon as I test the code, tell you how I'm gone.

A hug

Pauls,

Paul,

It would be a ream of paper and awkward. I get good mileage out of bound manuals. My 1984 Intel MCS-51 book for 8051 devices is right here above my desk and still 1" thick. The old book for 8047-48 microcontrollers has been put away. I really like bookmarks and margin notations and bound manuals that fit in a brief case.

John c

I really like bookmarks and margin notations and bound manuals that fit in a brief case.

I do, too. Except in my case, it's a backpack that I carry my laptop in. Sticking post-it notes on a pdf file on the screen just doesn't have the same affect.

It would be a ream of paper and awkward.

Can't help you there. There is a lot of material that needs to be covered, so the manual gets to be quite long.