#include <Servo.h>
Servo servo1;
int previousangle1;
int dc;
void setup()
{
dc = 10; // Delay calibration
servo1.attach(11); // connect Servo to pin 11
pinMode(A0, INPUT); // Connect potentiometer or Joystick x or y pin to A0 input
previousangle1 = 90; // Initialize position to 90 degrees (180 degree servo)
}
void loop()
{
int val1 = analogRead(A0); // Read the potentiometer position
int angle1 = map(val1, 0, 1023, 0, 180); // Map the value to be used with servo
if(angle1>previousangle1)
{
for(int i=previousangle1; i<=angle1; i++)
{
servo1.write(i); //turn servo by 1 degrees
delay(dc); //delay for smoothness
}
}
// Fix: This needed to be an else if
// Original piece of code had it as a second stand alone if
else if(angle1<previousangle1)
{
for(int j=previousangle1; j>=angle1; j--)
{
servo1.write(j);
delay(dc);
}
}
delay(dc);
previousangle1 = angle1;
}
What you need to do now, is make similar code, with different variables for all 4 servos. I still have to check the addition of a capacitor to make it work even better and maybe some ferrite filters to eliminate noise.
Original code is taken from here: https://arduino.stackexchange.com/a/29970/76974
I will edit this, adding code for the rest of the servos later on.