Go Down

Topic: mutliple switches on one analog port and a single interrupt? (Read 764 times) previous topic - next topic

JosAH

Greetings,

I have six switches and I plan to connect them to a single analog input pin through different resistors (a 'trick' that seems to work fine). Just because I know nothing about electronics (I'm an old math sod), here's my question: would it also be possible to connect those switches to, say pin#2, so all those switches would also generate interrupt#0 when one of them is pressed? If possible, how should it be done? Are any additional components necessary besides the different resistors for the switches?

My plan is as follows: if the interupt is triggered, the analog port is read and it is determined which switch is pressed. I know about debouncing the entire shebang.

Thanks for any tips, hints etc.

kind regards,

Jos

Magician

1.  You configure internal analog comparator, which 'd trigger interrupt.  - Complex, two resistor + low level programming.
2. Use additional transistor, passing current trough base-emmiter. Medium, one transistor (internal pull-up active on pin 2), easy programming with attachinterrupt.
3. Same as 2, only you connecting pin 2 and analog in parallel. To reliably trigger interrupt, voltage swing at pin2 should be between 2 and 3 V. Disadvantage of this simplest approach, that range for analog buttons limited to just 2 volts, for example, if no buttons pressed - 5V and any key sets level from 0 to 2V. May be not a problem with internal 1.1V for ADC. Than you setting high (+5V, internal pull-up on pin 2), and keys in 0 to 1V range.

MAS3

Nee, dat gaat niet zomaar.

No, that will not work like that.
You'll be sending analog values to the digital input hoping to get an interrupt.
What would you expect to happen if you send 2.5 volts to that pin ?
Or 1.25 or 3.75 ?
All these values could make sense in case of your setup.
But at best only half of your buttons would generate an interrupt.

There is ofcourse a way to do this, use keys that have 2 contacts.
One to register a key was pressed, and another one to register which key was pressed.

But ask yourself how fast (short) a human could press a key for you to miss it in your program (and the Arduino still be able to acquire the analog value).
Have a look at "blink without delay".
Did you connect the grounds ?
Je kunt hier ook in het Nederlands terecht: http://arduino.cc/forum/index.php/board,77.0.html

retrolefty

Well if I wanted to detect a change on the analog input switch array using interrupts, I would use the general purpose MStimer2 interrupt library ( http://playground.arduino.cc/Main/MsTimer2 ). I would set if for maybe a 100 millisec interrupt interval and in it's ISR I would read the analog input pin and compare that reading from the last valid reading (but in a range as the array will have some small value variation for each switch generated voltage), if it is a new value, save the value as a 'new' last valid reading and also set a variable flag true that the main loop function can test to see if there has been a change in the switches and reset the flag set by the ISR. Might sound a little complex but it's actually very simple and straight forward.

Lefty

JosAH

#4
Jan 19, 2013, 08:59 pm Last Edit: Jan 19, 2013, 09:07 pm by JosAH Reason: 1
Thank you all for your replies; an old saying comes to mind "in theory there's no difference between theory and practice; in practice there is ...". Allow me to show you my (obviously too naive idea); I scribbled down this (warning, ASCII art coming up):

Code: [Select]



    .-----.-----.-----.-----.-----.----------< 5V
    |     |     |     |     |     |
    R1    R2    R3    R4    R5    R6
    |     |     |     |     |     |
    .     .     .     .     .     .
    /     /     /     /     /     /
    .     .     .     .     .     .
    |     |     |     |     |     |
    .-----.-----.-----.-----.-----.-.------.-> Gnd
                                    |      |
                                    |      |
                                    |     Interrupt pin
                                Analog pin      



Assume R1 < R2 < ... < R6; R1 should be large enough not to short circuit the entire thing and R6 should me small enough to still generate a 'rising edge' interrupt while R6-R1 should have a wide enough range for the analog input pin to be able to distinguish between the switches. It sounded fair enough ;-)

w.r.t. transistors: I can barely distinguish a transistor from a hamster (I'm a math guy, remember?) so that's not an option for me. My 'sketch' setup is such that the loop() function is called every < 0.5s; maybe if I read the analog port everytime I can make those switches respond in < 0.5s;

thanks again for your replies and

kind regards,

Jos (<--- back to my scribbling paper)

MAS3

The way you have drawn it up here, it looks like you are shorting the 5 volts through some resistor while trying to measure GND with an analog input and the interrupt pin.
That will never work.
Instead you can add another resistor between the analog pin and GND.
You'll create a voltage divider that way, and now you can actually read voltages using your analog input.

Retrolefty has offered you a gem, setting a fixed interrupt that you can use to acquire the analog value and any changes in that.
That interrupt doesn't need to be triggered by an external event, so it wil save you an input as well.
Of course you might have to take in consideration that this also uses some time.
So if your sketch has some dependance on timing, you should check that out.
Have a look at "blink without delay".
Did you connect the grounds ?
Je kunt hier ook in het Nederlands terecht: http://arduino.cc/forum/index.php/board,77.0.html

Magician


JosAH


The way you have drawn it up here, it looks like you are shorting the 5 volts through some resistor while trying to measure GND with an analog input and the interrupt pin.
That will never work.
Instead you can add another resistor between the analog pin and GND.
You'll create a voltage divider that way, and now you can actually read voltages using your analog input.


Golly, I can't even make a proper ASCII art picture ... that resistor was there on my scribbling paper; sorry for the confusion; and then it resembles the second circuit by Magician (see above). I don't understand how his first circuit works though. Maybe I take the route suggested by Retrolefty:


Retrolefty has offered you a gem, setting a fixed interrupt that you can use to acquire the analog value and any changes in that.
That interrupt doesn't need to be triggered by an external event, so it wil save you an input as well.
Of course you might have to take in consideration that this also uses some time.
So if your sketch has some dependance on timing, you should check that out.


I downloaded the code and see if I need it all; thanks for the suggestion (also to Retrolefty). My sketch performs 'commands' either read from USB or a WiFi antenna or from a file (I have an SD card attached); every pass through the loop() function performs at most one command; in between those switches need to be read ...

kind regards,

Jos

Go Up