/*
* Created by ArduinoGetStarted.com
*
* This example code is in the public domain
*
* Tutorial page: https://arduinogetstarted.com/tutorials/arduino-joystick-servo-motor
*/
#include <Servo.h>
#define VRX_PIN A0 // Arduino pin connected to VRX pin
#define VRY_PIN A1 // Arduino pin connected to VRY pin
#define SERVO_X_PIN 2 // Arduino pin connected to Servo motor 1
#define SERVO_Y_PIN 3 // Arduino pin connected to Servo motor 2
Servo xServo; // create servo object to control a servo 1
Servo yServo; // create servo object to control a servo 2
const int BUTTON_PIN = 7; // Arduino pin connected to button's pin
const int RELAY_PIN = 5; // Arduino pin connected to relay's pin
void setup() {
Serial.begin(9600) ;
xServo.attach(SERVO_X_PIN);
yServo.attach(SERVO_Y_PIN);
pinMode(BUTTON_PIN, INPUT_PULLUP); // set arduino pin to input pull-up mode
pinMode(RELAY_PIN, OUTPUT); // set arduino pin to output mode
}
void loop() {
// read analog X and Y analog values
int xValue = analogRead(VRX_PIN);
int yValue = analogRead(VRY_PIN);
int xAngle = map(xValue, 0, 1023, 0, 180); // scale it to the servo's angle (0 to 180)
int yAngle = map(yValue, 0, 1023, 0, 180); // scale it to the servo's angle (0 to 180)
xServo.write(xAngle); // rotate servo motor 1
yServo.write(yAngle); // rotate servo motor 2
// print data to Serial Monitor on Arduino IDE
int buttonState = digitalRead(BUTTON_PIN); // read new state
if (buttonState == LOW) {
Serial.println("The button is being pressed");
digitalWrite(RELAY_PIN, HIGH); // turn on
}
else
if (buttonState == HIGH) {
Serial.println("The button is unpressed");
digitalWrite(RELAY_PIN, LOW); // turn off
}
}