Rotary Encoder

Hello everyone,

I'm fairly new to Arduino. I recently figured out how to make pushbuttons work,
and when it comes to complicated parts like rotary encoders, I'm completely lost.

So I tried to walk the easy path and searched for a code on the net.

http://pastebin.com/nxib6tTq (code can also be found in the attachments)

Since i use a Teensy 2.0, I changed the code to this:

#define DELAY            5  // Delay per loop in ms
 enum PinAssignments 
 {
  encoderPinA = 5,
  encoderPinB = 6,
  encoderPinC = 7,
  encoderPinD = 8,
 };
//This is up to your pin wiring

I really hope I did the right thing there because if the code was wrong this would be the wrong section.

I tried to wire the encoder like this.

And it doesn't respond, at all.

Where did I go wrong?

code.ino (1.7 KB)

Here is a basic tutorial that I put together recently. Maybe not so much a tutorial as commented code: it might help you understand. There are schematics, too.

It doesn't use interrupts: I have no idea how fast you can spin the knob before it misses counts.

It's written for an encoder with 20 pulses per revolution: there's a variable in there that you can change.

You set the pin numbers alright I see that. But I don't see any code there that tries to read the encoder or the interrupts or anything else. Do you think that just telling the compiler to use certain pin numbers will cause it to assume you have an encoder and automagically figure out how to read it for you and what to do with the pulses? You have to write that code.

This is just a part of the code, I linked the full code in the original post.

Delta_G:
Sorry man, very few people are going to go download your code from pastebin. We get hit by viruses that way. You can attach the file to your post or you can insert it in a code box if it is short enough.

My bad, I was unaware.

Delta_G:
No problem. Just go up and modify your post and put the code as an attachment. Or put it in a reply. Really anywhere but pastebin.

Done

Delta_G:
Oh, it's a Teensy 2.0. I guess I could go back to the OP.

Well in that case you need pins 5,6,7,8. You have it right in the post but in the code you have 0,1,2,3.

I left the attached code unchanged, the one uploaded to my board has the correct pins.

Delta_G:
You need to make all the variables that get changed in an interrupt volatile.

eg.

volatile boolean a_set = false;

Like this?

 int encoderPos[] = {0,0};
 static boolean rotating[] = {false,false};
 
 volatile boolean A_set = false;              
 volatile boolean B_set = false;
 volatile boolean C_set = false;              
 volatile boolean D_set = false;

Delta_G:
Yes, and encoderPos and Rotating and any anything that could potentially be modified inside that ISR.

It looks like this now but it still doesn't work.

 volatile int encoderPos[] = {0,0};
 volatile boolean rotating[] = {false,false};
 
 volatile boolean A_set = false;              
 volatile boolean B_set = false;
 volatile boolean C_set = false;              
 volatile boolean D_set = false;

I have a feeling, I broke the INT pins.
What is the easiest way to check if your pin still works?

Delta_G:
Make a short sketch that just increments a counter in the ISR and hook it up to a simple pushbutton.

Okey the pins still work, so I'm still missing something in the script.

I attached the code in its current form.

sketch_aug29a.ino (1.75 KB)

Delta_G:
Throw some Serial.print lines in the main loop and print out encoderPos and see if it is changing at all.

Not sure if this is what you wanted me to do, but I ended up with this error.

  This report would have more information with
  "Show verbose output during compilation"
  enabled in File > Preferences.
