Hi, I'm a total noob with arduino programming, but I have been spending about a week trying to solve this issue I'm having with simultaneous/independent stepper motor movement. The code below is what I have settled on using to control the steppers independently, but I would like to use AccelStepper to be able to move stepper2 alone and then, for a moment, move stepper1 and stepper 2 at the same time before returning to only moving stepper2 again. I'm not sure how to make the conditions of a for, while, or if loop such that they will run for a short time within the void loop and then terminate so that I can go back to these runToNewPosition movements for stepper2.
Please see this link to see what I have so far as far as movement. https://youtu.be/o4Y3FYZdYB4
Right Now the device I am making would make long passes like mowing the lawn, but at the ends would be square like a square wave and I am looking to make more of a rounded end to these passes.
Thanks.
Hi, Is there a simpler way to just get a specific pattern of both motors to execute before going back to individual movement? Sorry I am pretty new to this and it would tie it all in together if I could just see how this would work for my specific case in code.
Not sure exactly what you're after..
Here's your code modified to use state machines and non-blocking approach to the motors..
Got a state machine for each motor also added a pause function for each motor and demonstrated how to call it..
#include <AccelStepper.h>
#define STEPPER1_STEP_PIN 8
#define STEPPER1_DIR_PIN 9
#define STEPPER2_STEP_PIN 10
#define STEPPER2_DIR_PIN 11
AccelStepper stepper1(AccelStepper::DRIVER, STEPPER1_STEP_PIN, STEPPER1_DIR_PIN);
AccelStepper stepper2(AccelStepper::DRIVER, STEPPER2_STEP_PIN, STEPPER2_DIR_PIN);
int Motor1State, Motor2State;
bool CheckDest1, CheckDest2;
unsigned long lastPause1 = 0;
unsigned int intervalPause1 = 1000;
unsigned long lastPause2 = 0;
unsigned int intervalPause2 = 1000;
void setup()
{
stepper1.setMaxSpeed(5000);
stepper1.setAcceleration(2000);
stepper1.setCurrentPosition(0);
stepper2.setMaxSpeed(5000);
stepper2.setAcceleration(2000);
stepper2.setCurrentPosition(0);
Motor1State = 0;
Motor2State = 0;
stepper1.moveTo(-400);
stepper2.moveTo(2000);
CheckDest1 = true;
CheckDest2 = true;
}
void loop() {
stepper1.run();
stepper2.run();
if (CheckDest1) {
if (stepper1.distanceToGo() == 0) {
CheckDest1 = false;
Motor1State++;
if (Motor1State > 9)Motor1State = 0;
}
}
if (CheckDest2) {
if (stepper2.distanceToGo() == 0) {
CheckDest2 = false;
Motor2State++;
if (Motor2State > 9)Motor2State = 0;
}
}
switch (Motor1State) {
case 0: stepper1.moveTo(-400); CheckDest1 = true; break;
case 1: stepper1.moveTo(-800); CheckDest1 = true; break;
case 2: stepper1.moveTo(-1200); CheckDest1 = true; break;
case 3: stepper1.moveTo(-1600); CheckDest1 = true; break;
case 4: lastPause1 = millis(); intervalPause1 = 2000; Motor1State++; break;
case 5: PauseMotor1(); break;
case 6: stepper1.moveTo(-2000); CheckDest1 = true; break;
case 7: stepper1.moveTo(-2400); CheckDest1 = true; break;
case 8: stepper1.moveTo(-2800); CheckDest1 = true; break;
case 9: stepper1.moveTo(0); CheckDest1 = true; break;
}
switch (Motor2State) {
case 0: stepper2.moveTo(2000); CheckDest2 = true; break;
case 1: stepper2.moveTo(0); CheckDest2 = true; break;
case 2: stepper2.moveTo(2000); CheckDest2 = true; break;
case 3: stepper2.moveTo(0); CheckDest2 = true; break;
case 4: stepper2.moveTo(2000); CheckDest2 = true; break;
case 5: lastPause2 = millis(); intervalPause2 = 2000; Motor2State++; break;
case 6: PauseMotor2(); break;
case 7: stepper2.moveTo(0); CheckDest2 = true; break;
case 8: stepper2.moveTo(2000); CheckDest2 = true; break;
case 9: stepper2.moveTo(0); CheckDest2 = true; break;
}
}
void PauseMotor1() {
if (millis() - lastPause1 >= intervalPause1) {
lastPause1 = millis();
Motor1State++;
if (Motor1State > 9) Motor1State = 0;
}
}
void PauseMotor2() {
if (millis() - lastPause2 >= intervalPause2) {
lastPause2 = millis();
Motor2State++;
if (Motor2State > 9) Motor2State = 0;
}
}