Starting time delay between two stepper motors

hey, guys thanks for your support I have some issues with my code I was controlling a 2stepper motor with a limit switch but I want a delay between the two motors starting when one of the limit switches is pressed wait for the delay and start am trying to insert the delay function but the stepper step per revolution is decreased how can I fix this code.

// Declare Stepper Motor used pins
#define dirPin1  12 // stepper one
#define stepPin1 13

#define stepPin2 11 // stepper two
#define dirPin2 10

const int limit1 = 23;// limit switch of cover open
const int limit2 = 24;// limit switch of cover close


void setup() {
  // Set up stepper motor
  pinMode(stepPin1, OUTPUT);
  pinMode(dirPin1, OUTPUT);

  //Set up 2 limit switches as input pull-down
  pinMode(limit1 , INPUT);
  pinMode(limit2 , INPUT);

}
void loop()
{

  if (digitalRead(limit1) == LOW) // when limit switch one is not pressed
  {
    digitalWrite(dirPin1, HIGH); // stepper motor turns clock wise
    digitalWrite(stepPin1, HIGH);
    delayMicroseconds(1000);
    digitalWrite(stepPin1, LOW);
  }
  else {
    digitalRead (limit1 == HIGH); // when the limit switch one is pressed
    digitalWrite(stepPin1, LOW); // turn off stepper motor one
  }

  if (digitalRead(limit1) == HIGH) // when limit one is pressed
  {
    if (digitalRead(limit2) == LOW) // when limit two is not pressed
    {

      digitalWrite(dirPin2, LOW); //  stepper motor two turn anti clock wise
      digitalWrite(stepPin2 , HIGH);
      delayMicroseconds(1000);
      digitalWrite(stepPin2 , LOW);

    }
  }
  else {
    digitalRead(limit1 == LOW); // when limit one is pressed
    digitalRead(limit2 == HIGH); // when limit two is pressed
    digitalWrite(stepPin2, LOW); // turn off stepper two
  

Hi @davetesfaye

well done posting your code as a code-section.
Though starting a second thread on the same issue is against forum-rules.
You should report your cross-posting to a moderator that the moderator is moving this posting to your other thread.

For future postings you should take more care of a precise description of the wanted functionality that names all details explicit

as an example for such a detailed explicit naming all details description:
If I understand right you want:
stepper-motor A run until limit-switch A for stepper-motor A is pressed
if limit-switch A is pressed then stop motor

wait some time (how long?)

after waiting
start running stepper-motor B until limit-switch B is pressed
if limit-switch B is pressed then stop stepper-motor B

best regards Stefan

  • it's common to have a motor move in one direction until it hits a limit switch and then stop and/or change direction until it hits a limit switch in the opposite direction.

so what does the 2nd motor do?

  • why don't you configure the pins for the 2nd motor?
  • i don't see "dirPin1" set to anything other than HIGH. does it need to be set LOW when one of the limit switches becomes active?

shouldn't there be a delay after setting the pin LOW?

  • limit1 == HIGH will be either HIGH/LOW. digitalRead() takes a pin number.

did you mean (else would be limit1 is HIGH)

  else  {
    digitalWrite(stepPin1, LOW); // turn off stepper motor one
  }
  • couldn't these 2 cases be combined?
  • similar to above

Thanks, Stefan for your help
you are right that is what I want to do waiting time 50Minute.

  • what triggers the moving of stepper motor A?

  • what action causes either motor to return to it's original position?

  • since these are stepper motors, why not run them a specific # of steps instead of stopping them with limit switches?

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.