When I tried it out I found a couple of faults in the code in the previous post. I needed to increase the dead band for the potentiometers and I needed to put a short delay() into loop() so the servos wouldn't move too fast. Yes, I know that's a crude solution, but it is just demo code.
I also decided I should put limits on the min and max angles for the servos. And then I got lazy and decided to put all the items into arrays which allowed me to shorten the code further at the expense of a little less clarity.
Interestingly as I was only testing with a single pot and a single servo I discovered I had to tie the 3 unused analog pins to ground or else they all read the current value of the 4th pin.
This is the working code. I am leaving the previous post unchanged so as not to confuse anyone who has already read it.
#include <Servo.h>
#define mainArm 0
#define jib 1
#define bucket 2
#define slew 3
Servo servo[4];
byte angle[4] = {90, 90, 90, 90};
byte potPin[4] = {A0, A1, A2, A3};
byte servoPin[4] = {12, 11, 10, 9};
void setup() {
Serial.begin(9600);
Serial.println("Starting DiggerCode.ino");
for (byte n = 0; n < 4; n++) {
servo[n].attach(servoPin[n]);
}
}
void loop() {
readPotentiometers();
moveServos();
delay(10);
}
void readPotentiometers() {
int potVal;
for (byte n = 0; n < 4; n++) {
potVal = analogRead(potPin[n]);
if (potVal < 450) {
angle[n] += 1;
if (angle[n] > 170) {
angle[n] = 170;
}
}
if (potVal > 570) {
angle[n] -= 1;
if (angle[n] < 10) {
angle[n] = 10;
}
}
}
}
void moveServos() {
for (byte n = 0; n < 4; n++) {
servo[n].write(angle[n]);
}
}
...R