// INCLUDES
#include <Stepper.h>
// CONSTANTS
const int X_PIN = A0;
const int MOTOR_PIN_1 = 9;
const int MOTOR_PIN_2 = 10;
const int MOTOR_PIN_3 = 11;
const int STEP_PIN = 12;
const int DIR_PIN = 7;
const int STEPS_PER_REVOLUTION = 800;
// VARIABLES
int sensorVal, motorSpeed;
Stepper myStepper = Stepper(STEPS_PER_REVOLUTION, STEP_PIN, DIR_PIN);
void setup() { /* nothing to setup!! */}
void loop() {
sensorVal = analogRead(X_PIN); // read Joystick input
// set motor speed accordingly and step the motor
if(sensorVal < 470) {
motorSpeed = map(sensorVal, 0, 470, 60, 0);
myStepper.setSpeed(motorSpeed);
myStepper.step(1);
} else if(sensorVal > 550) {
motorSpeed = map(sensorVal, 550, 1023, 0, 60);
myStepper.setSpeed(motorSpeed);
myStepper.step(-1);
} else motorSpeed = 0;
}