#include <Servo.h>
Servo myservo; // create servo object to control a servo
// twelve servo objects can be created on most boards
int pos = 90; // variable to store the servo position, strat at middle
// Arduino pin numbers
const int SW_pin = 2; // digital pin connected to switch output
const int X_pin = A5; // analog pin connected to X output
const int Y_pin = A4; // analog pin connected to Y output
void setup() {
// servo setup
Serial.begin(9600);
myservo.attach(11); // attaches the servo on pin 11 to the servo object
myservo.write(pos); // start at middle position
// joystick setup
pinMode(SW_pin, INPUT);
digitalWrite(SW_pin, HIGH);
// Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
if( analogRead(Y_pin) > 512 && pos < 180) {
// turning clockwise
++pos;
myservo.write(pos);
}
else if (analogRead(Y_pin) < 512 && pos > 0) {
// turning counter-clockwise
--pos;
myservo.write(pos);
}
delay(15); // control the speed of turning
}