I'm new with arduino and I'm not good with coding, but I manage to make arduino work with my midi controller project using codes from internet and editing those.
Anyway major problem what I have is with rotary encoder. I have read every posts what I can find about encoders, but I just can't make it work. I can connect encoder to arduino, I have Mega 2560, but always it just work only one direction. Is there anybody who can help me with that?
This is just midi control code what I found, I have few kind of rotary encoders, and I have connect A and B wires on Arduino Mega 2560 digital pins 2 and 3 and C wire to ground. I have search tons codes for encoders but haven't found any working ones. Some of those give me multiple errors, and some of those don't give errors, but rotation not change at all. just rotate same direction doesn't matter which way I turn encoder.
Last one what I tried get working with midi code was this one
/* Encoder Library - TwoKnobs Example
* http://www.pjrc.com/teensy/td_libs_Encoder.html
*
* This example code is in the public domain.
*/
#include <Encoder.h>
// Change these pin numbers to the pins connected to your encoder.
// Best Performance: both pins have interrupt capability
// Good Performance: only the first pin has interrupt capability
// Low Performance: neither pin has interrupt capability
Encoder knobLeft(5, 6);
Encoder knobRight(7, 8);
// avoid using pins with LEDs attached
void setup() {
Serial.begin(9600);
Serial.println("TwoKnobs Encoder Test:");
}
long positionLeft = -999;
long positionRight = -999;
void loop() {
long newLeft, newRight;
newLeft = knobLeft.read();
newRight = knobRight.read();
if (newLeft != positionLeft || newRight != positionRight) {
Serial.print("Left = ");
Serial.print(newLeft);
Serial.print(", Right = ");
Serial.print(newRight);
Serial.println();
positionLeft = newLeft;
positionRight = newRight;
}
// if a character is sent from the serial monitor,
// reset both back to zero.
if (Serial.available()) {
Serial.read();
Serial.println("Reset both knobs to zero");
knobLeft.write(0);
knobRight.write(0);
}
}
But that give me '' positionLeft' was not declared in this scope'' error message...
Yes the problem is exactly that I need that rotary encoder for my Midi controller project, so I need to get those two sketches together. I have try many ways to do that, but because I'm very newbie with codes , I can't get it work...
Thank You for help , Code is right and I upload it to Arduino, but still encoder works only one direction. By one direction I mean it doesn't matter which way I turn the knob, the signal go only one direction. Midi is working and I can map that to my software but still same problem. tested with two encoders
I'll try that encoder prog tomorrow.. using interrups.
Which pins do u use for the encoders?
...
schematic for encoder connections?
I guess A and B to I/O- pins, center pin to GND, Pullups +/- 10 k
knut_ny:
I'll try that encoder prog tomorrow.. using interrups.
Which pins do u use for the encoders?
...
schematic for encoder connections?
I guess A and B to I/O- pins, center pin to GND, Pullups +/- 10 k
Yes. A and B goes to pins 2 and 3 or 18 and 19, I change those in code. Middle Pin (C) goes GND. I have tried with and without 10k resistors. If I understand right there is in arduino internal resistors for those pins?
yes. pullups are activated by library (+ /- 30k) Maybe enough.
Testrun: (without external pullups)
UNO : works "as in" on pin 5,6,7,8. works with 2,6 and 3,8(intr)
MEGA 1280: works "as in" on pin 5,6,7,8. works with 18,19 20,21 (all intr.pins)
the latter gave me some rubbish output also.. guess the 'midi-part' tries to tell me something
..........
hmmmm. ?!?
knut_ny:
yes. pullups are activated by library (+ /- 30k) Maybe enough.
Testrun: (without external pullups)
UNO : works "as in" on pin 5,6,7,8. works with 2,6 and 3,8(intr)
MEGA 1280: works "as in" on pin 5,6,7,8. works with 18,19 20,21 (all intr.pins)
the latter gave me some rubbish output also.. guess the 'midi-part' tries to tell me something
..........
hmmmm. ?!?
Also tested with another pins but no solution, makes me crazy with that encoder... it look like the encoder work's fine but only one direction no matter where I turn the knob. Direction not change even I change the wires from another pin to another.
pylon:
If it's working but just in one direction, usually one input pin is working while the other isn't.
Please post the wiring diagram or make a photo of your setup where all wiring is visible.
Are you able to check if both encoder signals are working with a scope?
Sorry can't make photo right now! will try to do that later...
Anyway Wiring is simple encoder have 3 legs in order ACB. Right Now A goes pin 5, B goes pin 6 and C goes ground. Both pins working but only in one direction... I have try also other pins but won't do difference..
Somehow I thinking that problem is in code code. You can see it earlier post by knut_ny. I just don't have so much experience with codes that I can see where is the problem...
What does that mean? What's the output combinations of the two pins if you turn the encoder? Have you really checked both pins to output a signal in that case?