# Coding 2 x Steppers

G’day,

So, my aim is to drive this little “car” some exact distances. I’m using 5V 28BYJ-48 steppers withULN2003 driver boards. Pretty standard stuff. Anyway, I need it to drive exactly 8.3 turns of the wheel, turn on light, turn left 90deg, go fwd one rev of wheel turn right 90 deg, turn on light then fwd.

The following code doesn’t work. I think I’m close but am confusing myself somewhere. I’ve installed the AccelStepper library. Help??!!

``````#include <AccelStepper.h>
#define HALFSTEP 8

// motor pins
#define motorPin1  3     // IN1 on the ULN2003 driver 1
#define motorPin2  4     // IN2 on the ULN2003 driver 1
#define motorPin3  5     // IN3 on the ULN2003 driver 1
#define motorPin4  6     // IN4 on the ULN2003 driver 1

#define motorPin5  8     // IN1 on the ULN2003 driver 2
#define motorPin6  9     // IN2 on the ULN2003 driver 2
#define motorPin7  10    // IN3 on the ULN2003 driver 2
#define motorPin8  11    // IN4 on the ULN2003 driver 2

/* Initialize with pin sequence IN1-IN3-IN2-IN4 for using the AccelStepper with 28BYJ-48*/

AccelStepper stepper1(HALFSTEP, motorPin1, motorPin3, motorPin2, motorPin4);
AccelStepper stepper2(HALFSTEP, motorPin5, motorPin7, motorPin6, motorPin8);

/* variables */
int stepperSpeed = 500; //speed of the stepper (steps per second)
int steps1=0;
int steps2=0;
int linesteps=33831; // 8.3 x 4076 steps actually= 33830.8
int turnsteps=1019; // 90deg = a quater turn =4076/4
int sidesteps=4076; // single rev of wheel

void setup() {
delay(3000);    //3 secs to put the robot down after switching it on
stepper1.setMaxSpeed(2000.0);
//  stepper1.setAcceleration(100.0);
//  stepper1.move(1);
//  stepper1.setSpeed(stepperSpeed);

stepper2.setMaxSpeed(2000.0);
//  stepper2.setAcceleration(100.0);
//  stepper2.move(-1);
//  stepper2.setSpeed(stepperSpeed);
}

void loop() {

stepper1.move(linesteps);
stepper2.move(linesteps);
stepper1.setSpeed(stepperSpeed);
stepper2.setSpeed(stepperSpeed);
stepper1.runSpeedToPosition();
stepper2.runSpeedToPosition();

digitalWrite (2,HIGH);
delay (3000);  // illuminate the L.E.D. for 3 secs
digitalWrite (2,LOW);
delay (500);

stepper1.move(turnsteps);
stepper2.move(-turnsteps);
stepper1.setSpeed(stepperSpeed);
stepper2.setSpeed(stepperSpeed);
stepper1.runToPosition();
stepper2.runToPosition();

stepper1.move(sidesteps);
stepper2.move(sidesteps);
stepper1.setSpeed(stepperSpeed);
stepper2.setSpeed(stepperSpeed);
stepper1.runSpeedToPosition();
stepper2.runSpeedToPosition();

stepper1.move(-turnsteps);
stepper2.move(turnsteps);
stepper1.setSpeed(stepperSpeed);
stepper2.setSpeed(stepperSpeed);
stepper1.runToPosition();
stepper2.runToPosition();

digitalWrite (2,HIGH);
delay (3000);  // turn on the L.E.D. for 3 secs
digitalWrite (2,LOW);
delay (500);

stepper1.move(sidesteps);
stepper2.move(sidesteps);
stepper1.setSpeed(stepperSpeed);
stepper2.setSpeed(stepperSpeed);
stepper1.runSpeedToPosition();
stepper2.runSpeedToPosition();
}``````