HI ALL I Have Issues on my program I have 1 Stepper Motor Kit Nema 34 8.0Nm Stepper Motor and 2 buttons 2 limit switches

#define LIMIT_SWITCH_1 9
#define LIMIT_SWITCH_2 10
#define SWITCH_1 11
#define SWITCH_2 12
#define DIR_PIN 6
#define EN_PIN 5
#define PUL_PIN 7

#define WAITING_TIME 2000

void setup() {
pinMode(DIR_PIN, OUTPUT);
pinMode(EN_PIN, OUTPUT);
pinMode(PUL_PIN, OUTPUT);
pinMode(LIMIT_SWITCH_1, INPUT_PULLUP);
pinMode(LIMIT_SWITCH_2, INPUT_PULLUP);
pinMode(SWITCH_1, INPUT_PULLUP);
pinMode(SWITCH_2, INPUT_PULLUP);
digitalWrite(EN_PIN, LOW);
}

void movement_clockwise() {
digitalWrite(DIR_PIN, HIGH); // SET DIRECTION
for (int i = 0; i < 10000; i++) {
digitalWrite(PUL_PIN, HIGH);
delayMicroseconds(500);
digitalWrite(PUL_PIN, LOW);
delayMicroseconds(500);

// Check the limit switch during movement
if (digitalRead(LIMIT_SWITCH_1) == LOW) {
 delay(WAITING_TIME);
  break; // Exit loop if the limit switch is triggered
}
 if (digitalRead(SWITCH_1) == HIGH) {
 delay(WAITING_TIME);

}
}
}
void movement_counterclockwise() {
digitalWrite(DIR_PIN, LOW); // SET DIRECTION
for (int i = 0; i < 10000; i++) {
digitalWrite(PUL_PIN, HIGH);
delayMicroseconds(500);
digitalWrite(PUL_PIN, LOW);
delayMicroseconds(500);

// Check the limit switch during movement
if (digitalRead(LIMIT_SWITCH_2) == LOW) {
  delay(WAITING_TIME);
  break; // Exit loop if the limit switch is triggered
}
  if (digitalRead(SWITCH_2) == HIGH) {
 delay(WAITING_TIME);

}
}
}

void movement_handler() {
if (digitalRead(LIMIT_SWITCH_1) == LOW) { movement_counterclockwise();}
else if (digitalRead(LIMIT_SWITCH_2) == LOW) { movement_clockwise();}
else {movement_clockwise();} // Default movement (e.g., clockwise)

}

void loop() {
movement_handler();
}


Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the < CODE/ > icon above the compose window) to make it easier to read and copy for examination

Please post your full sketch, using code tags when you do

Posting your code using code tags prevents parts of it being interpreted as HTML coding and makes it easier to copy for examination

In my experience the easiest way to tidy up the code and add the code tags is as follows

Start by tidying up your code by using Tools/Auto Format in the IDE to make it easier to read. Then use Edit/Copy for Forum and paste what was copied in a new reply. Code tags will have been added to the code to make it easy to read in the forum thus making it easier to provide help.

It is also helpful to post error messages in code tags as it makes it easier to scroll through them and copy them for examination

You have not said what your issues are

Your other topic on the same subject deleted.

Please do not duplicate your questions as doing so wastes the time and effort of the volunteers trying to help you as they are then answering the same thing in different places.

Please create one topic only for your question and choose the forum category carefully. If you have multiple questions about the same project then please ask your questions in the one topic as the answers to one question provide useful context for the others, and also you won’t have to keep explaining your project repeatedly.

Repeated duplicate posting could result in a temporary or permanent ban from the forum.

Could you take a few moments to Learn How To Use The Forum

It will help you get the best out of the forum in the future.

Thank you.




#define LIMIT_SWITCH_1     9
#define LIMIT_SWITCH_2     10
#define SWITCH_1   11
#define SWITCH_2    12
#define DIR_PIN            6
#define EN_PIN             5
#define PUL_PIN            7

#define WAITING_TIME  2000

void setup() {
  pinMode(DIR_PIN, OUTPUT);
  pinMode(EN_PIN, OUTPUT);
  pinMode(PUL_PIN, OUTPUT);
  pinMode(LIMIT_SWITCH_1, INPUT_PULLUP);
  pinMode(LIMIT_SWITCH_2, INPUT_PULLUP);
  pinMode(SWITCH_1, INPUT_PULLUP);
  pinMode(SWITCH_2, INPUT_PULLUP);
  digitalWrite(EN_PIN, LOW);
}

