Pages: [1]   Go Down
Author Topic: detecting rotary switch position change through interrupt  (Read 596 times)
0 Members and 1 Guest are viewing this topic.
India
Offline Offline
Newbie
*
Karma: 0
Posts: 1
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

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

Germany
Offline Offline
Full Member
***
Karma: 1
Posts: 141
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

My blog about arduino and Linux themes:
http://heliosoph.mit-links.info/

Austin, TX
Offline Offline
Full Member
***
Karma: 1
Posts: 182
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Chris J. Kiick
Robot builder and all around geek.

Germany
Offline Offline
Full Member
***
Karma: 1
Posts: 141
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

My blog about arduino and Linux themes:
http://heliosoph.mit-links.info/

United Kingdom
Offline Offline
Tesla Member
***
Karma: 220
Posts: 6587
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Formal verification of safety-critical software, software development, and electronic design and prototyping. See http://www.eschertech.com. Please do not ask for unpaid help via PM, use the forum.

Pages: [1]   Go Up
Jump to: