#include <Servo.h> //Include library to work with Servo motor
Servo myServo;
//Define a variable to store the values of joystick in horizontal direction
const int X_pin = A0; //To read value in x-direction from joystick
const int Y_pin = A1; //To read value in y-direction from joystick
int position = 0; // Store value for Servo position.
void setup()
{
pinMode(X_pin, INPUT); //Set SW_pin for input mode
Serial.begin(9600); //Open serial port at 9600 baud
myServo.attach(9); //Attach the Servo on pin 9 to the Servo object
}
void loop()
{
//Read in 10-bit analog value (0 to 1023) from joystick
position = analogRead(X_pin); //position variable will convert to degrees
//Use map() to convert values to degrees (0 to 180) for Servo motor
position = map(position, 0, 1023, 0, 180);
//Use the converted value to move the Servo
myServo.write(position); //Move to a position specified by value (0 to 180)
delay(10); //Then wait 15ms
}//loop