/* * Unipolar stepper motor speed and direction control with Arduino and joystick */ // include Arduino stepper motor library #include // define number of steps per revolution #define STEPS 32 // define stepper motor control pins #define IN1 11 #define IN2 10 #define IN3 9 #define IN4 8 // initialize stepper library Stepper stepper(STEPS, IN4, IN2, IN3, IN1); // joystick pot output is connected to Arduino A0 #define joystick A0 void setup() { } void loop() { int val = analogRead(joystick);// read analog value from the potentiometer if( (val > 500) && (val < 523) ) //stop the motor if the joystick is in the middle { digitalWrite(IN1, LOW); digitalWrite(IN2, LOW); digitalWrite(IN3, LOW); digitalWrite(IN4, LOW); } else { while (val >= 523) // move the motor in the first direction { // map the speed between 1 and 100 rpm int speed_ = map(val, 523, 1023, 1, 10); // set motor speed stepper.setSpeed(speed_); // move the motor (1 step) stepper.step(1); val = analogRead(joystick); } while (val <= 500) // move the motor in the other direction { // map the speed between 1 and 100 rpm int speed_ = map(val, 500, 0, 1, 10); // set motor speed stepper.setSpeed(speed_); // move the motor (1 step) stepper.step(-1); val = analogRead(joystick); } } }