Hi,
I am starting to put together my first robot (Boe Bot from Parallax) My microcontroller board is an Arduino Mega(ATmega1280)
I am having problems with the wheel encoders (Boe Bot digital encoder kit from Parallax). The encoder signal lines are connected to Arduino pins 2 & 3.
My problem is that I get interrupts much faster than the slots of the wheels pass the encoders. Also interrupts often continue to come in after the wheels have stopped.Here is my code:
#include <Servo.h>
Servo servoRight;
Servo servoLeft;
volatile int Rindex = 0;
volatile int Lindex = 0;
void setup()
{
attachInterrupt(0,rtEncoderInterrupt,RISING);
attachInterrupt(1,ltEncoderInterrupt,RISING);
Serial.begin(9600);
servoRight.attach(12);
servoLeft.attach(11);
servoRight.writeMicroseconds(1700);
servoLeft.writeMicroseconds(1300);
}
void loop()
{
// stop the servos after 16 interrupts from either encoder
if((Rindex == 16) || (Lindex == 16))
{
servoRight.writeMicroseconds(1500);
servoLeft.writeMicroseconds(1500);
}
// hold execution here
while((Rindex >= 16)||(Lindex >= 16)){}
}
void rtEncoderInterrupt()
{
Serial.println(Rindex);
Rindex++;
}
void ltEncoderInterrupt()
{
Serial.print(" ");
Serial.println(Lindex);
Lindex++;
}
From this code I expected to see the Boe Bot wheels turn two reolutions (there are 8 slots in each wheel), and to see the following on the serial monitor:
1
1
2
2
3
etc.
Instead I saw the wheels turn 1/4 to 1/2 revolution at constant speed and this on the monitor:
1
2
3
1
4
2
3
5
6
etc.
the pattern being random. After one of the columns reached 16 ,and the motors were stopped, interrupts continued to occor sporadically.
I have been working at this little sketch for two days and I am at my wits end. Can anyone make a suggestion?