#include <Servo.h>
Servo servo;
#define joystickBTN 4
int yPin = A0;
int xPin = A1;
int xValue;
int yValue;
int BTN;
// Define pin connections & motor's steps per revolution
const int dirPin = 2;
const int stepPin = 3;
const int stepsPerRevolution = 200;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(xPin, INPUT);
pinMode(yPin, INPUT);
pinMode(joystickBTN, INPUT_PULLUP);
// Declare pins as Outputs
pinMode(stepPin, OUTPUT);
pinMode(dirPin, OUTPUT);
servo.attach(5);
}
void loop() {
// put your main code here, to run repeatedly:
xValue = analogRead(xPin);
yValue = analogRead(yPin);
Serial.print("X Value: ");
Serial.print(xValue);
Serial.print(" Y Value: ");
Serial.println(yValue);
if(xValue == 512){
servo.write(90);
}
if(xValue >= 1023){
servo.write(0);
} else if(xValue < 512){
servo.write(180);
}
if(yValue >= 1023 ){
moveUp(200);
} else if(yValue < 512){
moveDown(200);
}
BTN = digitalRead(joystickBTN);
if(BTN == LOW){
moveUp(200);
}
}
void moveUp (int steps) {
for (int x = 0; x < steps; x++) {
digitalWrite(dirPin, HIGH);
delay(7);
digitalWrite(dirPin, LOW);
digitalWrite(stepPin, HIGH);
delay(7);
digitalWrite(stepPin, LOW);
delay(7);
}
}
void moveDown (int steps) {
for (int x = 0; x < steps; x++) {
digitalWrite(dirPin, LOW);
delay(7);
digitalWrite(dirPin, HIGH);
digitalWrite(stepPin, LOW);
delay(7);
digitalWrite(stepPin, HIGH);
delay(7);
}
}