Running 3 steppers simultaneously

I was wondering if there was anyone who might be able to give me some advice on how to make all three steppers begin simultaneously? I understand for the most part why they are turning on and off because of the way the Arduino “reads” the code starting from top to bottom, but i dont understand how to tell it to “read” all three commands to the steppers at the same time.

Thanks in advance!

#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_PWMServoDriver.h"

Adafruit_MotorShield AFMStop = Adafruit_MotorShield(0x60); 
Adafruit_MotorShield AFMSbot = Adafruit_MotorShield(0x61); 


Adafruit_StepperMotor *myMotor1 = AFMStop.getStepper(200, 2);
Adafruit_StepperMotor *myMotor2 = AFMStop.getStepper(200, 1);
Adafruit_StepperMotor *myMotor3 = AFMSbot.getStepper(200, 2);



int buttonPin = A0;
int buttonState = HIGH; 


int x;
int y;

void setup() {
  
    pinMode(buttonPin, INPUT_PULLUP); 
    
    x = random(0,19);
    y = random(0,9);

  AFMStop.begin();  // create with the default frequency 1.6KHz
  AFMSbot.begin();  // create with the default frequency 1.6KHz
  
    myMotor1->setSpeed(500);  //  rpm   
    myMotor2->setSpeed(500);  //  rpm 
    myMotor3->setSpeed(500);  //  rpm   

}

void loop() {
  
  buttonState = digitalRead(buttonPin);
  
  if (buttonState == LOW) {
  myMotor1->step(((20 * (10 + x)) + (20 * y)), FORWARD, SINGLE); 
  myMotor2->step(((20 * (30 + x)) + (20 * y)), FORWARD, SINGLE); 
  myMotor3->step(((20 * (50 + x)) + (20 * y)), FORWARD, SINGLE);
  }
}

If you want them to move simultaneously you should get them to move one step at a time and use your own program to keep track of the steps.

Or you could try the AccelStepper library. It will quite likely allow you to do what you want.

...R Stepper Motor Basics

I understand for the most part why they are turning on and off because of the way the Arduino "reads" the code starting from top to bottom

That is not true. The motors run one at a time because the step() method does not return until the specified number of steps has been taken.

The AccelStepper and CustomStepper classes deal with this issue in other ways. You will need to use one of them to get all of your steppers moving at the same time.\

Robin's idea of moving the steppers one step at a time is good. Whether or not it will work for you depends on what the steppers are doing. If two of them are moving something around on an XY table, and it takes 1000 steps to move 1 inch in either direction, stepping once in x then once in y will produce a stair-step trace that may, or may not, be acceptable. Each step would be 0.001" high and long.

With AccelStepper, the x and y steppers can be turning at the same time, producing a smoother curve (though unlikely to be a straight line) between two positions.

Thanks for the replies, i really appreciate it!

So i used the Accel library, (actually just the example for running multiple steppers). The problem i’m running into with this one is that while it does start all the motors at the same time, i have to hold down the button (i want to simply push the button once and have them start), and more importantly, after they finish their “cycles”, when i push the button again to restart them, nothing happens, i have to restart the program by either hitting the rest button on the shield or re-uploading the program. Here’s my sketch using the Accel. Any ideas? (btw, as you can probably tell, i’m not exactly a master at programming)

#include <AccelStepper.h>
#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_PWMServoDriver.h"

Adafruit_MotorShield AFMSbot(0x61); // Rightmost jumper closed
Adafruit_MotorShield AFMStop(0x60); // Default address, no jumpers

Adafruit_StepperMotor *myStepper1 = AFMStop.getStepper(200, 1);
Adafruit_StepperMotor *myStepper2 = AFMStop.getStepper(200, 2);
Adafruit_StepperMotor *myStepper3 = AFMSbot.getStepper(200, 2);


void forwardstep1() {  
  myStepper1->onestep(FORWARD, SINGLE);
}
void forwardstep2() {  
  myStepper2->onestep(FORWARD, SINGLE);
}
void forwardstep3() {  
  myStepper3->onestep(FORWARD, SINGLE);
}

AccelStepper stepper1(forwardstep1, forwardstep1);
AccelStepper stepper2(forwardstep2, forwardstep2);
AccelStepper stepper3(forwardstep3, forwardstep3);

int buttonPin = A0;
int buttonState = HIGH; 

int x;
int y;

