#include <Servo.h>
int xPin = A0;
int yPin = A1;
int buttonPin = 9;
int xPosition = 0;
int yPosition = 0;
int buttonState = 0;
int servoPin = 6;
int pin = 13;
Servo servo;
void setup() {
Serial.begin(9600);
// initialize serial communications at 9600 bps:
pinMode(xPin, INPUT);
pinMode(yPin, INPUT);
pinMode(buttonPin, INPUT_PULLUP);
pinMode(pin,OUTPUT);
//activate pull-up resistor on the push-button pin
servo.attach(servoPin);
}
void loop() {
xPosition = analogRead(xPin);
yPosition = analogRead(yPin);
buttonState = digitalRead(buttonPin);
Serial.print("X: "); // prints to the Serial Monitor
Serial.print(xPosition);
Serial.print(" | Y: ");
Serial.print(yPosition);
Serial.print(" | Button: ");
Serial.println(buttonState); // returns to the next line
delay(100);
if (buttonState == 0) {
for (int i = 0; i <= 180; i++) {
servo.write(i);
delay(10);
}
}
if (buttonState == 1) {
servo.write(xPosition / 5.7);
}
if (yPosition < 80 ){
digitalWrite(13, HIGH);
delay(200); // Wait for 200 millisecond(s)
digitalWrite(13, LOW);
delay(200); // Wait for 200 millisecond(s)
}
if (yPosition > 800 ) {
digitalWrite(13, HIGH);
} else {
digitalWrite(13, LOW);
}
}