Pages: 1 [2]   Go Down
Author Topic: Rotary encoders and interrupts  (Read 9269 times)
0 Members and 1 Guest are viewing this topic.
Swannanoa, New Zealand
Offline Offline
Full Member
***
Karma: 1
Posts: 202
New To Arduino (and C)
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Nick
The ones in NZ (and Aust) went 0 to 9.
Thats why we dialled 111 for an emergency, because it represented more pulses (9).

Apparently you guys use 000 for an emergency.

I do note the mindkits site has these portable phones (known as a Type 100)
http://www.mindkits.co.nz/store/communication/portable-rotary-phone-black

Kind of reminds me of the first cellphone we had while testing thr cell phone network (of one site) in chch way back in the 80's, although this is a bit smaller.



This link talks about dismantling one.
http://www.porticus.org/bell/telephones-technical_dials-rotary.html

Mark
Logged

North Yorkshire, UK
Offline Offline
Faraday Member
**
Karma: 104
Posts: 5531
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

In the UK we use 999
Logged

Global Moderator
Online Online
Brattain Member
*****
Karma: 480
Posts: 18720
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

We use 000 in Australia. As you can see from the picture it was exactly at the bottom of the phone, and you are less likely to dial it by tapping the cradle.

I have to say though, that the decision of various countries to use different systems hasn't helped people learn how to dial "emergency". We use 000, NZ 111, USA 911, UK 999. And then mobile phone companies tell users to dial 112.

Is this some ploy to confuse users? So they die?

You see this when we watch a video at school telling students how to "dial emergency" when the actors earnestly tell the kids if they see a fire to dial "911" and then the teacher has to stand up and say, "well, actually it is 000".
Logged


Swannanoa, New Zealand
Offline Offline
Full Member
***
Karma: 1
Posts: 202
New To Arduino (and C)
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Nick
You're not wrong.

In NZ they had to add the 911 capability, because we imported so much american produced programs...featuring real life emergencies ...
Our mobiles have 111 but also *555, you just have to remember to tell them where you are.

I'm not sure tapping the cradle was the issue, but more any line breaks (which amounts to the same).

That old phone reminds me of the first 'mobile phone' we had at the radio depot in telecom.
Back then we didn't have cellphones or pagers, so if you were on call you told them where you would be, and the phone number to call.

We used to install what was known as country sets, which were a radio link, that did everything include ring the bells on the normal phone.
Someone had the bright idea of putitng one in his car, and the other end on the local hills, so he had coverage all over christchurch.

Apparently he got funny looks while driving a Humber Super Snip and talking on this normal phone.
You should have seen the looks from the picnicers when it rung while we were at some park one weekend.

Many years later, we got pagers, then cellular phones.
mark

Logged

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

Hi Nick, I was really glad to find this tutorial because I am wanting to use 2 encoders on interrupts with my Uno.  You said :
Quote
This version only requires one interrupt, which frees up the other one for some other use (eg. a second rotary encoder). The pins are now defines so you can easily change where you connect the encoder.
  I copied that code only changing the baud rate to 9600 and with both wires connected (pins 2 & 3) it works perfectly but when I disconnect pin 3 it counts 0,1,0,1,0,1 etc.  I'm new at this and may be making a simple mistake, did I miss something?   smiley-confuse
Logged

Global Moderator
Online Online
Brattain Member
*****
Karma: 480
Posts: 18720
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Can you describe in more detail what you are doing? You have two encoders? And what pins are they attached to? Or are you trying with one?
Logged


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

Yes, I have 2 encoders for the project I’m working on but they are not both connected yet.  Each encoder has 4 wires (ground, 5volt, and two encoder wires which are blue and green).  With my Arduino Uno if I attach the ground to ground and the 5 volt line to the 5 volt pin and the blue wire to digital pin 2 and the green to digital pin 3 … using your code; when I turn the encoder the result counts as expected with no problems at all.  My assumption is that if I wish to use 2 encoders, instead of running them to digital pin 2 and 3, I would run one encoder to pin 2 and the other to pin 3.  Therefore, before connecting the second encoder, I am attempting to run the first encoder using only pin 2 (with ground and 5 volts still in place of course).  When I disconnect the wire going to pin 3 (no matter where I put it) the result is that the counter no longer counts up or down but alternates between the next number and the last number at each step of the encoder.  So if I make the switch when I’ve already counted up to 180, the result will be 181, 180, 181, 180 etc., and never go beyond 181.  If I restart the whole process the count is 0 then 1 then 0 then 1 etc. never going beyond 1.  Maybe I am using a different type encoder? or maybe I’m not connecting the disconnected wire (after removing from digital pin 3) in the right place? or maybe I need to modify the code in some way that I’m not seeing?  I could move to the Mega to take advantage of additional interrupts but if I can get this to work on the Uno, that would be my preference. 
Logged

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 361
Posts: 17292
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Yes you need to wire the disconnected wire for each encoder to two new digital input pins (non interrupt pins, say pins 4 and 5). Then in each of the ISR functions you need to add code to read that second encoder signal to determine the correct direction of rotation. The interrupt just tells you that the encoder has moved a step but only by reading the second encoder channel can you determine what direction that change was.

Lefty
Logged

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

Thanks Lefty for the hint and thanks Nick for all the rest....I've got it working!   Very happy.   smiley-grin
Logged

United Kingdom
Offline Offline
Tesla Member
***
Karma: 224
Posts: 6613
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

I never use interrupts with mechanical rotary encoders used as human input devices. Instead, I poll them at intervals of around 2ms, using either blink-without-delay style code if the sketch is simple enough, or a tick interrupt (you can generate this using the MsTimer2 library), or a scheduler. I have it working fine with 3 multiplexed encoders connected to an atmega328p. No hardware debouncing is needed. You can find the code at https://github.com/dc42/arduino.
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.

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 361
Posts: 17292
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I never use interrupts with mechanical rotary encoders used as human input devices. Instead, I poll them at intervals of around 2ms, using either blink-without-delay style code if the sketch is simple enough, or a tick interrupt (you can generate this using the MsTimer2 library), or a scheduler. I have it working fine with 3 multiplexed encoders connected to an atmega328p. No hardware debouncing is needed. You can find the code at https://github.com/dc42/arduino.

Good advice. My first stab at using encoders was with one of those cheap mechanical encoders and when used with interrupts the results were pretty poor due to bouncing contacts. Doing software debouncing with interrupts is pretty tricky and I didn't come across any sketch examples that worked well with my mechanical encoders. Only when I came across some $5 surplus magnetic encoders was I able to use interrupts in a satisfactory manner.

Lefty
Logged

Pages: 1 [2]   Go Up
Jump to: