Encoder + Motor Not working

Hello,

I have altered some example code from the encoder library yet it is not working. I'm justing trying to get some feedback from my motor encoder. Everything is hooked up correctly and the motors are moving, however, I never getting anything back on serial monitor except 0. Is there anything that is obviously wrong with my code? The only thing I can think of is that the two libraries are conflicting with each other.

Thanks!

/* Encoder Library - Basic Example
 * http://www.pjrc.com/teensy/td_libs_Encoder.html
 *
 * This example code is in the public domain.
 */

#include <Encoder.h>
#include <Servo.h>
Servo servo1;  // not a servo... it is a motor driver 
Servo servo2; // not a servo... it is a motor driver  (not need, I just put it here encase I had the encoder on the wrong motor) 

// Change these two 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 myEnc(23, 19);
//   avoid using pins with LEDs attached

void setup() {
  Serial.begin(9600);
  Serial.println("Basic Encoder Test:");
   servo1.attach(8);  // not a servo... it is a motor driver 
  servo2.attach(9);   // not a servo... it is a motor driver  (not need, I just put it here encase I had the encoder on the wrong motor)
}

long oldPosition  = -999;

void loop() {
  
 servo1.writeMicroseconds(1900);  // make motor 1 move 
servo2.writeMicroseconds(1900);  // make motor 2 move (not need, I just put it here encase I had the encoder on the wrong motor)

  long newPosition = myEnc.read();
  if (newPosition != oldPosition) {
    oldPosition = newPosition;
    Serial.println(newPosition);
  }
}

Thanks!

Well then the obvious thing to do is try each piece separately. First setup JUST the encoder and encoder library and try to get position data to go out the serial port. Then when you know THAT'S working, add in the other part.

Ok I ran several tests with two different Arduino Mega boards , and several encoders. I used the exact code that comes with the library and powered my motors separately. I also changed the ports to interrupt pins during some trials. Still no luck. I'm convinced that this is a software issue. I know that some older libraries need to be modified to work with the new Arduino software is the encoder library one of them? Is there any reason the the Mega might not work? Thanks for any help!

http://www.pjrc.com/teensy/td_libs_Encoder.html

/* Encoder Library - Basic Example
 * http://www.pjrc.com/teensy/td_libs_Encoder.html
 *
 * This example code is in the public domain.
 */

#include <Encoder.h>

// Change these two 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 myEnc(5, 6);
//   avoid using pins with LEDs attached

void setup() {
  Serial.begin(9600);
  Serial.println("Basic Encoder Test:");
}

long oldPosition  = -999;

void loop() {
  long newPosition = myEnc.read();
  if (newPosition != oldPosition) {
    oldPosition = newPosition;
    Serial.println(newPosition);
  }
}

Tested it with an Uno and it still did not work....

Did you read that whole page?

Pins 5 and 6 are not interrupt pins on the Mega or on the Uno. So, you are settling for the lowest performance.

Given that the encoder is then read only when read() is called, but that you call read() often enough, I can only question this:

Everything is hooked up correctly

The results that you are seeing suggests that this is not the case.

Ok. So it turns out that of my brand new encoders that I bought form parallax over 50% of them don't work. I finally found that buy unplugging them them from the Arduino and testing each one with my multimeter. I will be getting new ones (not from parallax).

Anyway I have a question with interrupts as far as the programing goes. From what I can tell when the state changes on one of the interrupts the Arduino breaks out of the current code and adds a count to the encoder. Will that affect the rest of a program?

Thanks!

Will that affect the rest of a program?

If it didn't, there would be no point in doing it, would there? What the impact would be depends on what the interrupt does, and what the rest of the program does.