can't reverse stepper motor

I’m using an Arduino Micro with an easystepper driver and accelStepper software calls. This is my first stepper motor project so I’m taking it one step at a time and so far haven’t gotten past that first step. I have the motor set up to actuate a micro switch at the home position. I call a stepperHome() routine to rotate clockwise until the switch goes high. It is supposed to reverse and go back to a preset position. I can rotate it down and it does trigger the switch but the next section of code, though executed does not move the motor CCW as expected, The digital write to pin 8 turns on an LED to let me know the code was executed. What am I doing wrong on I thought this would rotate back to where it started since the previous operation was a stepper.moveTo(500)?

stepper.moveTo(-500); //rotate counter clockwise to measurement position 2;


  • ReedMeasurementArduino.ino
  • Created: 8/1/2014 6:28:56 PM
  • Author: jrdarrah
    #include <AccelStepper.h>
    int state = 0; //indicate state device is in 0 = initializing, 1 = home position found, 2 = ready to measure (below reed), 3 ready to measure (above reeed)
    int home = 6; //home position sensor is on pin 6
    int extPwr = 7; //external (12V) power on input is pin 7
    int pos = 0; //stepper position
    int step = 4;
    int dir = 5;
    char incomingByte;
    AccelStepper stepper (1,step,dir); //1 = 2 wired, pin 4 is step, pin 5 is for direction

void setup()
Serial.begin(9600); //talk to the windows app at 9600 baud via serial over USB
//wait until external power is applied so we don’t try anything with just the USB port connected
while(digitalRead(extPwr) ==LOW)
//rotate stepper motor down until the home position pin goes high

void loop()

void stepperHome()
byte hBval;
hBval = digitalRead(home);
while (hBval == HIGH)
stepper.moveTo(500); //positive is clockwise
hBval = digitalRead(home);
stepper.moveTo(-500); //rotate counter clockwise to measurement position 2;
stepper.setCurrentPosition(0); //zero point for rest of the measurement
state = 1; //indicate position below reed

First, see how much easier it is to read code when it is posted within code tags - the # button.

Second, notice how I have split this piece of code into two sections. Now look at the difference between the first section which works and the second section which doesn't. The key difference is the WHILE loop and the command within it.

As things stand the second piece of code runs once - which lights your led - but it doesn't even call once so, naturally, the motor doesn't move. You need code that calls often enough so that the motor can move to its destination. And it may not move with every call to if the motor speed is low and a step on every call would make it move too quickly.

  while (hBval == HIGH)
      stepper.moveTo(500);            //positive is clockwise
      hBval = digitalRead(home);

      stepper.moveTo(-500);         //rotate counter clockwise to measurement position 2;
      stepper.setCurrentPosition(0);         //zero point for rest of the measurement
      state = 1;                        //indicate position below reed


I'll try this again. With Window 7 & explorer 11. Every time I posted it went to a white screen and all was lost. Firefox seems to be a lot more compatible with the Arduino Forum.

Thanks for the info about code tags. This is my first post on this forum and my first sketch. I like to learn from my mistakes so I don't make them twice. The intent of my stepperHome code is to start at position 0 which is set in the setup before the routine is called then rotate CW to 500. The while only works when the home bin is high. I verified it works correctly, starting at high and only goes low when a paddle on the end of the stepper contact a microswitch.

The code below the while is executed once to rotate the motor CCW to a starting point for my operation. In my code below the moveTo(500) & run seem to work. The motor does rotate CW and moves far enough to contact the switch. The code below the while must be executed since the LED attached to pin 8 lights. I've tried adding a delay of 1 sec before starting the CCW move. Still nothing happens. The direction pin never goes low and the motor doesn't turn CCW. I know that the dir pin works since I can see it start low until the motor moves CW at which point it goes high.

How can I make sure the CW move is completely finished ?

If I start at 0, moveTo(500). What is the proper moveTo to rotate CCW back to where I started when stepperHome() was called?

As Robin suggested, you need to call run() many times. Your first run() is contained within a while() loop, while the second one is not.

Basically, the second block wants to look like :

stepper.moveTo(-500);                           // SETS THE TARGET TO 500 step CCW from measurement position 2
while (stepper.distanceToGo()>0){     // until you get to your target.....;                                        // calculate if a step is needed, and take it.

There’s a bit of a shorthand for this, which is

stepper.moveTo(-500);         //set target
      while(;         // this will return true until it gets to the target,

thanks for setting me on the right path. I'll do some studying on the accelStepper code which will help me understand why it does what it does.