Arduino: 1.0.5-r2 (Windows 7), Board: "Teensy 2.0"
sketch_aug29a.ino: In function 'void loop()':
sketch_aug29a:39: error: call of overloaded 'print(volatile int [2])' is ambiguous
C:\Program Files (x86)\Arduino\hardware\teensy\cores\teensy/Print.h:47: note: candidates are: size_t Print::print(const String&) <near match>
C:\Program Files (x86)\Arduino\hardware\teensy\cores\teensy/Print.h:48: note:                 size_t Print::print(char) <near match>
C:\Program Files (x86)\Arduino\hardware\teensy\cores\teensy/Print.h:52: note:                 size_t Print::print(uint8_t) <near match>
C:\Program Files (x86)\Arduino\hardware\teensy\cores\teensy/Print.h:53: note:                 size_t Print::print(int) <near match>
C:\Program Files (x86)\Arduino\hardware\teensy\cores\teensy/Print.h:54: note:                 size_t Print::print(unsigned int) <near match>
C:\Program Files (x86)\Arduino\hardware\teensy\cores\teensy/Print.h:55: note:                 size_t Print::print(long int) <near match>
C:\Program Files (x86)\Arduino\hardware\teensy\cores\teensy/Print.h:56: note:                 size_t Print::print(long unsigned int) <near match>

Hi,
Get rid of the curly { } brackets around both Serial.print lines. They are not needed.

Tom...... :slight_smile:

excd:

Delta_G:
No problem. Just go up and modify your post and put the code as an attachment. Or put it in a reply. Really anywhere but pastebin.

Done

Actually, it is a lot easier if it can be included in the posting itself, if it is not too long. The ".ino" file has to be put in a correspondingly-named subdirectory and loaded in the IDE etc. etc.


What's all this about interrupts? What sort of encoder is this? Why do you want to use interrupts?

Delta_G:

Serial.println(encoderPos[0]);

Serial.println(encoderPos[1]);

I replaced the old line with this, but the serial monitor doesn't show anything at all.

Tried it with USB type: Serial + Keyboard + Mouse + Joystick and USB type: Serial

Paul__B:

excd:

Delta_G:
No problem. Just go up and modify your post and put the code as an attachment. Or put it in a reply. Really anywhere but pastebin.

Done

Actually, it is a lot easier if it can be included in the posting itself, if it is not too long. The ".ino" file has to be put in a correspondingly-named subdirectory and loaded in the IDE etc. etc.


What's all this about interrupts? What sort of encoder is this? Why do you want to use interrupts?

As I'm not sure how long too long is, I figured it would be better not to include the code in my post.

Have you guys given up on me,?

Since it's obviously the code which isn't working,
should I make a new thread in the Programming Questions forum?

Hi, have you looked here and tried the suggested sketches and libraries?

http://playground.arduino.cc/Main/RotaryEncoders

Tom.... :slight_smile:

Is the rotary encoder you are using a quadrature encoder? If it is this is a pretty hard project to cut your teeth on. Do you have code that can decode the speed and direction of rotation?

excd:
Have you guys given up on me,?

You haven't mentioned if you looked at the link I gave in Reply #2 and restated here.

My code there doesn't use interrupts, and interrupts are notoriously tricky for new-comers to get their heads round. Most of the time they're not needed anyway, it seems.

See if you can work with my code to get a feel for how your encoder actually works.

First principles are always a good way to start, which is why I wrote that code for myself.

excd:
Have you guys given up on me,?

That poses an interesting question. Who has given up on who?

excd:
As I'm not sure how long too long is, I figured it would be better not to include the code in my post.

That is the wrong way round. You put the code in "code" tags using the "#" icon in a post - the rest of which post need be no more than "This is the current code:" and either it posts, or it tells you it is too long. Only in the latter case do you then attach an inconvenient ".ino" file. I do suspect 1.75k will fit in a posting quite comfortably.

I asked for another two explanations which are crucial to the proper way to approach the project, without which information it is simply not worth anyone spending time trying to guess the proper answer to your questions; whether to tidy up your code or scrap it in favour of a proper approach.

We see not a few rants here where people cannot understand why others will not give them the answer they want.

JimboZA:

excd:
Have you guys given up on me,?

You haven't mentioned if you looked at the link I gave in Reply #2 and restated here.

My code there doesn't use interrupts, and interrupts are notoriously tricky for new-comers to get their heads round. Most of the time they're not needed anyway, it seems.

See if you can work with my code to get a feel for how your encoder actually works.

First principles are always a good way to start, which is why I wrote that code for myself.

I tried it but I dont get anything on the serial monitor either, the leds however seem to react properly.