DIP switch use [SOLVED]

Hi,

I'm trying to figure how may I use a 3 position dip switch. I would like to set the watchdog to wake up the atmega328 from 1 to 8 seconds, according to the dip switch settings.
But how do I read the setting, without using three of the chip's ports?

Use them with some resistors to make different analog levels, read the analog voltage.

Thanks CrossRoads.
Out of curiosity, if I needed to use an 8 pos dip switch would a shift in register do the job?

tzanampeths:
Thanks CrossRoads.
Out of curiosity, if I needed to use an 8 pos dip switch would a shift in register do the job?

Yup (you'd also need pullups or pulldowns, as appropriate, so when the switches are off, they're pulled to the correct state)

Ok, I'm all set!
Thank you!

tzanampeths:
Thanks CrossRoads.
Out of curiosity, if I needed to use an 8 pos dip switch would a shift in register do the job?

Well, it would, but you need three I/O functions; latch the data, shift the data and an input for the data.

There are some sneaky tricks using R-C time constant networks, indeed it may even be possible to read a shift register using a single pin. In general, the trick is to use two output ports that are shared with something else and for which each use it does not matter if they are sent random data at any other time than for the current interrogation.

So whilst this may not seem a good use of I/O pins for three DIP switches, if you can "share" two lines with something else, it may well be.

I have come to dislike the use of resistor combination encoding of switches to analog ports due to my experience of such arrangements failing in many devices including computer monitors, microwave ovens, MP3 players, mobile phones and tablet computers. I suspect it to be in fact the most common failure mode of these due to moisture ingress to the push-buttons; that may not be so much of a problem with DIP switches.

@Paul__B So you mean that, say, the "to" latch pin is an output. And I may have two devices connected to it and some usage will be relevant to device 1, while some other to device 2? Or use it as arduino output to the shift register and as arduino input to some other device?

Not sure what you mean by "to".

If you use two outputs for clock (shift) and latch, then if some other part of the program uses them for some purpose (even as an input, thought that would not be a good idea), all that will happen is that data will be randomly latched into the register and shifted through. When it comes to the part of your program where you now wish to read the register, you will perform a clean latch of the current data, shift it out and read that output into your chosen Arduino input pin.

The same two pins that you used for clock (shift) and latch can then be used by some other part (or multiple parts) of the program to perform a different function as long as it does not matter if they are in the same way, apparently "randomly" written when that function is not being performed. For example, they could be used as the clock and data for a shift out register which would then use a different pin as latch control. As long as that latch pin is not toggled other than immediately after a valid shift procedure has been performed, random data in that shift register does not matter.