# Stepper Motor reverse direction

Trying to make my stepper motor reverse direction after 1000 steps.

Using Easy Driver with Pin 8 from Arduino UNO to the DIR pin of the Easy Driver and Pin 9 from UNO to the STEP Pin of Easy Driver.

I initialize StepCounter as integer and set to zero, set direction Pin 8 to HIGH.

With StepCounter less than 1000 I expect the direction to be one way per digitalWrite(8, LOW);
After StepCounter is greater than or equal to 1000 I want direction reversed with digitalWrite(8, HIGH);

In fact the stepper motor turns one direction only and never reverses.

Code compiles, but obviously I am missing something in the logic? Motor does not reverse direction.

my code:

#define DISTANCE 5200

int StepCounter = 0;
int Stepping = false;

void setup() {
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
digitalWrite(8, LOW);
digitalWrite(9, LOW);
}

void loop() {
if (StepCounter < 1000 && Stepping == false)
{
digitalWrite(8, LOW);
Stepping = true;
}
if (StepCounter >= 1000 && Stepping == false)
{
digitalWrite(8, HIGH);
Stepping = true;
}

if (Stepping == true)
{
digitalWrite(9, HIGH);
delay(1);
digitalWrite(9, LOW);
delay(1);

StepCounter = StepCounter + 1;
Serial.begin(9600);
Serial.println(StepCounter);

if (StepCounter == DISTANCE)
{
StepCounter = 0;
Stepping = false;
}
}
}

Any help much appreciated

Ok first of all move the serial.begin to setup.

StepCounter = StepCounter + 1;
=> Serial.begin(9600);
Serial.println(StepCounter);

if (StepCounter == DISTANCE)
{
StepCounter = 0;
Stepping = false;
}
}
}

Next when StepCounter == DISTANCE you reset StepCounter back to 0 so this line resets the direction back to the original direction canceling the reverse rotation.

if (StepCounter < 1000 && Stepping == false)
{
digitalWrite(8, LOW);
Stepping = true;

``````so your code looks like this