# 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
digitalWrite(motorPin,HIGH); //move the motor
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
digitalWrite(motorPin,HIGH); //move the motor
``````

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...

``````//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
digitalWrite(motorPin,HIGH); //move the motor
digitalWrite(motorPin,LOW); //move the motor
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?

``````//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
digitalWrite(motorPin,HIGH); //move the motor
digitalWrite(motorPin,LOW); //move the motor
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