Robin2:
You have not taken account of the most critical piece of advice - putting stepper.run() into loop() rather than in functions.
Study the examples that come with the Accelstepper library.
...R
I'm uncertain as to how to integrate these functions by putting run in the loop; does the moveForward() function not get called constantly, and so I would not be able to reset the stepper motors position once they've completed their movement as it will just get reset constantly? I've read through several examples on this site (AccelStepper: Examples) but I don't see how I can get as much control by butting run() in the loop?
Edit: I rewrote it to use run in the loops. but now only one of the motors is spinning; I'm pretty sure it's blocking, but I don't know why this code is blocking it compared to the previous. What can I do to avoid blocking? This is the rewritten code:
// MultiStepper
// -*- mode: C++ -*-
//
// Control both Stepper motors at the same time with different speeds
// and accelerations.
// Requires the AFMotor library (https://github.com/adafruit/Adafruit-Motor-Shield-library)
// And AccelStepper with AFMotor support (https://github.com/adafruit/AccelStepper)
// Public domain!
#include <AccelStepper.h>
#include <AFMotor.h>
// two stepper motors one on each port
AF_Stepper motor1(200, 1);
AF_Stepper motor2(200, 2);
int movement = 0;
const int MOTION = 1000;
// you can change these to DOUBLE or INTERLEAVE or MICROSTEP!
// wrappers for the first motor!
void forwardstep1() {
motor1.onestep(FORWARD, SINGLE);
}
void backwardstep1() {
motor1.onestep(BACKWARD, SINGLE);
}
// wrappers for the second motor!
void forwardstep2() {
motor2.onestep(FORWARD, SINGLE);
}
void backwardstep2() {
motor2.onestep(BACKWARD, SINGLE);
}
// Motor shield has two motor ports, now we'll wrap them in an AccelStepper object
AccelStepper stepperLeft(forwardstep1, backwardstep1);
AccelStepper stepperRight(forwardstep2, backwardstep2);
void setup() {
stepperLeft.setMaxSpeed(200);
stepperLeft.setAcceleration(200);
stepperLeft.moveTo(0);
stepperRight.setMaxSpeed(200);
stepperRight.setAcceleration(200);
stepperRight.moveTo(0);
}
void moveLeft(int steps) // STEPS MUST BE NEG TO MAKE IT REVERSE
{
//stepperRight.stop();
stepperLeft.moveTo(steps);
stepperLeft.setSpeed(50);
}
void moveRight(int steps)
{
//stepperLeft.stop();
stepperRight.moveTo(steps);
stepperRight.setSpeed(50);
}
void moveForward()
{
stepperLeft.moveTo(MOTION);
stepperLeft.setSpeed(50);
stepperRight.moveTo(MOTION);
stepperRight.setSpeed(50);
}
void moveBackward()
{
stepperLeft.moveTo(-MOTION);
stepperLeft.setSpeed(50);
stepperRight.moveTo(-MOTION);
stepperRight.setSpeed(50);
}
void loop() {
int steps = random(-1000, 0);
int steps2 = random(-1000, 0);
if (stepperLeft.distanceToGo() == 0 && stepperRight.distanceToGo() == 0)
{
stepperRight.setCurrentPosition(0);
stepperLeft.setCurrentPosition(0);
movement += 1;
}
if (movement == 0) {
moveBackward();
}
if (movement == 1)
{
moveForward();
}
else {
moveRight(steps);
moveLeft(steps2);
}
stepperRight.run();
stepperLeft.run();
}
EDIT #2: I've tried making further changes but now the motors will spin one way indefinitely; I've really messed this up 
// MultiStepper
// -*- mode: C++ -*-
//
// Control both Stepper motors at the same time with different speeds
// and accelerations.
// Requires the AFMotor library (https://github.com/adafruit/Adafruit-Motor-Shield-library)
// And AccelStepper with AFMotor support (https://github.com/adafruit/AccelStepper)
// Public domain!
#include <AccelStepper.h>
#include <AFMotor.h>
// two stepper motors one on each port
AF_Stepper motor1(200, 1);
AF_Stepper motor2(200, 2);
int movement = 0;
int control = 0;
const int MOTION = 1000;
// you can change these to DOUBLE or INTERLEAVE or MICROSTEP!
// wrappers for the first motor!
void forwardstep1() {
motor1.onestep(FORWARD, SINGLE);
}
void backwardstep1() {
motor1.onestep(BACKWARD, SINGLE);
}
// wrappers for the second motor!
void forwardstep2() {
motor2.onestep(FORWARD, SINGLE);
}
void backwardstep2() {
motor2.onestep(BACKWARD, SINGLE);
}
// Motor shield has two motor ports, now we'll wrap them in an AccelStepper object
AccelStepper stepperLeft(forwardstep1, backwardstep1);
AccelStepper stepperRight(forwardstep2, backwardstep2);
void setup() {
stepperLeft.setMaxSpeed(200);
stepperLeft.setAcceleration(200);
stepperLeft.moveTo(0);
stepperRight.setMaxSpeed(200);
stepperRight.setAcceleration(200);
stepperRight.moveTo(0);
}
void moveLeft(int steps) // STEPS MUST BE NEG TO MAKE IT REVERSE
{
//stepperRight.stop();
stepperLeft.moveTo(steps);
stepperLeft.setSpeed(50);
}
void moveRight(int steps)
{
//stepperLeft.stop();
stepperRight.moveTo(steps);
stepperRight.setSpeed(50);
}
void moveForward()
{
stepperLeft.moveTo(MOTION);
stepperLeft.setSpeed(50);
stepperRight.moveTo(MOTION);
stepperRight.setSpeed(50);
if (stepperLeft.distanceToGo() == 0 && stepperRight.distanceToGo() == 0)
{
stepperRight.stop();
stepperLeft.stop();
stepperRight.setCurrentPosition(0);
stepperLeft.setCurrentPosition(0);
movement += 1;
}
}
void moveBackward()
{
stepperLeft.moveTo(-MOTION);
stepperLeft.setSpeed(50);
stepperRight.moveTo(-MOTION);
stepperRight.setSpeed(50);
if (stepperLeft.distanceToGo() == 0 && stepperRight.distanceToGo() == 0)
{
stepperRight.stop();
stepperLeft.stop();
stepperRight.setCurrentPosition(0);
stepperLeft.setCurrentPosition(0);
movement += 1;
}
}
void loop() {
int steps = random(-1000, 0);
int steps2 = random(-1000, 0);
if (movement == 0 && control == 0) {
moveBackward();
control += 1;
}
if (movement == 1 && control == 1)
{
moveForward();
control += 1;
}
else {
moveRight(steps);
moveLeft(steps2);
}
stepperRight.run();
stepperLeft.run();
}
Edit#3: After some debugging I've found that the "moveTo" and "distanceToGo" of the motors is increasing to infinity; I assume this is happening because my code keeps adding 1000 steps every loop. How would I go about rectifying this?