Go Down

### Topic: Convert digital inputs to byte (Read 2916 times)previous topic - next topic

#### Pelicar

##### Feb 14, 2011, 12:26 pm
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.

#### AWOL

#1
##### Feb 14, 2011, 12:27 pm
Have a look at "bitWrite"
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

#### jp128

#2
##### Feb 14, 2011, 12:30 pm
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.

John-Paul

#### djc

#3
##### Feb 14, 2011, 02:39 pmLast Edit: Feb 14, 2011, 06:17 pm by djc Reason: 1
Do you want 'easy' or do you want 'fast'?

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

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

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:

Code: [Select]
`byte temp = (PIND & 0xFC) | (PINB & 0x03);`

(Edited to correct stupid mistake)

#### AWOL

#4
##### Feb 14, 2011, 02:46 pm
Or better still,
Code: [Select]
`byte temp = (PIND & 0xFC) | (PINB & 0x03);`
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

#### djc

#5
##### Feb 14, 2011, 06:18 pm

Or better still

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

#### jcarrr

#6
##### Feb 15, 2011, 02:10 am
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

#### PaulS

#7
##### Feb 15, 2011, 10:43 am
Quote
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.

#### Pelicar

#8
##### Feb 15, 2011, 12:08 pm
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

#### jcarrr

#9
##### Feb 19, 2011, 02:25 am
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

#### PaulS

#10
##### Feb 19, 2011, 02:45 am
Quote
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.

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

Go Up

Please enter a valid email to subscribe