I’m using an A4988 driver and NEMA 17 stepper. I’d like to use a joystick to rotate the stepper clockwise when the joystick is pushed forward and counterclockwise when the joystick is pushed backwards. I don’t know anything about this driver but my current code is not moving the stepper.
#include <Stepper.h>
// defines pins numbers
const int stepPin = 3;
const int dirPin = 4;
int joyValue = 0;
int joyPin = A2;
int joyCenter = 500; // neutral value of joystick
int joyDeadRange = 150; // movements by this much either side of center are ignored
void setup() {
Serial.begin(9600);
// Sets the two pins as Outputs
pinMode(stepPin,OUTPUT);
pinMode(dirPin,OUTPUT);
}
void loop() {
moveStepper();
showPosition();
}
void moveStepper() {
joyValue = analogRead(joyPin);
if (joyValue > joyCenter + joyDeadRange) {
digitalWrite(dirPin,HIGH);
digitalWrite(stepPin,HIGH);
}
if (joyValue < joyCenter - joyDeadRange) {
digitalWrite(dirPin,LOW);
digitalWrite(stepPin,HIGH);
}
}
void showPosition() {
Serial.print("JoyValue ");
Serial.print(joyValue);
}
It appears you only set the step pin high and then leave it there. The step input requires a string of pulses equal to the number of steps you want the motor to rotate, i.e. 200 steps = 1 rev if the stepper motor has a 1.8 degree/step resolution.
The code in this link has the type of response to the joystick that you require. It is written to work with a servo but could be adapted for a stepper motor.
The standard Stepper motor library is not intended for use with a driver like the A4988 that takes step and direction signals. Try the AccelStepper library or just roll your own code.
Why would you say that a "standard stepper motor" can't be used with an A4988? It's done all the time. The A4988 is one of the most common stepper drivers around.
Ken_F:
Why would you say that a "standard stepper motor" can't be used with an A4988? It's done all the time. The A4988 is one of the most common stepper drivers around.
Humble apologies. I intended to say "standard stepper library"
You don't need a library. Just set the direction by writing HIGH or LOW to the DIR pin on the board, and step every time you pulse to the STEP pin. As Robin said, the AccelStepper library can be used if you want to use a library.