stepper

hi i have a stepper nema 17 with the a4988 driver now i use a simple forward and revers motion that will start and stop at a set position but when i power down and back on it starts at a new position if i put a switch just after where it stops i would like it to then reverse back to its first start point but when i put that part of the program in i get errors can someone help please

mel: but when i put that part of the program in i get errors can someone help please

We can't see your PC screen from way over here.

Post the program and post the error messages.

...R Stepper Motor Basics Simple Stepper Code

ok im new to this i have attached a screen shot of what i was trying to do i was getting the motor to go forward to the switch and detect an input and reverse back to it first point i was going to pu an if statement after the for loop. i dont no if you can see the attachment please let me no thanks

stepper fault 1.JPG

ok i tried to move the motor to a switch so i can detect an input and the reverse motor to the original position

Images from Replies #2 and #3 so we don’t have to download them. See this Image Guide

088ed7529f57ee4888ba6df40b2a25910f17d4ea.jpg

3ab7015ac12f43bf6374a4b1a2ce19ae9073c246.jpg

…R

Don’t post program code as pictures. Post it as text so we can copy it to a text editor or try to compile it ourselves. 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

Do the same for the error messages.

…R

ok i have sorted that part but its what to do with the if statement as i only want this to run once then run the loop
const int stepPin = 3;
const int dirPin = 4;
void setup() {
pinMode(3,OUTPUT);
pinMode(4,OUTPUT);
pinMode(5,INPUT);
digitalWrite(dirPin,HIGH);
for(int x = 0; x < 200; x++) {
digitalWrite(stepPin,HIGH);
delay(10);
digitalWrite(stepPin,LOW);
delay(10);
if
}

}
void loop(){

digitalWrite(dirPin,HIGH);

for(int x = 0; x < 200; x++) {
digitalWrite(stepPin,HIGH);
delay(10);
digitalWrite(stepPin,LOW);
delay(10);
}
delay(5000);

digitalWrite(dirPin,LOW);

for(int x = 0; x < 400; x++) {
digitalWrite(stepPin,HIGH);
delayMicroseconds(500);
digitalWrite(stepPin,LOW);
delayMicroseconds(500);
}
delay(1000);
}

stepper fault 3.JPG

mel: ok i have sorted that part but its what to do with ...

You don't seem to have read my Reply #5 carefully. I don't see any use of the code button and I do see a link for a picture.

You don't seem to have read my Reply #4 carefully as I can only see a link to the picture and not the picture.

Help us to help you.

...R

i posted the text in 6 can you see it or am i doing that wrong im sorry if i dont get this i had a stroke and it slowed me down so it takes me a time now

Your posted code should look like this

const int stepPin = 3;
const int dirPin = 4;
void setup() {
  pinMode(3,OUTPUT);
  pinMode(4,OUTPUT);
pinMode(5,INPUT);
digitalWrite(dirPin,HIGH);
for(int x = 0; x < 200; x++) {
   digitalWrite(stepPin,HIGH);
    delay(10);
    digitalWrite(stepPin,LOW);
    delay(10);
    if
}

}
void loop(){
 

  digitalWrite(dirPin,HIGH);

  for(int x = 0; x < 200; x++) {
    digitalWrite(stepPin,HIGH);
    delay(10);
    digitalWrite(stepPin,LOW);
    delay(10);
      }
  delay(5000);

  digitalWrite(dirPin,LOW);

 for(int x = 0; x < 400; x++) {
    digitalWrite(stepPin,HIGH);
    delayMicroseconds(500);
   digitalWrite(stepPin,LOW);
    delayMicroseconds(500);
  }
  delay(1000);
}

And you should post the error messages in the same way. Just copy and paste from the Arduino IDE

Also the full error message will include the line number where it has found the error

…R

You seem to have a stray IF on line 14.

If you use the AutoFormat tool it makes it easier to see that sort of problem.

...R

I'm guessing that you want to home the stepper in setup and that pin 5 has a microswitch attached to it to help detect that and you were looking for help with the if to detect that you're home. If so, this is probably close to how setup should look:

const byte stepPin = 3;
const byte dirPin = 4;
const byte switchPin = 5;
void setup() 
{
pinMode(stepPin,OUTPUT);
pinMode(dirPin,OUTPUT);
pinMode(switchPin,INPUT_PULLUP);
digitalWrite(dirPin,HIGH);
while(digitalRead(switchPin)==HIGH)
  {
  digitalWrite(stepPin,HIGH);
  delay(10);
  digitalWrite(stepPin,LOW);
  delay(10);
  }
}

ok yes this is what i want thanks for your help i will get better at posting thanks