#include <Servo.h> // add servo library
Servo myservo; // create servo object to control a servo
int jPin = A0; // analog pin used to connect the joystick (INPUT)
int servoPin = 9; // pwm pin used to connect the servo (OUTPUT)
int servoVal;
void setup() {
myservo.attach(servoPin); // attaches the servo on pin 9 to the servo object
Serial.begin(9600);
}
void loop() {
int joystickXVal = analogRead(jPin); //read joystick input on pin A1
if(joystickXVal >= 500 && joystickXVal <= 524){
servoVal = 90;
myservo.write(servoVal);
}
else if(joystickXVal < 500){
servoVal = map(joystickXVal, 499, 0, 91, 180);
myservo.write(servoVal);
}
else{
servoVal = map(joystickXVal, 524, 1023, 90, 0);
myservo.write(servoVal);
}
delay(20);
Serial.print(joystickXVal); //print the value from A1
Serial.print(" = input from joystick"); //print "=input from joystick" next to the value
Serial.print(" ; ");
Serial.print(servoVal);
Serial.println(" = output to servo"); //print "=output to servo" next to the value
}