void setup()
{  
  pinMode(buttonPin, INPUT_PULLUP);     
  
  x = random(0,19);
  y = random(0,9);
  
  AFMSbot.begin(); // Start the bottom shield
  AFMStop.begin(); // Start the top shield
   
  stepper1.setMaxSpeed(750.0);
  stepper1.setAcceleration(100.0);
  stepper1.moveTo(((20 * (10 + x)) + (20 * y)));
    
  stepper2.setMaxSpeed(750.0);
  stepper2.setAcceleration(100.0);
  stepper2.moveTo(((20 * (30 + x)) + (20 * y)));

  stepper3.setMaxSpeed(750.0);
  stepper3.setAcceleration(100.0);
  stepper3.moveTo(((20 * (50 + x)) + (20 * y)));
}

void loop()
{
  buttonState = digitalRead(buttonPin);
  
  if (buttonState == LOW) {   
    stepper1.run();
    stepper2.run();
    stepper3.run();
    }
}

Any ideas?

Get rid of code that you are not using.

Once the steppers get to the positions defined in setup(), as happens in loop(), they are where they are. Until you give them a new destination, they are doing exactly what they are supposed to do.

Okay, sure, I think I understand, sort of. Any idea or example you could give me? Like I said, I’m really not so good when it comes to programming. Thanks

Hey all, thank you very much for all the advice.

I finally figured out what to do to get the results i was looking for. Oh, a little back info, the project im doing is i am trying to make a slot machine using steppers where the “reels” all start at the same time, but stop one after another, all with the press of a button. fwiw, here’s the code that allows this to happen:

#include <AccelStepper.h>
#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_PWMServoDriver.h"

Adafruit_MotorShield AFMSbot(0x61); // Rightmost jumper closed
Adafruit_MotorShield AFMStop(0x60); // Default address, no jumpers

Adafruit_StepperMotor *myStepper1 = AFMStop.getStepper(200, 1);
Adafruit_StepperMotor *myStepper2 = AFMStop.getStepper(200, 2);
Adafruit_StepperMotor *myStepper3 = AFMSbot.getStepper(200, 2);


void forwardstep1() {  
  myStepper1->onestep(FORWARD, SINGLE);
}
void forwardstep2() {  
  myStepper2->onestep(FORWARD, SINGLE);
}
void forwardstep3() {  
  myStepper3->onestep(FORWARD, SINGLE);
}

AccelStepper stepper1(forwardstep1, forwardstep1);
AccelStepper stepper2(forwardstep2, forwardstep2);
AccelStepper stepper3(forwardstep3, forwardstep3);

int buttonPin = A0;
int buttonState = HIGH; 

int x;
int y;

void setup()
{  
  pinMode(buttonPin, INPUT_PULLUP);     
  
  x = random(0,19);
  y = random(0,9);
  
  AFMSbot.begin(); // Start the bottom shield
  AFMStop.begin(); // Start the top shield
   
  stepper1.setMaxSpeed(750.0);
  stepper1.setAcceleration(200.0);
    
  stepper2.setMaxSpeed(750.0);
  stepper2.setAcceleration(200.0);

  stepper3.setMaxSpeed(750.0);
  stepper3.setAcceleration(200.0);
}

void loop()
{
  buttonState = digitalRead(buttonPin);
 if (stepper1.distanceToGo() == 0)
	stepper1.moveTo(1);

    if (stepper2.distanceToGo() == 0)
	stepper2.moveTo(1);

    if (stepper3.distanceToGo() == 0)
	stepper3.moveTo(1);

  if (buttonState == LOW) {   
      stepper1.moveTo(((20 * (10 + x)) + (20 * y)));
      stepper2.moveTo(((20 * (30 + x)) + (20 * y)));
      stepper3.moveTo(((20 * (50 + x)) + (20 * y)));
    }
    stepper1.run();
    stepper2.run();
    stepper3.run();
}
Adafruit_StepperMotor *myStepper1 = AFMStop.getStepper(200, 1);
Adafruit_StepperMotor *myStepper2 = AFMStop.getStepper(200, 2);
Adafruit_StepperMotor *myStepper3 = AFMSbot.getStepper(200, 2);


void forwardstep1() {  
  myStepper1->onestep(FORWARD, SINGLE);
}
void forwardstep2() {  
  myStepper2->onestep(FORWARD, SINGLE);
}
void forwardstep3() {  
  myStepper3->onestep(FORWARD, SINGLE);
}

Useless code. None of these instances do anything. None of these functions is called.