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();
}

Hi Adrian,

Thanks for posting your question. When you say that it doesn’t work, could you provide some more information about what is happening?

Sry, yeah.
Well, stepper 1 ticks over slowly at about 1 step per 2 secs While stepper 2 doesn’t move. That’s it. I haven’t run it long enough to see if it does anything else.
I’m running both motors in parallel off a separate Pow Supp. (Which is that DC transformer pictured)
So there should be enough V and I to run it at 500 step/sec.

Ok, well the best thing to do when troubleshooting systems is to go back to the start and test each element as you go to make sure nothing has been overlooked.

So the first thing I would do is to just take one of the steppers, a driver, and the power supply, and write a few simple programs to make sure it works as expected. Then, repeat the process with your other motor and driver. Then connect them both at the same time, and just use some simple code to check their basic functionality.

If that’s all working as expected, then it indicates that there is an error in your code somewhere. If not, then you know what the issue is and we can get that sorted out.

1 Like