Pages: [1]   Go Down
Author Topic: Convert digital inputs to byte  (Read 1659 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 309
Posts: 26512
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Have a look at "bitWrite"
Logged

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

Wisconsin
Offline Offline
Newbie
*
Karma: 2
Posts: 26
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

Logged

John-Paul
Geek Squad - Getting Geekier smiley-grin

0
Offline Offline
Newbie
*
Karma: 1
Posts: 28
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
byte temp = (PIND & 0xFC) | (PINB & 0x03);

(Edited to correct stupid mistake)
« Last Edit: February 14, 2011, 12:17:09 pm by djc » Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 309
Posts: 26512
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Or better still,
Code:
byte temp = (PIND & 0xFC) | (PINB & 0x03);
Logged

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

0
Offline Offline
Newbie
*
Karma: 1
Posts: 28
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Or better still

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

0
Offline Offline
Jr. Member
**
Karma: 3
Posts: 77
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 642
Posts: 50367
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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. smiley-cool
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

0
Offline Offline
Jr. Member
**
Karma: 3
Posts: 77
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 642
Posts: 50367
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Pages: [1]   Go Up
Jump to: