stepper motor direction doesnt work

Hey guys for the past 3 days I have been working on the stepper motor. The only thing I dont understand is the direction part of the stepper motor. This is how my code works in short. “+500” makes the stepper move in the clockwise direction 500 steps. “-500” makes the stepper move in the counter clockwise direction 500 steps. Now my problem is this the stepper recognizes the correct steps but fails to get the direction right. So if I input +500 twice first it will move clockwise then it will go counterclockwise. I have tried many ways and have run out off any means to correct this. Any help regarding this would be highly appreciated. Thank you.

here is my code

int dirpin = 7;
int steppin = 11;

void setup()
{
Serial.begin(9600);
pinMode(dirpin, OUTPUT);
pinMode(steppin, OUTPUT);
}//end setup

int i;
int val;
int steps;
char direc;
int state;

void loop()
{

if( Serial.available())
{
direc = Serial.read();

if( direc == ‘+’)
{
Serial.println(" Inside direc == + ");

//Set motor direction to clockwise
digitalWrite(dirpin, LOW); // direction
delay(200);
state = digitalRead(7);
Serial.println(state);
Serial.println(" Direction is clockwise! ");
Serial.println(direc);
steps = Serial.read();
steps -= 48;

while( Serial.available() )
{
val = Serial.read();
val -= 48;
steps = (steps * 10 + val);
}//end while

Serial.println(steps);
Serial.println(" ----------------------- " );

for( i = 0; i < steps; i++)
{
digitalWrite(steppin, LOW);
delayMicroseconds(200);
digitalWrite(steppin, HIGH);
delayMicroseconds(200);
}//end for
}// end if
digitalWrite( dirpin , HIGH);
if ( direc == ‘-’)
{
Serial.println(" Inside direc == - ");

//Set motor direction to clockwise
Serial.println(" Direction is COUNTERclockwise! ");

digitalWrite( dirpin , LOW); // opposite direction
delay(200);
state = digitalRead(7);
Serial.println(state);

steps = Serial.read();
steps -= 48;

while( Serial.available() )
{
val = Serial.read();
val -= 48;
steps = (steps * 10 + val);
}//end while
Serial.println(" “);
Serial.println(steps);
Serial.println(” ----------------------- " );

for( i = 0; i < steps; i++)
{
digitalWrite(steppin, LOW);
delayMicroseconds(200);
digitalWrite(steppin, HIGH);
delayMicroseconds(200);
}//end for
}

}
}//end void loop

It may be easier to fix it if you separate the stepping logic from the serial logic. If you had a function that you call with the direction and the number of steps it should be easier to test and get working. It would also simplify adding any future functionality. I think there was an example of how to do a stepping function in your other thread.

Hey mem thank you for replying. yes you gave me a sample code in the other post. I just did not want to call a function because it was getting complicated for me. So I wrote up my code. I just dont understand what happens if digitalWrite(dirpin , LOW) and digitalWrite( dirpin , HIGH). can you please tell me if each call to the dirpin is to change the directions?.. confused on this part.

Yes, the datasheet you linked in the other thread says that the step pin controls the step direction. Having a separate function to control the motor should make your code easier rather than more difficult. For example, you could test the two parts of your sketch (the serial receiver and the motor controller) separately. When you have both of them working you can put them together.