Hi,
I'm working on an electronics project, I'm using the Baron 4WD rover;
The encoders were included;
https://www.dfrobot.com/product-98.html
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
positionL=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()
{
positionL++;
}
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?