I am using a Arduino Nano with a A4988 stepper driver connected to a Nema11 stepper motor.
I wish to spin the motor in one direction (I dont care which) at 60-75rpm for ~20 seconds, then stop, reverse direction and repeat until I power off the device.
I have written the following code using "AccelStepper" library as it would be nice to slowly accelerate and decelerate, but I am unsure as to whether I have written a good code, partially or complete gibberish.
Speeds are calculated thus...250 steps per second at 200 steps per revolution gives me 1.25 revs per second or 75rpm.
200 steps for 20 seconds is 5000 steps.
I also apologise profusely if this is not the correct way to post code...Its only short and didnt know a better way.
// Include AccelStepper Library
#include <AccelStepper.h>
//Define motor pin connections
const int dirPin = 2;
const int stepPin = 3;
// Define motor interface type
#define motorInterfaceType 1
// Creates an instance
AccelStepper myStepper(motorInterfaceType, stepPin, dirPin);
void setup() {
// set the maximum speed, acceleration factor,
// initial speed and the target position
myStepper.setMaxSpeed(350);
myStepper.setAcceleration(50);
myStepper.setSpeed(250);
myStepper.moveTo(5000);
}
void loop() {
// Change direction once the motor reaches target position
if (myStepper.distanceToGo() == 10)
myStepper.moveTo(-myStepper.currentPosition());
// Move the motor one step
myStepper.run();
}
I am not sure if it works...I am currently at work and my breadboard set up at home. I will check on it over the weekend (amongst other chores...booo!).
I am not sure what i want to do with steppers next. I am a mechanical engineer/designer by trade and have designed a lot around steppers, but never got into the coding side.
This is one of my old printers from about 13 years ago
No, it should be zero. distanceToGo tells the remaining steps until it reaches the target. So if it is zero, the target ( 5000 in your case) is reached, and you can reverse.
Even worse: distance to go is a signed value. when turning backwards it returns a negative value and will never get 10. So after one cycle forward/backwards it will stop. Of course this doesn't matter if you check for 0.
An be aware, when you tell it to go to -myStepper.currentPosition() it will not go back to 0, but back to position -5000 (in your case). So it will step 10000 steps backwards. from position +5000 to position -5000.
So its starting from 0....Moving 5000 steps then into reverse and to -5000?
Could i just lose the reverse step (after void loop) and tell it to go to 5000 and then go to 0?
void setup() {
// set the maximum speed, acceleration factor,
// initial speed and the target position
myStepper.setMaxSpeed(350);
myStepper.setAcceleration(50);
myStepper.setSpeed(250);
myStepper.moveTo(5000);
myStepper.moveTo(0)
}
// Include AccelStepper Library
#include <AccelStepper.h>
//Define motor pin connections
const int dirPin = 2;
const int stepPin = 3;
const long targetPos = 5000;
// Define motor interface type
#define motorInterfaceType 1
// Creates an instance
AccelStepper myStepper(motorInterfaceType, stepPin, dirPin);
void setup() {
// set the maximum speed, acceleration factor,
// initial speed and the target position
myStepper.setMaxSpeed(350);
myStepper.setAcceleration(50);
// myStepper.setSpeed(250); // This is useless, as .run() only follows maxSpeed!
myStepper.moveTo(5000);
}
void loop() {
// Change direction once the motor reaches target position
if (myStepper.distanceToGo() == 0) {
if (myStepper.currentPosition() == 0) {
myStepper.moveTo(targetPos);
} else {
myStepper.moveTo(0);
}
}
// Move the motor one step
myStepper.run();
}