Hi, very basic / stupid question as I am new to Arduino and programming.
I have two NEMA 34 motors that I would like to rotate until they reach a certain position, wait 5 seconds or so, and then return to the home position. I am having trouble achieving a smooth motion with both motors, and was wondering if there was a better way of doing it using the AccelStepper library. Currently I'm using the millis() command for the wait time but am struggling to make it work smoothly. I've read lots of other posts and explored examples in AccelStepper but can't quite get it right. Any advice would be much much appreciated.
Code:
#include <AccelStepper.h>
AccelStepper stepper1(1, 7, 6);
AccelStepper stepper2(1, 9, 8);
unsigned long currentMillis = 0;
unsigned long previousMillis = 0;
unsigned long Interval = 1000*10;
void setup() {
stepper1.setMaxSpeed(1500.0); // set the maximum speed
stepper1.setAcceleration(1000.0); // set acceleration
stepper1.setCurrentPosition(0);// set position
stepper1.moveTo(5000);
stepper2.setMaxSpeed(2500.0); // set the maximum speed
stepper2.setAcceleration(1000.0); // set acceleration
stepper2.setCurrentPosition(0);// set position
stepper2.moveTo(10000);
}
void loop() {
currentMillis = millis();
run();
stepper1.run();
stepper2.run();
}
void run(){
if (currentMillis - previousMillis >= Interval) {
stepper1.moveTo(-stepper1.currentPosition());
stepper2.moveTo(-stepper2.currentPosition());
}
}