# How do you program 2 motors to move at once?

I am trying to make an obstacle avoidance robot with 2 steppers and an ultra-sonic sensor. It has taken me forever to find out how to get one moving, but If I want the robot to drive, then I need to move both motors at the same time.

How on earth do you program 2 things to do something at the same time.

This is my current attempt.

#include <Stepper.h>

int motorPin1 = 4;
int motorPin2 = 5;
int motorPin3 = 6;
int motorPin4 = 7;

int motorPin5 = 8;
int motorPin6 = 9;
int motorPin7 = 10;
int motorPin8 = 11;

#define STEPS 64

Stepper stepper1(STEPS, motorPin1, motorPin3, motorPin2, motorPin4);
Stepper stepper2(STEPS, motorPin5, motorPin7, motorPin6, motorPin8);

int Steps2Take;

void setup(){
stepper1.setSpeed(200);
stepper2.setSpeed(200);
}

void loop(){
stepper1.setSpeed(200); stepper2.setSpeed(200);
Steps2Take = 2048; Steps2Take = 2048;
stepper1.step(Steps2Take); stepper2.step(Steps2Take);
delay(1000);

stepper1.setSpeed(200); stepper2.setSpeed(200);
Steps2Take = -2048; Steps2Take = 2048;
stepper1.step(Steps2Take); stepper2.step(Steps2Take);
delay(1000);
}

I have found out a way to move both motors at the same time, but the piece of code that I need for the ultrasonic sensor slows down the motors. Is there a way around this problem?

#include <Stepper.h>

int motorPin1 = 4;
int motorPin2 = 5;
int motorPin3 = 6;
int motorPin4 = 7;

int motorPin5 = 8;
int motorPin6 = 9;
int motorPin7 = 10;
int motorPin8 = 11;

int echo = 3;
int trig = 2;

#define STEPS 64

Stepper stepper1(STEPS, motorPin1, motorPin3, motorPin2, motorPin4);
Stepper stepper2(STEPS, motorPin5, motorPin7, motorPin6, motorPin8);

int Steps2Take;

void setup(){
stepper1.setSpeed(200);
stepper2.setSpeed(200);
pinMode (trig, OUTPUT);
pinMode (echo, INPUT);
}

void loop(){
int duration, distance; //ultrasonic sensor stuff
digitalWrite(trig, HIGH);
delayMicroseconds(1000);
digitalWrite(trig, LOW);
duration = pulseIn(echo, HIGH);
distance = (duration/2) /29.1;
if (distance > 30) //if nothing is in the way, go forwards.
{
stepper1.step(1);
stepper2.step(1);
}
else //if there is, turn.
{
stepper1.step(2048);
}
}

Why the enormous jump from stepper1.step(1) to .step(2048)?

There is no point dividing by 29.1 when using integers.

Get rid of the delay() and replace it with the technique in the Blink Without Delay example sketch. That will allow your motors to keep moving throughout the “waiting” period.

Consider using the AccelStepper library.

…R

I cannot change these parts because it is for the U.S sensor. (I got this part off youtube)

digitalWrite(trig, HIGH); delayMicroseconds(1000); digitalWrite(trig, LOW); duration = pulseIn(echo, HIGH); distance = (duration/2) /29.1;

RoBoTs9999: I cannot change these parts because it is for the U.S sensor. (I got this part off youtube)

I did not mean that you should not have a "wait" of 1 second. I just meant that you need to change how you achieve it.

...R

1000 [u]MICRO[/u]seconds is 1 millisecond.

RoBoTs9999: 1000 [u]MICRO[/u]seconds is 1 millisecond.

Sorry - my mistake.

The delay will slow the frequency of motor steps - but I don't know if that matters. Try commenting out the line to see if it helps the steppers even if it screws up the sensor temporarily.

A more likely culprit might be the code to read the sensor. PulseIn() waits for stuff to happen.

Perhaps you don't need to read the sensor so often and you could move that code out of loop() and call it (say) every few seconds. Use the Blink Without Delay technique for the timing so that motor steps can continue. If pulseIn() is the problem you will still get delays in the steps whenever it is called. If that matters you will have to write your own non-blocking version of pulseIn() or find one someone else wrote.

...R

I don't get how to adapt the blink without delay program. It's too complicated and I think it doesn't do what I'm looking for. This part of my program makes it difficult to change and is why I don't get it.

if (distance > 30) { stepper1.step(1); //move forwards if distance is more than 30cm stepper2.step(1); } else { stepper1.step(2048); //turn if distance is less than 30cm }

Could you please write some code for me to copy? I'm still learning. ;)

RoBoTs9999: Could you please write some code for me to copy? I'm still learning. ;)

You never answered my earlier question about the jump from stepper.run(1) to stepper.run(2048). It may help me to undertsand how you are thinking.

The sketch attached to the first post in this Thread is an extended demo of the Blink Without Delay technique. It uses LEDs but I think you should see how it could be used with steppers.

...R

Sorry about that. I hope this explains it.

stepper1 step(2048) is move one motor one rotation.

stepper1 step(1); move each stepper 1 step and then repeat this action through the loop to make it seem that both motors are stepper2 step(1); moving at the same time

The problem is that you can't leave a stepper on while you turn another one on. The first stepper needs to stop moving before you can instruct the other one what to do.

I found a solution!!!!!!!!!! XD You have to put the steppers on (HIGH); :P I didn't know you could do that. :roll_eyes:

RoBoTs9999: You have to put the steppers on (HIGH);

I don't understand. It would help others if you explain.

...R

Where I have written, stepper.step(1); you need to replace the 1 with HIGH which makes the stepper turn infinitely :).

RoBoTs9999: Where I have written, stepper.step(1); you need to replace the 1 with HIGH which makes the stepper turn infinitely :).

Strange. As far as I know HIGH and 1 are the same thing.

...R

When you write .step(), the number in the brackets is how many steps the motor will take. But if you write digitalWrite() with a normal motor, 1 and HIGH are the same.

RoBoTs9999:
When you write .step(), the number in the brackets is how many steps the motor will take. But if you write digitalWrite() with a normal motor, 1 and HIGH are the same.

So if you write .step(HIGH) how many steps does the compiler insert in place of HIGH?

…R

Infinite

Robin2:

RoBoTs9999:
When you write .step(), the number in the brackets is how many steps the motor will take. But if you write digitalWrite() with a normal motor, 1 and HIGH are the same.

So if you write .step(HIGH) how many steps does the compiler insert in place of HIGH?

…R

LOW is #defined as 0
HIGH is #defined as 1

MarkT: LOW is #defined as 0 HIGH is #defined as 1

@RoBoTs9999 seems to have a different opinion.

...R