Analog Feedback Servo Calibration, How do I get a value and record it somewhere?

So I am currently using a Servo motor with feedback analog feedback loop from pot to Arduino. I wanted to calibrate the values from pot to the angles 0 to 130 degrees.

I do not know if this is the most efficient way to do this. But as a beginner I have came up with code below;

MY PROBLEM: I want to catch the values of feedbackmin and feedbackmax from calibration and record them in the loop I have written with (map) function.

CODE:

// Include Arduino Servo Library
#include <Servo.h>

// Control and feedback pins
int servoPin = 9;
float feedbackPin = A0;

// Value from feedback signal
float feedbackValue;

// Calibration

float feedbackMin = ; // minimum feedback value
float feedbackMax = ; // maximum feedback value

// Create a servo object
Servo myservo;

void setup()
{
// Setup Serial Monitor
Serial.begin(9600);

// Attach myservo object to control pin
myservo.attach(servoPin);

// Home the servo motor
myservo.write(0);

// Step through servo positions
// Increment by 5 degrees
for (float servoPos = 0; servoPos <= 130; servoPos = servoPos + 1) {

// Position servo motor
myservo.write(servoPos * 1.1);
// Allow time to get there
delay(200);

// Read value from feedback signal
feedbackValue = analogRead(feedbackPin);

// Write value to serial monitor
Serial.print("Position = ");
Serial.print(servoPos);
Serial.print("\t");
Serial.println(feedbackValue);

}

// Print to serial monitor when done
Serial.println("Finished!");
float deg = 90 * 1.1;
myservo.write(deg);
delay(1000);

}

void loop()
{
Serial.println(map(analogRead(feedbackPin), feedbackMin, feedbackMax, 0, 130));

delay(500);

}

10-2_servo_cal22.ino (1.35 KB)

Welcome,

You can record min value with something like this

float feedbackMin = 99999; // initialize with a big value

...

if ( feedbackMin > feedbackValue )
{
    feedbackMin = feedbackValue;
}

Do almost the same thing for the max value

I want to record smallest value from feedbackpin to feedbackMin

and largest value from feedbackpin to feedbackmax.

this is after i run :

for (float servoPos = 0; servoPos <= 130; servoPos = servoPos + 1)
// Position servo motor
myservo.write(servoPos * 1.1);
// Allow time to get there
delay(200);

Servo.write() takes an integer NOT a float. What makes you think that adding 1 will "Increment by 5 degrees"? And why do you keep multiplying your pos value by 1.1?

For min and max can't you just write(0) and take the feedback value as min. Then write(130) and take the feedback value as max?

Steve

slipstick:
Servo.write() takes an integer NOT a float. What makes you think that adding 1 will "Increment by 5 degrees"? And why do you keep multiplying your pos value by 1.1?

For min and max can't you just write(0) and take the feedback value as min. Then write(130) and take the feedback value as max?

Steve

I have changed the increment to 1 but did not change comment, sorry.
I multiply by 1.1 because I have found that this gives more accurate values in degrees.
Thank you so much Steve I will try that.

It's good that you've discovered that the write() value doesn't give true angles. 0 just means go to the end stop in one direction and 180 means go to the far end stop. Depending on the servo that might be a total range of 90, 120, 180 or even 270 degrees. In rare case of a winch servo write(180) might move as much as 720 degrees. So you would need to recalibrate your 1.1 for different types of servo and possible different examples of the "same" servo (hobby servos are not exactly precision devices).

Steve

eengineer93:
I have changed the increment to 1 but did not change comment, sorry.
I multiply by 1.1 because I have found that this gives more accurate values in degrees.
Thank you so much Steve I will try that.

what would the code to write 0 and 180 look like? how do I take the value and record it?

Do you understand the code that you posted? It already writes various values to the servo. And it also uses analogRead(feedbackPin) to get the feedback value. A simple = assigns a value to a variable.

So something like

myservo.write(0);
feedbackMin = analogRead(feedbackPin);
myservo.write(180);       // or maybe 130? or 130 * 1.1?? Up to you!
feedbackMax = analogRead(feedbackPin);

Try putting it in setup() instead of the for loop.

Steve

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.