Arduino stepper motor control

Hi guys, I have an Arduino Uno and stepper motor. i want to be able to step the motor clockwise until it hits the limit switch then steps anticlockwise for specific steps. i had wrote a code that you can see below but it doesn’t work. if you guys help me i would be Grateful.

#define CONT_PIN 7
#define Limit01 2
#define step_pin 9
#define dir_pin 8
#define DISTANCE 3200
int StepCounter = 0;
int Stepping = false;
void setup() {
pinMode(dir_pin, OUTPUT);
pinMode(step_pin, OUTPUT);
pinMode(Limit01, INPUT);
pinMode(CONT_PIN, INPUT_PULLUP);

}

void loop() {
if (digitalRead(CONT_PIN) == 0 && Stepping == false) {
if (digitalRead(Limit01) == 0) {
digitalWrite(dir_pin, HIGH);
Stepping == false;
delay(500);
Stepping == true;
StepCounter = StepCounter + 1;
if (StepCounter == DISTANCE){
StepCounter = 0;
Stepping = false;
}
}

else {
digitalWrite(dir_pin, LOW);
Stepping == true;
if (Stepping == true){
digitalWrite(9, HIGH);
delay(10);
digitalWrite(9, LOW);
delay(10);
}
}
}
}

sketch_jun23a.ino (880 Bytes)

        Stepping == false;
        delay(500);
        Stepping == true;

These statements compare Stepping with false then true with a half second delay between the comparisons and throws away the answers. I don't think that is what you meant to do or if you did I don't see why.

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

We need to see how you have connected the stepper and the switch, also how you are powering the arduino and the stepper.
Also a link to the specs on your stepper motor.

Thanks.. Tom.. :slight_smile:

no i just want, after it reaches the limit switch stop and then after half second ,start in reverse direction for specific steps . how can i correct the code?

my stepper motor has 200 steps and im powering arduino 5v adaptor.

  1. pls do what @TomGeorge said:
    we need a schematic to see if your sketch is "compatible" with your wiring.

  2. did you understand what @UKHeliBob asked?

and:
3. -> what is CONT_PIN 7 doing?

I believe this is a problem of thinking rather than coding.

The usual way to get to a limit switch is like this pseudo code

move 1 step towards switch
check switch
are we there yet?
   if not
     repeat
   if yes
    save the value of millis()

and the usual way to do nothing for an interval is
repeatedly compare the latest value of millis() with the saved value

if (millis() - savedMillis >= interval) {
    // time to go to work
}

see the demo Several Things at a Time

...R
Stepper Motor Basics
Simple Stepper Code