Hello,
I would like to control the position (angle) of my servo motor. When I ask it to turn 90 degrees for example, the motor does it correctly but the feedback that appears is wrong. It shows me 45.48 degrees. What is wrong with my code ?
#include <Servo.h>
Servo myservo;
float servoAnaloglnPin=A1;
const int servoPin = 2;
const int servoAnalogOut = A0;
unsigned int servoValue0Deg;
unsigned int servoValue330Deg;
int voltage_of_servo;
float voltage;
float current;
float voltage_of_servo_1;
float voltage_of_servo_2;
int position_feedback_angle;
void setup() {
myservo.attach(servoPin);
Serial.begin(9600);
calibration();
}
void loop() {
if (Serial.available()){
int angle = Serial.parseInt();
if (Serial.read() == '\n'){
Serial.print("-Desired position (degree) : ");
Serial.println(angle);/
angle = map(angle, 0, 360, 0, 190); // Mapea el ángulo de 0-360 a 0-190
myservo.write(angle); // Mueve el servomotor al ángulo especificado
delay(150); // waits 15 ms for the servo to reach the position
voltage_of_servo = analogRead(servoAnaloglnPin);
Serial.print("Analog voltage: ");
Serial.println(voltage_of_servo);
//float voltage = map(voltage_of_servo, 0, 1023, servoValue0Deg, servoValue180Deg); // Rescale to potentiometer's voltage (from 0V to 8.4V): même chose que en bas
voltage = 7.4*(voltage_of_servo)/1023; //Ajout Ikram 1023 au lieu de servoValue330Deg
//Serial.print("Analog A0: ");
//Serial.println(analogRead(servoAnalogOut));
Serial.print(" -volatge (V):");
Serial.println(voltage);
current = voltage/1.6; //I=V/R, resister is 1.6 ohm
Serial.print(" -CURRENT (ohm):");
Serial.println(current);
delay(150);
float position_feedback_angle = ((voltage * 330)/ 7.4);
Serial.print("-Feedback position (degree): ");
Serial.println(position_feedback_angle);
}
}
}
void calibration() {
myservo.write(0); //set the servo to 0 position
delay(500); //wait for the servo to reach there
servoValue0Deg= analogRead(servoAnalogOut); // Pot value at 0 degrees
Serial.println("Pot value for 0 deg is " + String(servoValue0Deg)); // Print it! //
voltage_of_servo_1 = 7.4*(servoValue0Deg)/1023;
Serial.println(voltage_of_servo_1); //It does what it says
delay(1000); //fancy delay
myservo.write(180); //go to 180 degrees
delay(500); //wait for the servo to reach there
servoValue330Deg= analogRead(servoAnalogOut); //pot value at 180 deg
Serial.println("Pot value for 330 deg is " + String(servoValue330Deg));
voltage_of_servo_2 = 7.4*(servoValue330Deg)/1023;
Serial.println(voltage_of_servo_2); //It does what it says
Serial.println("Now going to 0 Degrees"); //It does what it says
myservo.write(0);// going to 90 degrees
delay(1000);// wait for it to reach there
}