#include <Servo.h>
//Servo objects created to control the servos
Servo myServo1;
Servo myServo2;
int servo1 = 3; //Digital PWM pin used by the servo 1
int servo2 = 5; //Digital PWM pin used by the servo 2
int initial_position1 = 90;
int initial_position2 = 90;
int joyX = A0; //Analog pin to which the joystick (X) is connected
int x_pos;
int joyY = A1; //Analog pin to which the joystick (Y) is connected
int y_pos;
void setup(){
Serial.begin (9600);
Serial.print(x_pos);
Serial.println(y_pos);
myServo1.attach(servo1);
myServo2.attach(servo2);
pinMode (joyX, INPUT);
pinMode (joyY, INPUT);
}
void loop()
{
x_pos = analogRead(joyX);
y_pos = analogRead(joyY);
Serial.print("x= ");
Serial.print(x_pos);
Serial.print(" ");
Serial.print("y= ");
Serial.println(y_pos);
delay(100);
int valX = analogRead(joyX); //Read the joystick X value (value between 0 and 1023)
int valY = analogRead(joyY); //Read the joystick Y value (value between 0 and 1023)
valX = map(valX, 0, 1023, 0, 180); //Scale the joystick X value to use it with the servo
valY = map(valY, 0, 1023, 0, 180); //Scale the joystick X value to use it with the servo
if (x_pos < 300)
{
if (initial_position1 < 10) { }
else
{
initial_position1 = initial_position1 - 10;
myServo1.write(initial_position1);
Serial.println(initial_position1);
delay (100);
}
}
if (x_pos > 700)
{
if (initial_position1 > 170){ }
else
{
initial_position1 = initial_position1 + 10;
myServo1.write(initial_position1);
Serial.println(initial_position1);
delay (100);
}
}
if (y_pos < 300)
{
if (initial_position2 < 10) { }
else
{
initial_position2 = initial_position2 -= 10;
myServo2.write(initial_position2);
Serial.println(initial_position2);
delay (100);
}
}
if (y_pos > 700)
{
if (initial_position2 > 170){ }
else
{
initial_position2 = initial_position2 + 10;
myServo2.write(initial_position2);
Serial.println(initial_position2);
delay (100);
}
}
}