How to move 2 stepper motors at the same time

Hi I am using a Arduino Mega a small reduce speed stepper motor(very slow and limited speed of 15), and a stepper motor driver(picture shown below).I want to write code for 2 stepper motors moving at the same time. I've searched everywhere for it but cannot find it. The code shown below is a version that doesn't work. I was wondering if there is another way to do it.

#include <arduino.h>

#include <Stepper.h>

int stepIN1Pin = 46;
int stepIN2Pin = 48;
int stepIN3Pin = 50;
int stepIN4Pin = 52;

int step2in1Pin = 37;
int step2in2Pin = 35;
int step2in3Pin = 33;
int step2in4Pin = 31;

int stepsPerRevolution = 2048;

Stepper myStepper(stepsPerRevolution,
stepIN1Pin, stepIN3Pin,
stepIN2Pin, stepIN4Pin);

Stepper myStepper2(stepsPerRevolution,
step2in1Pin, step2in3Pin,
step2in2Pin, step2in4Pin);

int echoPin = 12;
int trigPin = 11;

void setup()
{
// set the RPM
myStepper.setSpeed(16);
myStepper2.setSpeed(15);
Serial.begin(9600);
pinMode(echoPin, INPUT);
pinMode(trigPin, OUTPUT);
}
float distanceCentimeters;
int pulseLenMicroseconds;

void forward()
{
myStepper.step(1);
myStepper2.step(1);
}

void turn(String Direction)
{
if (Direction == "left")
{
myStepper.step(150);
myStepper.step(5);
}
else if (Direction == "right")
{
myStepper.step(5);
myStepper.step(150);
}
}

void backward()
{
myStepper.step(-1);
myStepper2.step(-1);
}

void loop()
{

distanceCentimeters = pulseLenMicroseconds / 29.387 / 2;
Serial.println(distanceCentimeters);
forward();
Serial.println(distanceCentimeters);
forward();
Serial.println(distanceCentimeters);

// bit-bang a small square wave
// on the trig pin to start the range
// finder
digitalWrite(trigPin, LOW);
delayMicroseconds(20);
digitalWrite(trigPin, HIGH);
delayMicroseconds(100);
digitalWrite(trigPin, LOW);

// measure the pulse length from the echo pin
pulseLenMicroseconds = pulseIn(echoPin, HIGH);

// calculate the distance using the speed of sound

}

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html . Then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Can you post a link to specs/data of your stepper controllers please?

Thanks.. Tom.. :slight_smile:

The code shown below is a version that doesn't work.

"Doesn't work" conveys no useful information that we can use to help you. You have not told us what the code is supposed to do nor what the code actually does.

Hi,
Have you got code that you have written that just controls ONE stepper only, no code at all for the other stepper.

If you have, did it work?
Can you please post the code?

Thanks.. Tom.. :slight_smile:

If you want to move two stepper motors at the same time with the Stepper library then you must move each of them one step at a time and use your own code to deal with the intervals between steps.

If the movement of the two motors does NOT need to be coordinated then you could use the AccelStepper library.

If you need coordinated motion (meaning that stepperA needs to go 156 steps in the exact same time as stepperB does 403 steps) then you can use the MultiStepper version of the AccelStepper library. But note that it does not do acceleration.

If you need coordination with acceleration it is not all that difficult to write your own code.

...R
Stepper Motor Basics
Simple Stepper Code

#include <arduino.h>

#include <Stepper.h>

int stepIN1Pin = 46;
int stepIN2Pin = 48;
int stepIN3Pin = 50;
int stepIN4Pin = 52;

int step2in1Pin = 37;
int step2in2Pin = 35;
int step2in3Pin = 33;
int step2in4Pin = 31;

int stepsPerRevolution = 2048;

Stepper myStepper(stepsPerRevolution,
stepIN1Pin, stepIN3Pin,
stepIN2Pin, stepIN4Pin);

