I am using a 9 Volt power supply and my dirPin is connected to pin number 3 in the arduino board and my stepPin to pin number 2. For now what I'm trying to do is to move the motor 100 steps clockwise and then move it 100 steps anti-clockwise, so that it returns to the starting position. This is the code I am using:
#include <AccelStepper.h>
#define dirPin 3
#define stepPin 2
#define motorInterfaceType 1
int SPR = 200;
AccelStepper stepper(motorInterfaceType, stepPin, dirPin);
void setup() {
pinMode(dirPin,OUTPUT);
pinMode(stepPin,OUTPUT);
// put your setup code here, to run once:
stepper.setMaxSpeed(200);
stepper.setAcceleration(30);
}
void loop() {
// put your main code here, to run repeatedly:
stepper.moveTo(100);
stepper.runToPosition();
delay(1000);
stepper.moveTo(-100);
stepper.runToPosition();
delay(1000);
}
But I am having 2 issues:
Firstly before moving the motor 100 steps, it moves like a couple of steps (like 15 degrees), and then it starts moving. Firstly 100 steps, and then 200 steps. That would be the first iteration, and I don´t understand why it moves those 200 steps. And then, in the following iterations, the motor just moves 200 steps.
Secondly, it just moves in one direction. I can´t manage to move it in the anti-clockwise direction.
Thanks for reading, if someone can help me I would greatly appreciate it.
Check that your motor coil wires aren't crossed. If you swap one of the coil A wires for a coil B wire, you can get one-directional rotation and weakness.
Also:
How strong is your 9V power supply? An inadequate supply relative to your stepper could give strange results.