Reversing Stepper Motor with Delay

Hey guys,

So, I don't know a lot but I know I've got quite an education reading this forum and I'm grateful for it.

I'm trying to figure out how to insert a delay in this code so when the switch is tripped, the motor will pause before starting in the opposite direction. Right now, the gantry bounces between the switches (which is fun but I need a delay at the end of each stroke).

I have my Arduino Uno and Big Easy Driver connected to a Mercury Motor SM-42BYG011-25 with a Step Angle 1.8*, 12V, 2Phase, .33A.

#define stepPin 2
#define dirPin 3
#define pinLimitswitchLeft 4 // these can be either left or right depending on motor wiring and dir pin.
#define pinLimitswitchRight 5 // these switches must be N.O. types
int stepDelay = 100; // this is the step delay in microseconds. Reduce this to speed up motor rotation and vice versa.

void setup()
{
pinMode (stepPin, OUTPUT);
pinMode (dirPin, OUTPUT);
pinMode (pinLimitswitchLeft, INPUT_PULLUP);
pinMode (pinLimitswitchRight, INPUT_PULLUP);
}

void loop()
{
digitalWrite(dirPin, HIGH); // set direction pin, the direction that the motor will rotate is dictated by it's wiring
while (digitalRead(pinLimitswitchLeft) == HIGH) // move motor until left limit switch is pressed.
{
stepMotor();
}

digitalWrite(dirPin, LOW); // set direction pin, the direction that the motor will rotate is dictated by it's wiring
while (digitalRead(pinLimitswitchRight) == HIGH) // move motor until right limit switch is pressed.
{
stepMotor();
}

}

void stepMotor()
{
digitalWrite(stepPin, HIGH);
delayMicroseconds(stepDelay);
digitalWrite(stepPin, LOW);
delayMicroseconds(stepDelay);
}

Please highlight your code and click the CODE toolbutton ("<>" , just to the left of QUOTES button)

If this is all the code does, then insert delays before each new action.

As @bluejets says for a very simple program it would be sufficient to put a delay() between the two movements.

However if you are looking ahead to a more complex program you should see how to use millis() to manage time as illustrated in the demo several things at a time. And the second example in this simple stepper demo shows how to use millis() to control the motor timing.

...R

Thanks guys. I'll give it a try.