So I have a linear actuator with a 10k pot for feedback. I am trying to isolate the source of error, how much is mechanical, how much is electrical, and how much is the Arduino etc. Now to see how long it takes from me sending a stop command to the feedback settling, I wrote a simple piece of code that reads extends the arm and tells me its position reading, looping as fast as it can. It then sends a stop command (directly turns off a relay) and stops feeding serial data and just collects feedback data every 10mS and saves it. After taking 20 readings it spits them out over serial, reverses direction and starts giving me data again. The data has a strange behavior. it stops as expected, and is perfectly stable during the 20 readings. when the next reading comes about 300mS later, it has dropped significantly. the next reading after this is slightly higher than the stable reading, and then starts dropping off as expected with the actuator retreating. Is reading the analog intput every 10 mS too often? am I violoating some obvious rule of proper analoge readings? I am going to add a capacitor to the input as close to the board as possible.
Below is my sketch and I will attach a plot. The blue spike is where the Relay was switched off, you can see the points get closer together on the plot since the readings are coming faster now. Any feedback would be welcome.
int MIN_LAC_POS = 110;
int MAX_LAC_POS = 330;
int LAC_MIN_USEFUL = 570;
int pwrRelay = 23; //pin: out for on
int dirRelay = 22; //pin: out for direction
int feedbackPin = 0; //pin: in for feedback //takes 100 micro seconds to read analog
int feedback;
void setup(){
Serial.begin(9600);
pinMode(pwrRelay, OUTPUT);
pinMode(dirRelay, OUTPUT);
digitalWrite(pwrRelay, HIGH); //low is on
digitalWrite(dirRelay, LOW); //low is extend by making red +??
}
void loop(){
boolean extend=true;
update();
while (extend && feedback<MAX_LAC_POS) {
digitalWrite(dirRelay, HIGH);
digitalWrite(pwrRelay, LOW);
Serial.print(millis());
Serial.print(" ");
Serial.print(extend);
Serial.print(" ");
Serial.print(feedback);
Serial.print(" ");
Serial.println(millis());
update();
}
int time[40];
int pos[40];
Serial.print("halt ");
Serial.println(millis());
digitalWrite(pwrRelay, HIGH);
for (int i=0; i<40; i++) {
time[i]=millis();
pos[i]=analogRead(feedbackPin);
delay(10);
}
Serial.println("saved info");
for (int i=0; i<40; i++) {
Serial.print(time[i]);
Serial.print(" ");
Serial.println(pos[i]);
}
while (feedback>MIN_LAC_POS) {
digitalWrite(dirRelay, LOW);
digitalWrite(pwrRelay, LOW);
Serial.print(millis());
Serial.print(" ");
Serial.print(extend);
Serial.print(" ");
Serial.print(feedback);
Serial.print(" ");
Serial.println(millis());
update();
}
}
int update(){
feedback = analogRead(feedbackPin);
return feedback;
}