Calibrating a motor with attached potentiometer...

I am trying to come up with a function that can be used to determine the range of motion from a motor that has a potentiometer attached to it. The motor has a hard stop mechanism so it will only turn about 90 degrees (maybe less) before it stops. My goal is to learn the minimum and maximum values of the potentiometer without having to test them manually and without burning up the motors.

Here is the routine I cam up with (I have not tested this code):

//motor is controlled by an h-bridge...
byte motorLeft = 3;	//digital pin 3
byte motorRight = 4;	//digital pin 4
byte motorVRPin = 0; //analog pin 0

void setup() {
  Serial.begin(9600);
  int minMotor = calibrateMotor(motorLeft, motorVRPin);
  int maxMotor = calibrateMotor(motorRight, motorVRPin);
  Serial.print("Min:");
  Serial.println(minMotor);
  Serial.print("Max:");
  Serial.println(maxMotor);
}

void loop() {  
}

int calibrateMotor(byte motorPin, byte motorVR){
	digitalWrite(motorPin,HIGH); //move the motor
	int position1 = analogRead(motorVR); //read motor position
	digitalWrite(motorPin,HIGH); //move the motor
	int position2 = analogRead(motorVR); //read motor position
	int positionDiff = position1 - position2;
	if(abs(positionDiff)<10){
		//when the motor has reached the stopping point
		//it will bounce back a little once power is removed.
		return(position1); //found stopping point of motor
	} else {
		calibrateMotor(motorPin, motorVR); //stopping point not reached so move the motor some more.
	}
}

Is there a better or easier way to do this?

I really would not recommend recursion as a form of iteration on a memory-limited machine like an Arduino.

Can't you try a simpler "while" loop?

	digitalWrite(motorPin,HIGH); //move the motor
	int position1 = analogRead(motorVR); //read motor position
	digitalWrite(motorPin,HIGH); //move the motor
	int position2 = analogRead(motorVR); //read motor position

The second digitalWrite() accomplishes nothing. The pin was already HIGH.

Perhaps motorVR means something to you. It doesn’t mean anything to me.

 ]	if(abs(positionDiff)<10){
		//when the motor has reached the stopping point
		//it will bounce back a little once power is removed.
		return(position1); //found stopping point of motor
	} else {
		calibrateMotor(motorPin, motorVR); //stopping point not reached so move the motor some more.
	}

A recursive call is NOT what you want. A while loop might.

Personally, I think you need a whole different approach. Install two limit switches, and read them. When the left one is triggered, read the potentiometer. Then, make the motor go the other way, until the right switch is hit. When that happens, read the potentiometer again.

Bear in mind that reading the potentiometer is not a terribly accurate way of telling position later on.

Here is the routine I cam up with (I have not tested this code):

Why not? You would have known right off that it was flawed.

Sorry, motorVR is the pin the potentiometer is attached to. I can't use limit switches, because what i am doing is more of a "hack" and space is limited...

How about this:

//motor is controlled by an h-bridge...
byte motorLeft = 3; //digital pin 3
byte motorRight = 4;    //digital pin 4
byte motorVRPin = 0;         //analog pin 0

void setup() {
  Serial.begin(9600);
  int minMotor = calibrateMotor(motorLeft, motorVRPin);
  int maxMotor = calibrateMotor(motorRight, motorVRPin);
  Serial.print("Min:");
  Serial.println(minMotor);
  Serial.print("Max:");
  Serial.println(maxMotor);
}

void loop() {  
}

int calibrateMotor(byte motorPin, byte motorVR){
    int position1 = 0;
    int position2 = 0;
    int positionDiff = 100;
    while(abs(positionDiff)>10){
        digitalWrite(motorPin,HIGH); //move the motor
        digitalWrite(motorPin,LOW); //stop the motor
        position1 = analogRead(motorVR); //read motor position
        digitalWrite(motorPin,HIGH); //move the motor
        digitalWrite(motorPin,LOW); //move the motor
        position2 = analogRead(motorVR); //read motor position
        positionDiff = position1 - position2;
        //when the motor has reached the stopping point it will bounce back a little once power is removed.
        calibrateMotor(motorPin, motorVR); //stopping point not reached so move the motor some more.
    }
    return(position1); //found stopping point of motor
}

That's still recursive, isn't it?

Yes...my bad...

//motor is controlled by an h-bridge...
byte motorLeft = 3; //digital pin 3
byte motorRight = 4;    //digital pin 4
byte motorVRPin = 0; //analog pin 0

void setup() {
  Serial.begin(9600);
  int minMotor = calibrateMotor(motorLeft, motorVRPin);
  int maxMotor = calibrateMotor(motorRight, motorVRPin);
  Serial.print("Min:");
  Serial.println(minMotor);
  Serial.print("Max:");
  Serial.println(maxMotor);
}

void loop() {  
}

int calibrateMotor(byte motorPin, byte motorVR){
    int position1 = 0;
    int position2 = 0;
    int positionDiff = 100;
    while(abs(positionDiff)>10){
        digitalWrite(motorPin,HIGH); //move the motor
        digitalWrite(motorPin,LOW); //stop the motor
        position1 = analogRead(motorVR); //read motor position
        digitalWrite(motorPin,HIGH); //move the motor
        digitalWrite(motorPin,LOW); //move the motor
        position2 = analogRead(motorVR); //read motor position
        positionDiff = position1 - position2;
    }
    return(position1); //found stopping point of motor
}
        digitalWrite(motorPin,HIGH); //move the motor
        digitalWrite(motorPin,LOW); //stop the motor

What kind of motor is this driving? How far is the motor going to move in the few nanoseconds between these statements?

        digitalWrite(motorPin,HIGH); //move the motor
        digitalWrite(motorPin,LOW); //stop the motor
        position1 = analogRead(motorVR); //read motor position
        digitalWrite(motorPin,HIGH); //move the motor
        digitalWrite(motorPin,LOW); //move the motor
        position2 = analogRead(motorVR); //read motor position

How much are position1 and position2 going to differ after running the motor for a few nanoseconds?

The motor is a regular DC motor with a gearbox assembly. I may need to add a delay between the HIGH and LOW digitalWrites to the motor, but I don't have the hardware handy right now to test, so I am only guessing what the outcome will be.

Here is a picture of two of the motor assemblies (FYI.. This is a WowWee Alive Elvis)