Stepper myStepper2(stepsPerRevolution,
step2in1Pin, step2in3Pin,
step2in2Pin, step2in4Pin);

int echoPin = 12;
int trigPin = 11;

void setup()
{
// set the RPM
myStepper.setSpeed(16);
myStepper2.setSpeed(15);
Serial.begin(9600);
pinMode(echoPin, INPUT);
pinMode(trigPin, OUTPUT);
}
float distanceCentimeters;
int pulseLenMicroseconds;

void forward()
{
myStepper.step(1);
myStepper2.step(1);
}

void turn(String Direction)
{
if (Direction == "left")
{
myStepper.step(150);
myStepper.step(5);
}
else if (Direction == "right")
{
myStepper.step(5);
myStepper.step(150);
}
}

void backward()
{
myStepper.step(-1);
myStepper2.step(-1);
}

void loop()
{

distanceCentimeters = pulseLenMicroseconds / 29.387 / 2;
Serial.println(distanceCentimeters);
forward();
Serial.println(distanceCentimeters);
forward();
Serial.println(distanceCentimeters);

// bit-bang a small square wave
// on the trig pin to start the range
// finder
digitalWrite(trigPin, LOW);
delayMicroseconds(20);
digitalWrite(trigPin, HIGH);
delayMicroseconds(100);
digitalWrite(trigPin, LOW);

// measure the pulse length from the echo pin
pulseLenMicroseconds = pulseIn(echoPin, HIGH);

// calculate the distance using the speed of sound

}

Code:

#include <arduino.h>

#include <Stepper.h>

int stepIN1Pin = 46;
int stepIN2Pin = 48;
int stepIN3Pin = 50;
int stepIN4Pin = 52;

int step2in1Pin = 37;
int step2in2Pin = 35;
int step2in3Pin = 33;
int step2in4Pin = 31;

int stepsPerRevolution = 2048;

Stepper myStepper(stepsPerRevolution,
                stepIN1Pin, stepIN3Pin,
                stepIN2Pin, stepIN4Pin);

Stepper myStepper2(stepsPerRevolution,
                 step2in1Pin, step2in3Pin,
                 step2in2Pin, step2in4Pin);

int echoPin = 12;
int trigPin = 11;

void setup()
{
// set the RPM
myStepper.setSpeed(16);
myStepper2.setSpeed(15);
Serial.begin(9600);
pinMode(echoPin, INPUT);
pinMode(trigPin, OUTPUT);
}
float distanceCentimeters;
int pulseLenMicroseconds;

void forward()
{
myStepper.step(1);
myStepper2.step(1);
}

void turn(String Direction)
{
if (Direction == "left")
{
  myStepper.step(150);
  myStepper.step(5);
}
else if (Direction == "right")
{
  myStepper.step(5);
  myStepper.step(150);
}
}

void backward()
{
myStepper.step(-1);
myStepper2.step(-1);
}

void loop()
{

distanceCentimeters = pulseLenMicroseconds / 29.387 / 2;
Serial.println(distanceCentimeters);
forward();
Serial.println(distanceCentimeters);
forward();
Serial.println(distanceCentimeters);

// bit-bang a small square wave
// on the trig pin to start the range
// finder
digitalWrite(trigPin, LOW);
delayMicroseconds(20);
digitalWrite(trigPin, HIGH);
delayMicroseconds(100);
digitalWrite(trigPin, LOW);

// measure the pulse length from the echo pin
pulseLenMicroseconds = pulseIn(echoPin, HIGH);

// calculate the distance using the speed of sound

}

LIT.jpg

For AccelStepper library how do I download latest version? :confused:

The stepper motors I am using: 28byj-48 geared step motors

The stepper motors I am using: 28byj-48 geared step motors

It is very difficult to keep up when you post several Replies in succession without waiting for a response. You can always edit a Reply and add to it if necessary.

Do you have a question at the moment, and what is it?

...R