Since arduino has only 2 pins that can use interrupts and I need 2 encoders but not simultaneously I thought of making a digital switch using NPN transistors.
The idea is to have both encoders connected on pins 2 and 3 and if pin 4 is HIGH then it would use encoder2 and if pin5 is HIGH, then encoder1.
I don't know much about electronics so please take a look at the diagram to see if this would work.
Encoder outputs 0.5V and And Vcc from arduino 5V pin, (also mentions outputing 20mA/channel)
So far I am using one encoder directly on pins 2 and 3 without any resistors, and have no problem
Probably would work, but wouldn't it add software overhead as you there needs to be a constant switching output to make both encoders avalible to the interupt pins? That kind of takes away some of the advantages of a interrupt driven change of state function.
I recently wired up a simple mechanical encoder to the two interrupt pins just to see if I could make it work. Used a library function from the sandbox and it worked good. I set the inputs to soft pull-ups and grounded the encorder common pin.
Anyway if I was trying to process two encoders using one common interrupt function, I would try it differently. However it would require 6 I/O pins in all and four diodes. I would wire encoder1 to pins 4 & 5 and encoder2 to pins 6 & 7. I would then wire a diode from pin 4 to pin 2, another from pin 5 to pin 3, another from pin 6 to pin 2 and a last one from pin 7 to pin 3. All diodes have their anode ends wired to either pins 2 or 3.
A change of state interrupt should be generated if either encoder has a change of state and the ISR can then read each encoders I/O pins, 4,5,6 & 7 to tell which or if both changed sense last interuppt cycle. Again encoder commons goes to ground and internal soft input pull-up resistors need to be turned on.
I only have one encode switch handy so I can't actually test this at this time. I keep meaning to find a old mouse and take it's optical encoders out for playing
EDIT: Actually on reflection the above won't work correctly. The using of wired ORed diodes does not satisfy the possible state that they encoders state pins can be in. An external change of state detector is required. I'll think about it.
I dont need constant switching output but only every several minutes, that is why I did consider this transistor thing.
Right now I am playing with blinking example and transistor with Base pin in Digital13 and LED connected on GND... the strange thing to me is that even if I just connect other LED leg to collector it acts as switch (nothing connected to emitter but works also with 5V on emitter)
. I tried it also reversing
Base - Digital 13
Emmiter - Led
Collector - Nothing
the other side of the LED is via resistor connected to GND
and this also works?!?!?!?
I did once design a solution that eliminated the problem of bounce altogether so that normal pins could be used but this used two IC chips per encoder. I haven't got it to hand but could probably reinvent it over the weekend if you are interested.
Alternatively just use analogue multiplexers to switch them over, details of that in the playground.
The best solution would be to make other 2 pins work with interrupts the same way as 2 and 3 :-?
But this multiplexers seem a very good thing, I did not know about them before. After reading about them I can not figure out why do you suggest using analog multiplexers over digital?
other 2 pins work with interrupts the same way as 2 and 3
Yes but that is how the microprocessor is, it's one of the things that can't be changed. You can't make any other pins trigger interrupts.
I can not figure out why do you suggest using analog multiplexers over digital
It's just that there is an article / examples about them and they are slightly easer to use because you don't have to bother about what direction the signals are going. If you want a digital multiplexer then a 74LS157 or 74LS158 would work just as well.
The best solution would be to make other 2 pins work with interrupts the same way as 2 and 3.
There might be a way to do that. The ATmega has something called pin change interrupts. They work per-port but a little software juggling could simulate an actual interrupt pin.
I might be able to make a chunk of code, or a library, to do this. Any interest?
The ATmega has something called pin change interrupts.
I do beg your pardon, you are correct, I have just had a look at the data sheet. You only get notification / vector per port but you can use any of the pins. That means if you want to trigger of two (or more) pins off one port they will go to the same ISR but that should not be a handycap in this case.
That would be great!
well the best would be if there is a way to detach 2 & 3 and attach 4 & 5 as interrupts during runtime on serial instruction,... or to have all 4 at once. I hope second version will not make encoder miss steps.
Hi,
I just posted an example in the playground for Pin Change interrupts. It allows you to associate a function with each individual pin, and have it called when the pin changes levels (only CHANGE supported). Take a look and tell me what you think. You can use it in place of, or in addition to attachInterrupt.