#include <Stepper.h>
const int stepsPerRevolution = 200;
// initialize the stepper library on pins 9 through 12:
Stepper leftMotor(stepsPerRevolution, 9, 10, 11, 12);
Stepper rightMotor(stepsPerRevolution, 2, 3, 4, 5);
const int motorSpeed = 10;
float horizontal;
float vertical;
bool forward = false;
bool backward = false;
bool left = false;
bool right = false;
void joyStickRead() {
horizontal = analogRead(A0);
vertical = analogRead(A1);
//Serial.println(horizontal);
//Serial.println(vertical);
}
void horizontalMotorRead() {
//Left and Right Motor Application
if (horizontal > 512) {
right = false;
left = true;
} else if (horizontal < 512) {
right = true;
left = false;
} else if (horizontal == 512) {
right = false;
left = false;
}
}
void verticalMotorRead() {
//Up and Down Motor Application
if (vertical > 512) {
forward = true;
backward = false;
} else if (vertical < 512) {
forward = false;
backward = true;
} else if (vertical == 512) {
forward = false;
backward = false;
}
}
void horizontalMotorControl() {
if (right == true && left == false) {
leftMotor.step(-motorSpeed);
rightMotor.step(motorSpeed);
} else if (right == false && left == true) {
leftMotor.step(motorSpeed);
rightMotor.step(-motorSpeed);
} else if (right == false && left == false) {
leftMotor.step(0);
rightMotor.step(0);
} else {
Serial.println("Unknown Horizontal Input");
}
}
void verticalMotorControl() {
if (forward == true && backward == false) {
leftMotor.step(motorSpeed);
rightMotor.step(motorSpeed);
} else if (forward == false && backward == true) {
leftMotor.step(-motorSpeed);
rightMotor.step(-motorSpeed);
} else if (forward == false && backward == false) {
leftMotor.step(0);
rightMotor.step(0);
}
}
void setup() {
// set the speed at 100 rpm:
leftMotor.setSpeed(100);
rightMotor.setSpeed(100);
// initialize the serial port:
Serial.begin(9600);
}
void loop() {
bool killSwitch = false;
while (killSwitch == false) {
joyStickRead();
horizontalMotorRead();
verticalMotorRead();
horizontalMotorControl();
verticalMotorControl();
}
}