#include <Servo.h>
#define stepPin 13
#define dirPin 12
#define sw 11
#define vert A0
#define horz A1
int x=100;
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 joyX = 0; //Analog pin to which the joystick (X) is connected
int joyY = 1; //Analog pin to which the joystick (Y) is connected
void setup()
{
Serial.begin(9600);
pinMode(dirPin, OUTPUT);
pinMode(stepPin, OUTPUT);
pinMode(sw, INPUT_PULLUP);
myServo1.attach(servo1);
myServo2.attach(servo2);
}
void loop()
{
if(analogRead(vert)<512)
{
digitalWrite(dirPin, HIGH);
digitalWrite(stepPin, HIGH);
delay(x);
digitalWrite(stepPin, LOW);
delay(x);
}
if(analogRead(vert)>512)
{
digitalWrite(dirPin, LOW);
digitalWrite(stepPin, HIGH);
delay(x);
digitalWrite(stepPin, LOW);
delay(x);
}
if(analogRead(vert)==512)
{
digitalWrite(stepPin, HIGH);
digitalWrite(stepPin, HIGH);
}
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, 10, 170); //Scale the joystick X value to use it with the servo
valY = map(valY, 0, 1023, 10, 170); //Scale the joystick X value to use it with the servo
//Sets the servo position according to the scaled values.
myServo1.write(valX);
myServo2.write(valY);
delay(5);
/*
if(analogRead(horz)>100)
{
x=x+3;
delay(200);
if(x>100)
x=100;
Serial.println(x);
}
if(analogRead(horz)<100)
{
x=x-3;
delay(200);
if(x<2)
x=1;
Serial.println(x);
}
*/
}