// Controlling Micro Servos using Joystick
#include <VarSpeedServo.h>
// Create object to control the base servo
VarSpeedServo servoBase;
// Create object to control the tilt servo
VarSpeedServo servoInc;
#define PIN_X A0 // Pin connected to the X axis of the joystick
#define PIN_Y A1 // Pin connected to the Y axis of the joystick
#define PIN_SERVO_1 3 // Pin connected to servo 1
#define PIN_SERVO_2 5 // Pin connected to servo 2
int valX; // Set the X-axis value
int valY; // Set the value of the Y axis
void setup()
{
// Defines the port to be connected to the base servo
servoBase.attach(PIN_SERVO_1, 1, 180);
// Defines the port to be connected to the tilt servo
servoInc.attach(PIN_SERVO_2, 1, 180);
}
void loop()
{
// Receive the value of the joystick, X axis
valX = analogRead(PIN_X);
// Convert the read value to a value between 1 and 180 degrees
valX = map(valX, 0, 1023, 1, 180);
// Move the base servo to the position defined by the joystick
servoBase.slowmove(valX, 60);
// Receive the value of the joystick, Y axis
valY = analogRead(PIN_Y);
// Convert the read value to a value between 1 and 180 degrees
valY = map(valY, 0, 1023, 1, 180);
// Move the tilt servo to the position defined by the joystick
servoInc.slowmove(valY, 60);
// Wait for the servo to move and resumes reading
delay(30);
}