Encoder Issue.


I'm working on an electronics project, I'm using the Baron 4WD rover;

The encoders were included;

I'm using an Adruino Uno, with a 2AMP motor shield;

I've been trying to implement the encoders. I initially connect just my encoders onto my Adruino shield, and run the following code;

volatile unsigned long positionL = 0;  //vehicle position count from left encoder
const byte interruptPin = 3;

void setup() 
 Serial.begin(9600); //Starts a serial connection with the computer at a baud rate of 9600. (Don't need to worry about this, just how to do it)

void loop() 
 // Need to reset the encoder values to ensure they start on 0
 attachInterrupt(digitalPinToInterrupt(interruptPin), encoder2, RISING);
 // Specify an interrupt service routine to call when an interrupt occurs. Essentially when there is a change of signal on the interrupt pins (2 or 3), it will call the defined function
 // in our case that is encoder1 or encoder2.   
 while(1) //Will loop here forever.
   Serial.print(positionL); // This prints the current value of positionL in the serial monitor on the computer.
   Serial.print("\t"); // This creates a tab on the monitor 
   Serial.println(); // This creates a new line to print on

// When the interrupt pin is triggered it calls one of the following function. 
// All these do is increment our variables positionR or positionL by 1. 

void encoder2()


The code works, and I receive 20 pulses/rev as I manually rotate rover wheels.

When I stack the motor shield, with the front and back motor of each side connected to one port (in parallel), and manually rotate the rover wheel, the pulses are proportional to the speed I rotate the wheel with. As I increase my rotational speed, the pulses I receive jump up, and do not increment by 1.

I'm not sure why I'm having this problem. Could anyone be of help?

Please edit your post, to enclose the code in Code Tags </>. See "How to use this forum".

The interrupt(s) should be attached in setup(), not in loop(). Also remove the while(1) from loop(), it's already called repeatedly.

The Serial transmission will slow down loop(), so that it's normal to miss some encoder pulses in the log. But the ISR ensures that all pulses are counted properly.

You may want to detect both forward and backward movement of the robot, by inspecting both encoder signals (A/B) in the ISR.

While its natural to miss pulses, my pulses increment from 20 to 120 when I quickly jerk on the wheel, which is not supposed to happen.

Your other post.

Of course the signal lines should not catch noise. Review your circuits.