Control two stepper motors to rotate, wait, and then return to home position

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());
   
  }
 
}

You certainly must have more that what you listed. What driver modules and what power supplies are you using. Please include a schematic of how you have all this connected. Then we can talk about software.

And where is previousMillis updated ?

Hi Larry,

I don't have previousMillis being updated anywhere

Look at some millis() timing tutorials to see where and why previousMillis must be updated.
Blink without delay().
Beginner's guide to millis().
Several things at a time.

If you want the motors to reach the destinations simultaneously, look at the Multistepper class of AccelStepper. No acceleration, though.

Then currentMillis - previousMillis >= Interval serves little use.

Apologies for not including.

I am using a 48V power supply (an Ender 3 Power Supply) and a Digital Stepper Driver 2.4-7.2A 20-80VDC as my driver.

In terms of wiring, I have it set up exactly as shown minus the resistor, pushbutton, and potentiometer

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.