void movement_clockwise() {
  digitalWrite(DIR_PIN, HIGH); // SET DIRECTION
  for (int i = 0; i < 10000; i++) {
    digitalWrite(PUL_PIN, HIGH);
    delayMicroseconds(500);
    digitalWrite(PUL_PIN, LOW);
    delayMicroseconds(500);

    // Check the limit switch during movement
    if (digitalRead(LIMIT_SWITCH_1) == LOW) {
     delay(WAITING_TIME);
      break; // Exit loop if the limit switch is triggered
    }
     if (digitalRead(SWITCH_1) == HIGH) {
     delay(WAITING_TIME);
  }
}
}
void movement_counterclockwise() {
  digitalWrite(DIR_PIN, LOW); // SET DIRECTION
  for (int i = 0; i < 10000; i++) {
    digitalWrite(PUL_PIN, HIGH);
    delayMicroseconds(500);
    digitalWrite(PUL_PIN, LOW);
    delayMicroseconds(500);

    // Check the limit switch during movement
    if (digitalRead(LIMIT_SWITCH_2) == LOW) {
      delay(WAITING_TIME);
      break; // Exit loop if the limit switch is triggered
    }
      if (digitalRead(SWITCH_2) == HIGH) {
     delay(WAITING_TIME);
  }
  }
}

void movement_handler() {
       if (digitalRead(LIMIT_SWITCH_1) == LOW) { movement_counterclockwise();}
  else if (digitalRead(LIMIT_SWITCH_2) == LOW) { movement_clockwise();} 
  else    {movement_clockwise();}     // Default movement (e.g., clockwise)   
  
}

void loop() {
  movement_handler();
}


You managed to post using code tags but have not described your issues ?

What is the code meant to do ?
What does it actually do ?

I have issues with your post !

No schematics or block diagram, what’s the problem you’re having ?

Better questions get better answers.

I Have 2 buttons 2 limitswitches

ISSUE : The Switch is not responding when i press

when button 1 is turn on the motor will move clockwise ,ones it reaches the limit switch 1 it will stop .Continue loop

when you press the button 2 the motor will move counter clockwise ,ones it reaches to the limit switch 2 it will stop. Continue loop


/////FINALPROGRAM ELEVATOR CONTROL 2C LIMIT SWITCHS...

#define LIMIT_SWITCH_1     9
#define LIMIT_SWITCH_2     10
#define SWITCH_1   11
#define SWITCH_2    12
#define DIR_PIN            6
#define EN_PIN             5
#define PUL_PIN            7

#define WAITING_TIME  2000

void setup() {
  pinMode(DIR_PIN, OUTPUT);
  pinMode(EN_PIN, OUTPUT);
  pinMode(PUL_PIN, OUTPUT);
  pinMode(LIMIT_SWITCH_1, INPUT_PULLUP);
  pinMode(LIMIT_SWITCH_2, INPUT_PULLUP);
  pinMode(SWITCH_1, INPUT_PULLUP);
  pinMode(SWITCH_2, INPUT_PULLUP);
  digitalWrite(EN_PIN, LOW);
}

void movement_clockwise() {
  digitalWrite(DIR_PIN, HIGH); // SET DIRECTION
  for (int i = 0; i < 10000; i++) {
    digitalWrite(PUL_PIN, HIGH);
    delayMicroseconds(500);
    digitalWrite(PUL_PIN, LOW);
    delayMicroseconds(500);

    // Check the limit switch during movement
    if (digitalRead(LIMIT_SWITCH_1) == LOW) {
     delay(WAITING_TIME);
      break; // Exit loop if the limit switch is triggered
    }
     if (digitalRead(SWITCH_1) == HIGH) {
     delay(WAITING_TIME);
  }
}
}
void movement_counterclockwise() {
  digitalWrite(DIR_PIN, LOW); // SET DIRECTION
  for (int i = 0; i < 10000; i++) {
    digitalWrite(PUL_PIN, HIGH);
    delayMicroseconds(500);
    digitalWrite(PUL_PIN, LOW);
    delayMicroseconds(500);

    // Check the limit switch during movement
    if (digitalRead(LIMIT_SWITCH_2) == LOW) {
      delay(WAITING_TIME);
      break; // Exit loop if the limit switch is triggered
    }
      if (digitalRead(SWITCH_2) == HIGH) {
     delay(WAITING_TIME);
  }
  }
}

void movement_handler() {
       if (digitalRead(LIMIT_SWITCH_1) == LOW) { movement_counterclockwise();}
  else if (digitalRead(LIMIT_SWITCH_2) == LOW) { movement_clockwise();} 
  else    {movement_clockwise();}     // Default movement (e.g., clockwise)   
  
}

void loop() {
  movement_handler();
}


We need to see a circuit diagram and I don't mean a 'photo of a mess of wires

Please draw the circuit, using pencil and paper is good enough, showing how all components are connected, pin numbers and how the project is powered. Upload a photo of the drawing


Debug using serial monitor; print out the status of your buttons and limit switches and see if the do what they are supposed to do.

You can write a simple test program that does not involve the stepper to test buttons and limit switches.

Hi, @wire12

What is with using pin 5 as enable, but not using the enable terminals on the stepper driver?

Tom.... :smiley: :+1: :coffee: :australia:

i connect already but still not working

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