Hi friends,
I want to use other pins except pins 2 and 3 on my Arduino for connecting an encoder. But when I rotate the encoder, it doesn't work properly and shows strange numbers on the serial monitor. One of my friends suggested the following code, but it didn't work either. I appreciate it if you could guide me on what to do.
Hello every one
I used an encoder, a motor driver, and a motor in the following code, which starts moving the motor by rotating the encoder.
Now I want to use another encoder as well. How can I define the second encoder in this code, considering that I cannot use pins 2 and 3 because encoder number one is connected to them?
The Encoder library will use 2, 1 or no interrupts. So you could use pin 2 and 4 (for instance) for 1 encoder and 3 and 7 (for instance) for the second encoder. That may be fast enough and still allow 2 encoders. If you don't need a lot of speed from the second encoder (human interface), use 2 non-interrupt pins and save the interrupts for the encoder that requires speed.
// define the input pins
#define pinA 19
#define pinB 20
#define pinP 21
// create an encoder object initialized with their pins
Encoder encoder( pinA, pinB, pinP );
// setup code
void setup()
{
// start the encoder time interrupts
EncoderInterrupt.begin( &encoder );
}
// loop code
void loop() {
// read the debounced value of the encoder button
bool pb = encoder.button();
// get the encoder variation since our last check, it can be positive or negative, or zero if the encoder didn't move
// only call this once per loop cicle, or at any time you want to know any incremental change
int delta = encoder.delta();
// add the delta value to the variable you are controlling
myEncoderControlledVariable += delta;
}
whatever questions you have about this demo-code just ask them.
Just make sure that your questions are specific to the code
Thank you so much! I've been reading a lot of articles and testing various codes for the past two days, but I couldn't find a solution at all. You have been a great help to me.
Another question I had is, what should I do if I want to use more than two encoders? I would appreciate it if you could also guide me on this matter.
I have no direct experience having used only 2 at once. If they are slow speed and can be used with regular digital you can have as many encoders as pins and time to read them.
If you need more interrupts, for fast encoders, see if there are encoder libraries that use pin-change interrupts or get a board with more hardware interrupts (Mega has 6).