Reputation: 19
Im programming a Robotarm with DC Motors and try to read values for the motors with Potentiometers. To store multiple positions I save the values in arrays. Somehow reading the values of the Potentiometers dosn't work that well. The code is working fine sofar, there is no error message but the Serial Communication is giving back values that don't fit the potis. There is one Potentiometer that always shows the same value.
Here my code:
//Variablen definieren
byte read1 = 0;
byte read2 = 0;
byte read3 = 0;
byte read4 = 0;
byte read5 = 0;
int array1[9];
int array2[9];
int array3[9];
int array4[9];
int array5[9];
int cur[] = {0,0,0,0,0};
int tmp[] = {0,0,0,0,0};
int i = 0;
int j = 0;
int dif = 0;
byte k = 0;
byte l = 0;
// Poti Pins definieren
const int Poti1 = A1;
const int Poti2 = A2;
const int Poti3 = A3;
const int Poti4 = A4;
const int Poti5 = A5;
// Motoren Pins definieren
const int Motor []= {2,3,4,5,6,7,8,9,10,11};
// Knöpfe Pins defininieren
byte Knopftmp = 12;
byte Knopfarr = 13;
void setup(){
Serial.begin(19200); // Serial begin
// Pin Modes festlegen
pinMode(Poti1, INPUT);
pinMode(Poti2, INPUT);
pinMode(Poti3, INPUT);
pinMode(Poti4, INPUT);
pinMode(Poti5, INPUT);
pinMode(Motor[0], OUTPUT);
pinMode(Motor[1], OUTPUT);
pinMode(Motor[2], OUTPUT);
pinMode(Motor[3], OUTPUT);
pinMode(Motor[4], OUTPUT);
pinMode(Motor[5], OUTPUT);
pinMode(Motor[6], OUTPUT);
pinMode(Motor[7], OUTPUT);
pinMode(Motor[8], OUTPUT);
pinMode(Motor[9], OUTPUT);
pinMode(Knopftmp, INPUT_PULLUP);
pinMode(Knopfarr, INPUT_PULLUP);
}
void loop(){
// Werte auslesen
read1 = analogRead(Poti1);
delay(5);
read2 = analogRead(Poti2);
delay(5);
read3 = analogRead(Poti3);
delay(5);
read4 = analogRead(Poti4);
delay(5);
read5 = analogRead(Poti5);
// Sekunden berechnen
// Potivariable, 0, 100, 0, Millisekunden für eine volle/maximale Umdrehung
cur[0] = map(read1, 0, 1024, 0, 100)*10;
delay(5);
cur[1] = map(read2, 0, 1024, 0, 100)*10;
delay(5);
cur[2] = map(read3, 0, 1024, 0, 100)*10;
delay(5);
cur[3] = map(read4, 0, 1024, 0, 100)*10;
delay(5);
cur[4] = map(read5, 0, 1024, 0, 100)*10;
delay(5);
// Bei Knopfdruck die aktuell in tmp gespeicherte Position mit der cur Position abgleichen und bei Bedarf die Ärme in die neue Position fahren
if (digitalRead(Knopftmp) == LOW){ //Knopfabfrage
while (i <= 4){ // i als Variable mit der Motoren bestimmt werden
switch (i) // Cases passen die Pins an den Motor an
{
case 0:
k = 0;
l = 1;
break;
case 1:
k = 2;
l = 3;
break;
case 2:
k = 4;
l = 5;
break;
case 3:
k = 6;
l = 7;
break;
case 4:
k = 8;
l = 9;
break;
default:
break;
}
if (tmp[i] == cur[i]){} // bei keiner Änderung passiert nichts
else if (0 > cur[i] - tmp[i]){ // sollte der cur Wert geringer als der tmp Wert sein wird der Motor um die differenz zurück gedreht
digitalWrite(Motor[k], LOW);
digitalWrite(Motor[l], HIGH);
delay ((cur[i]-tmp[i])*(-1));
digitalWrite(Motor[k], LOW);
digitalWrite(Motor[l], LOW);
}
else{ // sollte der cur Wert größer als der tmp Wert sein wird der Motor um die differenz weiter gedreht
digitalWrite(Motor[k], HIGH);
digitalWrite(Motor[l], LOW);
delay ((cur[i]-tmp[i]));
digitalWrite(Motor[k], LOW);
digitalWrite(Motor[l], LOW);
}
tmp[i]=cur[i];
i++;
}
}
else{ // Zurücksetzen des Motorencounter
if (i == 5)
{
i = 0;
}
}
// Position in Arrayspeichern
if (digitalRead(Knopfarr) == LOW){
array1[j] = {tmp[1]};
array2[j] = {tmp[2]};
array3[j] = {tmp[3]};
array4[j] = {tmp[4]};
array5[j] = {tmp[5]};
Serial.print(j);Serial.println(":");
Serial.print(array1[0]); Serial.print(" "); Serial.print(array1[1]); Serial.print(" "); Serial.print(array1[2]); Serial.print(" "); Serial.print(array1[3]); Serial.print(" ");Serial.print(array1[4]);Serial.print(" ");Serial.print(array1[5]); Serial.print(" "); Serial.print(array1[6]); Serial.print(" "); Serial.print(array1[7]); Serial.print(" "); Serial.println(array1[8]);
Serial.print(array2[0]); Serial.print(" "); Serial.print(array2[1]); Serial.print(" "); Serial.print(array2[2]); Serial.print(" "); Serial.print(array2[3]); Serial.print(" ");Serial.print(array2[4]);Serial.print(" ");Serial.print(array2[5]); Serial.print(" "); Serial.print(array2[6]); Serial.print(" "); Serial.print(array2[7]); Serial.print(" "); Serial.println(array2[8]);
Serial.print(array3[0]); Serial.print(" "); Serial.print(array3[1]); Serial.print(" "); Serial.print(array3[2]); Serial.print(" "); Serial.print(array3[3]); Serial.print(" ");Serial.print(array3[4]);Serial.print(" ");Serial.print(array3[5]); Serial.print(" "); Serial.print(array3[6]); Serial.print(" "); Serial.print(array3[7]); Serial.print(" "); Serial.println(array3[8]);
Serial.print(array4[0]); Serial.print(" "); Serial.print(array4[1]); Serial.print(" "); Serial.print(array4[2]); Serial.print(" "); Serial.print(array4[3]); Serial.print(" ");Serial.print(array4[4]);Serial.print(" ");Serial.print(array4[5]); Serial.print(" "); Serial.print(array4[6]); Serial.print(" "); Serial.print(array4[7]); Serial.print(" "); Serial.println(array4[8]);
Serial.print(array5[0]); Serial.print(" "); Serial.print(array5[1]); Serial.print(" "); Serial.print(array5[2]); Serial.print(" "); Serial.print(array5[3]); Serial.print(" ");Serial.print(array5[4]);Serial.print(" ");Serial.print(array5[5]); Serial.print(" "); Serial.print(array5[6]); Serial.print(" "); Serial.print(array5[7]); Serial.print(" "); Serial.println(array5[8]);
Serial.println();
j++;
}
}
Upvotes: 0
Views: 533
Reputation: 7409
Comparing exact potentiometer values will lead to poor results. Read the Atmel datasheet for the specifications (linearity, accuracy) of the ADC pins. And the ADCs fluctuate even with a VERY steady input voltage... an oscilloscope will reveal this. You should design your code to react to the ADC values in ranges, perhaps with a little "dead-band" in the middle which means "do nothing." Remember, at 5V, 1024th of that is only few millivolts, so adding in the ADC inaccuracies, you should see that this is much like comparing float values to 0 without use of an epsilon. Unless it is EXACTLY zero, the comparison will fail.
Better parts often will not help, either. Even in designs where I've used expensive, 25-turn precision potentiometers, there's a tiny fluctuation in the ADC values that must be accounted for in your code.
Read the datasheet section on ADC Noise Cancellation Techniques. Datasheet and on ADC Accuracy.
Bottom line: Your results are at least partly software-related and likely partly electrical design related which of course is off-topic here (but you'll learn the limitations by reading the datasheet).
Upvotes: 1