// This sketch uses one analogue joystick to control a servo for X-Axis movement
// and Y-Axis movement. The X servo will move then stop and resume.
// The Y is momentary and mirror the joystick position.
#include <Servo.h>
const byte ledPin = 19;
const byte vertPin = 26;
const byte horzPin = 27;
const byte servoPinX = 18;
const byte servoPinY = 13;
Servo xServo;
Servo yServo;
int xPosition = 89;
int yPosition = 0;
void setup() {
// put your setup code here, to run once:
Serial1.begin(9600);
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW);
pinMode(vertPin, INPUT);
pinMode(horzPin, INPUT);
xServo.attach(servoPinX);
yServo.attach(servoPinY);
}
void loop() {
// put your main code here, to run repeatedly:
delay(1); // this speeds up the simulation
int xMove = constrain(map(analogRead(horzPin), 0, 1023, 0, 180), 0, 180);
//Serial1.println(xMove);
int yMove = constrain(map(analogRead(vertPin), 0, 1023, 0, 180), 0, 180);
//Serial1.println(yMove);
if (xMove > 89) {
digitalWrite(ledPin, HIGH);
xPosition = constrain(xPosition++, 0, 180);
delay (50);
} else if (xMove < 89) {
digitalWrite(ledPin, HIGH);
xPosition = constrain(xPosition--, 0, 180);
delay (50);
} else {
digitalWrite(ledPin, LOW);
}
//Serial1.println(xPosition);
xServo.write(xPosition);
yServo.write(yMove);
}