detecting rotary switch position change through interrupt

I have a single pole 5 position rotary switch, which is connected through a resistor ladder to an analog pin
the rotary switch is a regular mechanical switch (NO encoders) with wiper contact on each pole.
wired up similar to http://forum.arduino.cc/index.php/topic,20125.0.html

I call analog Read by polling and now i need to get rid of polling and call analog read only when there
is a change in the position.

I would imagine that when the knob is turned the wiper contact is floating, can this be hooked
up in some way to generate a pulse that can be fed into the interrupt pin, which in turn can
request for a analogRead thus removing the need to poll.

since the knob is is being turned by hand, i expect 2-5 changes per second at max

has somebody resolved this situation?

It depends on the actual resistor values you are using. When the pole goes between positions it may be not connected, or it may make the connection before breaking. If the pole does float then the Analogue input will be undefined. You need to pull that input either up or down with a resistor and then recalculate the values you will get from the resistor network. You can then use a comparator to detect the high or low pulse, assuming that a high or low value will be outside the range given by your resistor network.

Hello,

can you add a rotary encoder to your device? You could make one that gives you a pulse when the knob is turned from one position to the next. Connect this signal to one of the interrupts of the arduino and you are done.

Elektrix

Don't forget debouncing.

In general, attaching switches to an interrupt is a Bad Idea because of the noisy nature of the input. You'll get lots of overlapping and back-to-back interrupts for each turn of the switch. Polling is actually the preferred method to handle switches.

You could try to do something externally with a comparator and a latch, but it would probably be more complicated than it's worth. Use a pull-down resistor so that the output goes to gnd when the wiper is not connected to a pole, and set the comparator to signal when its input goes lower than the lowest value of the switch. Feed that into a latch, and the output of that can go to an interrupt pin. You'd need to reset the latch after each read, though.

ckiick:
Don't forget debouncing.

Ok, what I meant is to add an optical encoder to the same axis. Then you don't have the bouncing problem. And maybe a optical rotary encoder is the better choice to solve your problem.

Elektrix

ckiick:
You could try to do something externally with a comparator and a latch, but it would probably be more complicated than it's worth. Use a pull-down resistor so that the output goes to gnd when the wiper is not connected to a pole, and set the comparator to signal when its input goes lower than the lowest value of the switch. Feed that into a latch, and the output of that can go to an interrupt pin. You'd need to reset the latch after each read, though.

You could do the same thing using the internal analog comparator, if you connect the switch wiper to A0 and feed A1 from a voltage divider. However, this will only work if your switch is break-before-make.

Alternatively, if you can afford to use 3 digital or analog pins instead of a single analog pin, you can use 3 diodes to turn the 5-positions into a binary code on the 3 inputs. Then you can use pin change interrupts to detect transitions.

However, unless you have a requirement to put the mcu to sleep and wake it up when the switch is turned, then it is better to stick with polling. If your project includes an LCD display that can be used to display the current selection, then it may be better to use a rotary encoder instead.