# 2 stepper motor

**URL:** <https://forum.arduino.cc/t/2-stepper-motor/521596>\
**Category:** Motors, Mechanics, Power and CNC\
**Created:** [April 21, 2018, 7:35am UTC](https://forum.arduino.cc/t/2-stepper-motor/521596 "2018-04-21T07:35:20Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![sabar](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/sabar/32/219065_2.png) [@sabar](https://forum.arduino.cc/u/sabar)\
**Post date:** [April 21, 2018, 7:35am UTC](https://forum.arduino.cc/t/2-stepper-motor/521596/1 "2018-04-21T07:35:20Z")

</div>

please help me. how to use 2 stepper motors, the motor 1 forward and then the motor 2 forward and then stop.

this is the code that im using.

```cpp
#include <AccelStepper.h>

AccelStepper stepper1(AccelStepper::DRIVER, 2, 5);
AccelStepper stepper2(AccelStepper::DRIVER, 4, 7);

void setup()
{
Serial.begin(9600);
stepper1.setMaxSpeed(3000);
stepper1.setAcceleration(1000);
stepper2.setMaxSpeed(3000);
stepper2.setAcceleration(1000);
}

void loop()
{
if (Serial.available() > 0) {
    char a = Serial.read();
switch(a)
{
  case '0':{
    stepper1.moveTo(2000);
    break;
  }
  case '1':{
    stepper2.moveTo(1000);
    break;
  }
  default : {
    // default is optional
 }
}
{
  stepper1.run();
  stepper2.run();
}
}
}

```

but this code don't work, there may be something wrong

thank you

---

<div class="post-metadata">

**Author:** ![Robin2](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/robin2/32/216045_2.png) [@Robin2](https://forum.arduino.cc/u/Robin2)\
**Post date:** [April 21, 2018, 8:07am UTC](https://forum.arduino.cc/t/2-stepper-motor/521596/2 "2018-04-21T08:07:40Z")

</div>

Try moving these two lines

```cpp
		  stepper1.run();
		  stepper2.run();

```

out of the IF clause and just put them as the last two lines in loop()

Those lines need to be called all the time, not just when data is received.

Your code will be much easier to read if you use the AutoFormat tool to indent it consistently.

...R  
[Stepper Motor Basics](http://forum.arduino.cc/index.php?topic=284828.0)  
[Simple Stepper Code](http://forum.arduino.cc/index.php?topic=277692.0)

---

<div class="post-metadata">

**Author:** ![TomGeorge](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/tomgeorge/32/215261_2.png) [@TomGeorge](https://forum.arduino.cc/u/TomGeorge)\
**Post date:** [April 21, 2018, 1:52pm UTC](https://forum.arduino.cc/t/2-stepper-motor/521596/3 "2018-04-21T13:52:07Z")

</div>

Hi  
Is this related to this old thread?

[http://forum.arduino.cc/index.php?topic=286652.0](http://forum.arduino.cc/index.php?topic=286652.0)

Tom... 🙂

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [May 6, 2021, 9:47am UTC](https://forum.arduino.cc/t/2-stepper-motor/521596/4 "2021-05-06T09:47:51Z")

</div>

Closed on May 6, 2021, 9:47am UTC


