Hello Friends pls i need to move my stepper motor forward and backward

Please i need arduino code for my stepper motor that when an input signal is been sent to the arduino to initaite the stepper motor start revolving or moving using the A4899 or the easy driver board and when it reaches the other endstop it reverser in the other direction which is coming from and when it goes to the other endstop it finally stop and exit loop

This is what i wrote early but i can not write more than one if statement in the loop please how can i write a code that will able to consider three if statement then finally exit when it is done…Thanks for your help

int Distance = 0;
const int analogPin = A0;
const int dirpin = 8;
const int steppin = 9;
const int threshold = 100;
const int outputsignal=7;
void setup(){
Serial.begin(9600);
pinMode(dirpin, OUTPUT);
pinMode(steppin, OUTPUT);
pinMode(outputsignal,OUTPUT);
}
void loop(){
int analogValue = analogRead(analogPin);

if (analogValue>threshold){

int i;

digitalWrite(dirpin, LOW); // Set the direction.
delay(10000);
{digitalWrite(outputsignal,HIGH);
delay(10000);
digitalWrite(outputsignal,LOW);
delay(10000);}
for (i = 0; i<100000; i++) // Iterate for 4000 microsteps.
{
digitalWrite(steppin, LOW); // This LOW to HIGH change is what creates the
digitalWrite(steppin, HIGH); // “Rising Edge” so the easydriver knows to when to step.
delayMicroseconds(500); // This delay time is close to top speed for this
} // particular motor. Any faster the motor stalls.

digitalWrite(dirpin, HIGH); // Change directiofor (i = 0; i<10000; i++) // Iterate for 4000 microsteps
{
digitalWrite(steppin, LOW); // This LOW to HIGH change is what creates the
digitalWrite(steppin, HIGH); // “Rising Edge” so the easydriver knows to when to step.
delayMicroseconds(500); // This delay time is close to top speed for this
} // particular motor. Any faster the motor stalls.
exit (0);

}
}

fally_doodle, there are many Arduino tutorials that have tried and tested, for example

https://www.arduino.cc/en/Tutorial/StepperSpeedControl

Have fun, Ken

@fally_doodle, Please modify your post and use the code button </>

so your code looks like this

and is easy to copy to a text editor. See How to use the Forum

You have not said whether your motor moves at all. This Simple Stepper Code works with an A4988 stepper driver.

For reversing direction when you reach an end-stop you need code that detects the switch and changes the value of a direction variable. Something like

leftEndPinState = digitalRead(leftEndPin);
rightEndPinState = digitalRead(rightEndPin);

if (leftEndPinState == LOW) { // assumes LOW == pressed
   direction = 'R';
}
if (rightEndPinState == LOW) {
   direction = 'L';
}

…R

leftEndPinState = digitalRead(leftEndPin); rightEndPinState = digitalRead(rightEndPin);

if (leftEndPinState == LOW) { // assumes LOW == pressed direction = 'R'; } if (rightEndPinState == LOW) { direction = 'L'; }

Thanks for the code i really appreciate but i want to ask a question about the code how will arduino know that R is right and L is left movement..Thanks

fally_doodle:
how will arduino know that R is right and L is left movement…Thanks

You will tell it (in the other code that you write).

And please use the code button </> so your code looks like this and is easy to copy to a text editor

…R