reading feedback values from a servo motor

Hello everyone, i harve some servo motors in a project with a fourth wire witch is for feedback. When i move the servo with a joystick and analogRead the feedback pin. I was calibrate the values but when i see hows it goes in the serial monitor i see that it decrease or increase sometimes the values. For example :

90
91
92
95
45
100
101
102
130
131
110
111
112
150
120
etc..
do you know how can i fix that by increasing or decreasing steply?? thanks for your time

do you know how can i fix that by increasing or decreasing steply??

Can you give is more detail about what you want? What do you mean by "steply"? Do you mean that when the servo is still are the readings are noisy? Do the readings follow the servo?

You need to provide details of the hardware you are talking about. Otherwise we have much less chance of solving the problem than you have.

I have a robotic arm with 6Dof. I want to make a project with two senario. One is manual by moving the servo with a joystick and second is when i do that record and play back the motion. I have an external power supply for the motors, arduino adk mega powerd by usb from pc, one thumbstick joystick, a keypad and an sd card module for storing the feedback maped in to degrees values.

When i record the motion of the servo first problem is that the servo slow down his speed by far.(but the angle values record correctly).

when i read the txt from the sd i want to read it column by column for each servo (for example column 1 of values for motor 1 , column 2 for the motor 2...etc), and i dont know how to do it.

ill post you my code so i appreciate any help cause i cant find a solution in the internet (i dont want someone to solve my hole problem i want to advise me - guide me) thanks you very much for your time:

(motion1 is created to have the acceleration of the speed doesnt have any problem)

manual case the motion1 .. motion2 etc runs independently. when i pressed record button i run the motion function and the record function so i can run and record. but servo slows down his speed a lot.

void loop() {

char customKey = customKeypad.getKey();
if (customKey=='1') {
counter=1;}
if (counter==1){
motion1();
}
else{

servo1.detach();
}
if (customKey == '2') {
counter=2;}
if (counter==2){
motion2();

} else {
servo2.detach();
}

void motion1() {

servo1.attach(5);
value1 = analogRead(pin0);
value2 = analogRead(pin1);
feedback1 = map(value2, 90, 405, 0, 150);
//Serial.println(feedback);
value1 = map(value1, 0, 1023, 1, 29);
if (value1 <= 17 && value1 >= 13) {
value1 = 15;
}
forservo1 = forservo1 + (value1 - 15);

if (forservo1 < 1) {
forservo1 = 1;
}
if (forservo1 > 1023) {
forservo1 = 1023;
}

angle1 = map(forservo1, 1, 1023, 0, 150);
servo1.write(angle1);
delay(x);

}

void record() {

servo1.attach(5);
servo2.attach(6);
// servo3.attach(3);
// servo4.attach(4);
// servo5.attach(5);
// servo6.attach(6);
// make a string for assembling the data to log:
String dataString = "";

// Read pot value and append to the string
// Map to range of 0-180 for servo
//val = map(analogRead(analogPin), 0, 1023, 0, 180);
dataString += String(feedback1);
dataString += String('\t');
dataString += String(feedback2);
// dataString += String('\t');
// dataString += String(feedback3);
// dataString += String('\t');
// dataString += String(feedback4);
// dataString += String('\t');
// dataString += String(feedback5);
// dataString += String('\t');
// dataString += String(feedback6);
// Write to the servo
// Delay to allow servo to settle in position
// myservo.write(val);
// delay(15);
//

// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
File dataFile = SD.open("servopos.txt", FILE_WRITE);

// if the file is available, write to it:
if (dataFile) {
dataFile.println(dataString);
dataFile.close();
// print to the serial port too:
Serial.println(dataString);
}
// if the file isn't open, pop up an error:
else {
Serial.println("error opening servopos.txt");
}
}

void playback() {

servo1.attach(5);
servo2.attach(6);
// servo3.attach(3);
//servo4.attach(4);
// servo5.attach(5);
//servo6.attach(6);
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
File dataFile = SD.open("servopos.txt");

// If the file is available, read it
if (dataFile) {
while (dataFile.available()) {
// Write one line to buffer
buffer = dataFile.readStringUntil('\n');
// Print to serial monitor
// Serial.println(buffer);
// // Convert string to integer and position servo

long int x1=dataFile.parseInt();

long int x2=dataFile.parseInt();
// long int x3=dataFile.parseInt();
//
// long int x4=dataFile.parseInt();
// long int x5=dataFile.parseInt();
//
// long int x6=dataFile.parseInt();
Serial.print(x1);
Serial.print(" ");
Serial.print(x2);
//Serial.println(" ");
// Serial.print(x3);
// Serial.print(" ");
// Serial.print(x4);
// Serial.println(" ");
// Serial.print(x5);
// Serial.print(" ");
// Serial.print(x6);
// Serial.println(" ");
if (x1>0){
servo1.write(x1);
delay(10);
}
if (x2>0){
servo2.write(x2);
delay(10);
}
// if (x3>0){
// servo1.write(x3);
// delay(10);
// }
// if (x4>0){
// servo1.write(x4);
// delay(10);
// }
// if (x5>0){
// servo1.write(x5);
// delay(10);
// }
// if (x6>0){
// servo1.write(x6);
// delay(10);
// }

}
dataFile.close();
runOnce = 0;

}
// if the file isn't open, pop up an error:
else {
Serial.println("error opening servopos.txt");
}

}

a photo:

I'm thinking that it would be nice if the code were able to record the input value(s) to the servo(s) every 100 milliseconds or so, or whatever time interval is practical or feasible.

If the arduino is able to get the buffered data to sd-card or something, the that would be good.

Could probably work from there. One thing is ----- just how fast the servo is able to react or respond to a command. Like ...... after the command is given..... how long does it take for the servo's position to reach the commanded position.

It looks like you will need to determine the reason for those sudden high values ----- the ones that you see in the data at various times. It is unwanted noise.

One thing you could try is to get the arduino to take a bunch of measurements in quick succession ----- whatever duration is workable ----- and then get the arduino to take an average. This is so that any tall poppy gets put back in its place.

Or, after you have recorded your data, run your raw data through a processing routine that does a moving average. That could help smooth things out.