vimit
December 25, 2024, 3:42pm
1
Hi All,
I want to test a basic stepper motor control using speed and acceleration using AccelStepper library. Test scenario - Run to position 900 or 900 steps with a set acceleration and speed, delay for 2 seconds,Use code tags to format code for the forum and then Run to position 0 so rotate the motor in the reverse direction. Repeat this after a pause of 1 second.
I am not sure which part of the code is not working. Would love any guidance from the group.
I have created a sample simulation for this as well to test. Link here Stepper motor control with acceleration and speed
P.S. I am a very beginner and learning different stepper motor controls in different ways.
#include <AccelStepper.h>
// Define pin assignments
#define dirPin 8
#define stepPin 9
// Define stepper motor parameters
const int stepsPerRevolution = 200; // Change based on stepper motor specs
const float degreesPerStep = 360.0 / stepsPerRevolution;
const int stepsPer90Degrees = stepsPerRevolution / 4; // Steps for 90 degrees
const float maxSpeed = 30; // Max speed (steps per second)
const float setAcceleration = 1; // Acceleration (steps per second^2)
AccelStepper stepper(AccelStepper::DRIVER, stepPin, dirPin);
void setup() {
pinMode(stepPin, OUTPUT);
pinMode(dirPin, OUTPUT);
digitalWrite(dirPin,HIGH);
digitalWrite(stepPin,HIGH);
stepper.setMaxSpeed(maxSpeed);
stepper.setAcceleration(setAcceleration);
//digitalWrite(stepPin, LOW);
delay(100);
}
void loop() {
if (digitalRead(stepPin)==HIGH) {
stepper.moveTo(900);
delay(2000);
stepper.moveTo(0);
}
stepper.runToPosition();
digitalWrite(stepPin, LOW);
delay(1000);
}
MrY
December 25, 2024, 4:33pm
2
If you look at the AccelStepper bounce example , there is no delay in loop(), but the run method is called continuously.
I suggest starting with this example, slowly modifying it, and testing it until you achieve your desired result.
1 Like
JCA34F
December 25, 2024, 4:47pm
3
Read the details section of this:
Finding myself needing to use the AccelStepper library again, and again finding little documentation after several searches, I decided to write this manual as much for myself as for anybody else. This is not to say there is no documentation, it's...
2 Likes
vimit
December 25, 2024, 8:48pm
4
Thank you both! Its partially working now except without any pause or delay in between.
What is the best way to use delay in the if loop? I tried and the motor does not move at all.
Simulation link
void setup() {
stepper.setMaxSpeed(maxSpeed);
stepper.setAcceleration(setAcceleration);
stepper.moveTo(16200);
}
void loop() {
if (stepper.distanceToGo() == 0)
//delay(1000);
stepper.moveTo(0);
stepper.run();
}
You don't. You call stepper.run() in the loop function.
Have a look at the library docs, example programs and the manual linked in the post above, in order to learn how to use the library.
When using any new library, always make sure that one or more of the provided examples (e.g. "bounce") works as expected, before writing any code.
vimit
December 26, 2024, 12:24am
6
Is there a way to improve my code without using any buttons or switches, and also add a pause before the motor rotates in opposite direction? Thanks!
Start with the AccelStepper bounce example and get that working. Then add the delay correctly, using a code block (bracketed set of statements following the if clause).
1 Like
vimit
December 26, 2024, 6:32pm
8
Thank you! It works now
Here is the updated code
void loop() {
if (stepper.distanceToGo() == 0) {
delay(5000);
stepper.moveTo(0);
}
stepper.run();
}
system
Closed
June 24, 2025, 6:32pm
9
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.