Navigating a Maze Using Odometry

hello
I’ve got an issue with initiating two DC motors for a maze following exercise. I’ve got the odometry working but my motors will not initiate. I’m using a Sparkfun motor shield with the encoder inputs on pins 7 and 8. If you see where I’m screwing up the motor initiation, please let me know.

Thanks!

Below is my code:

*/
#include “PinChangeInt.h”

#define EncoderCountsPerRev (12.0)
#define DistancePerRev (22.3)
// these next two are the digital pins we’ll use for the encoders
#define EncoderMotorLeft 7
#define EncoderMotorRight 8
#define GAINleftMotor (1)
#define GAINrightMotor (1)
#define LEFTmotor 1
#define RIGHTmotor 2
// here we define the pwm and direction for Right Motor (A) and Left Motor (B)
#define motorRightPWM 3
#define motorRightDirection 12
#define motorLeftPWM 11
#define motorLeftDirection 13
int buttonPush;
int buttonPin;
///////////////////////////////////
volatile unsigned int leftEncoderCount = 0;
volatile unsigned int rightEncoderCount = 0;

void setup(){
// set stuff up

Serial.print("Encoder Testing Program ");
Serial.print("Now setting up the Left Encoder: Pin ");
Serial.print(EncoderMotorLeft);
Serial.println();
pinMode(EncoderMotorLeft, INPUT); //set the pin to input
digitalWrite(EncoderMotorLeft, HIGH); //use the internal pullup resistor
// this next line setup the PinChange Interrupt
PCintPort::attachInterrupt(EncoderMotorLeft, indexLeftEncoderCount,CHANGE);
// if you “really” want to know what’s going on read the PinChange.h file :slight_smile:
/////////////////////////////////////////////////
Serial.print("Now setting up the Right Encoder: Pin ");
Serial.print(EncoderMotorRight);
Serial.println();
pinMode(EncoderMotorRight, INPUT); //set the pin to input
digitalWrite(EncoderMotorRight, HIGH); //use the internal pullup resistor
//we could have set this up using just "pinMode(EncoderMotorRight, INPUT_PULLUP);
PCintPort::attachInterrupt(EncoderMotorLeft, indexRightEncoderCount,CHANGE);
buttonPin = 2;
pinMode(buttonPin, INPUT_PULLUP);
Serial.begin(9600);
} /////////////// end of setup ////////////////////////////////////

/////////////////////// loop() ////////////////////////////////////
void loop()
{
float distance; // how far we want to go

distance = 9000;
drive(distance);
exit(0);
}
//////////////////////////////// end of loop() /////////////////////////////////

////////////////////////////////////////////////////////////////////////////////
int drive(float distance)
{
int countsDesired, countsCompleted;
int CMDleft, CMDright;
int countLeft = 0;
int countRight = 0;
int errorLeft, errorRight;
int power;

countsDesired = (int) ((distance / DistancePerRev) * EncoderCountsPerRev);
leftEncoderCount = 0;
rightEncoderCount = 0;
// we make the errors non-zero so our first test gets us into the loop
errorLeft = 1;
errorRight = 1;
while(errorLeft || errorRight)
{
CMDleft = computeCommand(GAINleftMotor, errorLeft);
CMDright = computeCommand(GAINrightMotor, errorRight);
motor(motorLeftPWM, CMDleft);
motor(motorRightPWM, CMDright);
countLeft = leftEncoderCount;
countRight = rightEncoderCount;
errorLeft = countsDesired - countLeft;
errorRight = countsDesired - countRight;
// need to convert to Arduino Speak
// printf(“eL= %d, eR= %d, cL=%d, cR=%d\n”,errorLeft, errorRight, cmdLeft, cmdRight);
}

}

int computeCommand(int gain, int error)
// gain, and error, both are integer values
{
int cmdDir = 0;
//
// since the motor command limits at ± 255 we limit our command to that range.
//
cmdDir = (gain * error);
if (cmdDir > 255)
cmdDir = 255;
else
if (cmdDir < -255)
cmdDir = -255;
return(cmdDir);
}

void motor(int m, int pwm){
Serial.print(“entering motor”);
Serial.print(m);
Serial.print("\t");
Serial.println(pwm);

if(m == LEFTmotor){

if(pwm > 0){
digitalWrite(motorLeftDirection, HIGH);
analogWrite(motorLeftPWM, pwm);
}
else if(pwm < 0){
digitalWrite(motorLeftDirection, LOW);
analogWrite(motorLeftPWM, -pwm);
}
else{
analogWrite(motorLeftPWM, 0);
}
}
/////////////////////////// end of left motor //////////////
/////// now right motor //////////////
if(m == RIGHTmotor){
if(pwm > 0){
digitalWrite(motorRightDirection, HIGH);
analogWrite(motorRightPWM, pwm);
}
else if(pwm < 0){
digitalWrite(motorRightDirection, LOW);
analogWrite(motorRightPWM, -pwm);
}
else {
analogWrite(motorRightPWM, 0);
}
}////////////////////////////// end of right motor //////////////
}

/////////////////////////////////
//////////////////////////////////////////////////////////
void indexLeftEncoderCount()
{
leftEncoderCount++;
}
//////////////////////////////////////////////////////////
void indexRightEncoderCount()
{
rightEncoderCount++;
}

Please read the "How to use this forum" post and post your code properly. Also describe the problem more clearly. "will not initiate" is meaningless.