Using Two Stepper Motors in Unison

I made this sketch to move a stepper a certain distance with a button press then move a bit further with another press of the button and then back to the start after the second distance is complete.

I'm using a mega 2560 and the sketch works well, see below.

I now want to do the same with two motors in unison son I changed how the motors are defined so I could have stepper1 and stepper2 but I am getting the error below when trying to compile.

Error compiling for board Arduino/Genuino Mega or Mega 2560.

I am an engineer but quite new to Arduino sketching any help would be appreciated.

first sketch.... this works

// Include the AccelStepper library:
#include <AccelStepper.h>
const int buttonPin = 4;
// Define stepper motor connections and motor interface type. Motor interface type must be set to 1 when using a driver:
#define dirPin 2
#define stepPin 3
#define motorInterfaceType 1

// Create a new instance of the AccelStepper class:
AccelStepper stepper = AccelStepper(motorInterfaceType, stepPin, dirPin);

void setup() {
// button to start each position:
pinMode(buttonPin, INPUT);

// Set the maximum speed in steps per second:
stepper.setMaxSpeed(1000);
}

void loop() {
int buttonState (LOW);
while (digitalRead(buttonPin)== LOW);
{

// Set the current position to 0:
stepper.setCurrentPosition(0);

// Run the motor forward at 600 steps/second until the motor reaches 400 steps (2 revolutions):
while(stepper.currentPosition() != 400)
{
stepper.setSpeed(600);
stepper.runSpeed();
}

delay(1000);
int buttonState (LOW);
while (digitalRead(buttonPin)== LOW);
{

// Reset the position to 0:
stepper.setCurrentPosition(0);

// Run the motor forward at 600 steps/second until the motor reaches 400 steps (1 revolution):
while(stepper.currentPosition() != 400)
{
stepper.setSpeed(600);
stepper.runSpeed();
}

delay(1000);

// Reset the position to 0:
stepper.setCurrentPosition(0);

// Run the motor backward at 400 steps/second until the motor reaches 600 steps (3 revolutions):
while(stepper.currentPosition() != -800)
{
stepper.setSpeed(-400);
stepper.runSpeed();
}

delay(3000);
}
}
}

second sketch...… this doesn't work

#include <AccelStepper.h>

/*Example sketch to control a stepper motor with A4988 stepper motor driver, AccelStepper library and Arduino: number of steps or revolutions. More info: https://www.makerguides.com */

// Include the AccelStepper library:

const int buttonPin = 4;
// Define stepper motor connections and motor interface type. Motor interface type must be set to 1 when using a driver:

// Create a new instance of the AccelStepper class:
AccelStepper stepper1(AccelStepper::DRIVER, 3, 2);

void setup() {

pinMode(buttonPin, INPUT);

// Set the maximum speed in steps per second:
stepper1.setMaxSpeed(1000);
}

void loop() {
int buttonState (LOW);
while (digitalRead(buttonPin)== LOW);
{

// Set the current position to 0:
stepper1.setCurrentPosition(0);

// Run the motor forward at 200 steps/second until the motor reaches 400 steps (2 revolutions):
while(stepper1.currentPosition() != 400)
{
stepper1.setSpeed(600);
stepper1.runSpeed();
}

delay(1000);
int buttonState (LOW);
while (digitalRead(buttonPin)== LOW);
{

// Reset the position to 0:
stepper1.setCurrentPosition(0);

// Run the motor forward at 600 steps/second until the motor reaches 400 steps (1 revolution):
while(stepper1.currentPosition() != 400)
{
stepper1.setSpeed(600);
stepper1.runSpeed();
}

delay(1000);

// Reset the position to 0:
stepper1.setCurrentPosition(0);

// Run the motor forward at 400 steps/second until the motor reaches 600 steps (3 revolutions):
while(stepper1.currentPosition() != -800)
{
stepper1.setSpeed(-400);
stepper1.runSpeed();
}

delay(3000);
}
}
}

To make it easy for people to help you please modify your post and use the code button </>
codeButton.png

so your code 
looks like this

and is easy to copy to a text editor. See How to use the Forum

Your code is too long for me to study quickly without copying to my text editor. The text editor shows line numbers, identifies matching brackets and allows me to search for things like all instances of a particular variable or function.

Also please use the AutoFormat tool to indent your code consistently for easier reading.

What exactly do you mean by "in unison". If you just want the two motors to move in exact lock-step as though they were a single motor then you could connect the two stepper drivers to the same Arduino step and direction pins.

If that is not what you mean please describe the requirement in detail because it may affect the approach to the program.

...R

We understand your concerns here..

What we are asking you to do is FORMAT your code so we can offer help or see whats going on.

Please do what was requested of you. (edit your post and format the code)

It makes it A LOT easier for us to see what is going on with correctly formatted code and not just text/run-ons.

update:

ok.. I guess i'm just not following what you are trying to do here?

I see ZERO references to a stepper2 instance ANYWHERE in your code attempts..

xl97:
update:

ok.. I guess i'm just not following what you are trying to do here?

I see ZERO references to a stepper2 instance ANYWHERE in your code attempts..

And you won't either. The OP posted his working sketch. So, there is no way to know where the problem he is having is.

As to what I "think" he's attempting to acheive. I have a project that involves 3 stepper motors. None of which, in my case, will have to run at the same time. However, I didnt want to use 12 pins on the Arduino to run them. So, I did as Robin2 mentioned above. And used the same 4 pins for all 3 motors. I then used 3 2N2222 transistors to switch the power to each of those motors as needed.

Maybe the OP could use a similar method. One transistor to switch power to both so they can run simultaneously. Another to power only one when he needs to calibrate. If course, if he needs to calibrate on the fly, this idea wouldn't work.

Anyway, I thought it relevant. If not, my apologies. Carry on.