So I'm trying to use an 8-wide dip switch to grab some user input in binary. I have a rough sketch of my design and code and I was wondering if it looks okay:
now switchState[7] should contain 1's and 0s which is the binary code.
Why would switchState[7] (a single level of the array) contain anything but a single 0 or 1 ?
Do you perhaps mean that the switchState array as a whole will contain the number as a series of 0s and 1s ? If so then the array will need to have 8 entries of which number 7 is the last as the entries are numbered from 0 to 7
Can I suggest that you post your whole program rather than just some lines of code from it
Your switch is wired "backwards". It should be wired between the digital inputs and ground so the switch can pull-down, "overpowering" the internal pull-ups. (You don't need a 5V connection to the breadboard.)
Since on=low when using pull-ups, you may have to flip the switch around or reverse the logic in software.
Depending on what you want, you can write the data into a byte (instead of an array) with [u]bit shifting[/u]. Then, you'd have a regular "binary" variable that you can read-out as decimal (or hex) if you wish.
There are also port commands that would allow you to read all 8-bits into a variable at once, but I can't help you with that 'cause I've never done it, and I don't believe it's covered in the "basic" language reference.
UKHeliBob:
Why would switchState[7] (a single level of the array) contain anything but a single 0 or 1 ?
Do you perhaps mean that the switchState array as a whole will contain the number as a series of 0s and 1s ? If so then the array will need to have 8 entries of which number 7 is the last as the entries are numbered from 0 to 7
Can I suggest that you post your whole program rather than just some lines of code from it
My apologies. switchState[] should contain the number as a series of 0s and 1s. That 7 should not have been there.
You may find bitWrite() an easy way to write bits to a single byte:
The most efficient way to do this is to have all the switches wired to a single port in order of the bits of that port (which you do not currently) but unfortunately on the Uno that is only possible with PORTD but PD0 and PD1 are used for uploads and also for Serial so it's not a great idea to use those pins for anything else.
For the sake of learning I don't think there is anything wrong with just using digitalRead(). It's not as if speed is important for this application.
DVDdoug:
Your switch is wired "backwards". It should be wired between the digital inputs and ground so the switch can pull-down, "overpowering" the internal pull-ups. (You don't need a 5V connection to the breadboard.)
Since on=low when using pull-ups, you may have to flip the switch around or reverse the logic in software.
Depending on what you want, you can write the data into a byte (instead of an array) with [u]bit shifting[/u]. Then, you'd have a regular "binary" variable that you can read-out as decimal (or hex) if you wish.
There are also port commands that would allow you to read all 8-bits into a variable at once, but I can't help you with that 'cause I've never done it, and I don't believe it's covered in the "basic" language reference.
Perfect! That solved it! I appreciate the help. Just read up again on pull-up resistors and it all makes sense now. Thanks everybody for the responses.