Moteur Stepper 2limits switchs and arduino uno

Hello everyboby thank you very much for your help
i am french newbie sorry for my broken English

i have two bipolar steppeer nema 34 and 2 chinese driver long with entrance DIR and PUL and arduino uno
for that no problem
at the begining i use accel stepper librairy with the two moteur (working in same time)

DualMoteurShield

stepper 1 i want running only clock wise for that i disconnect the wire DIR
(its working idon’t know if is good?)

stepper2 running forward and backward for 9500steps i adjust the nut on the middle of the
screw it ok its working right
but when i switch off and i restart the moteur someting run clock wise and sometime counter clock wise and with 9500step and he goes out off the screw

for that raison i try to use two limit switch ( right and Left) at each textremity off the screw
like that when the nut hit the right switch he go backward still he hit the left switch and he go foward and so and still the powe is on

i find on the internet one sketch

Control Stepper Motor with Limit Switches - Arduino Programming
and i try to mixte the two sketch but i began crazy
thank for all
willyinclude <AccelStepper.h>

// The X Stepper pins
#define STEPPER1_DIR_PIN 3 // stepper 1 CHARIOT AVEC FIN DE COURSE
#define STEPPER1_STEP_PIN 2
// The Y stepper pins

#define STEPPER2_DIR_PIN 7 // stepper 2 ROTATION continue
#define STEPPER2_STEP_PIN 6

const int LimitSwitch_LEFT_Pin = 10;
const int LimitSwitch_RIGHT_Pin = 11;

int leftSw = digitalRead( LimitSwitch_LEFT_Pin);//declaration variable
int rightSw = digitalRead( LimitSwitch_RIGHT_Pin);//declaration variable

// Define some steppers and the pins the will use
AccelStepper stepper1(AccelStepper::DRIVER, STEPPER1_STEP_PIN, STEPPER1_DIR_PIN);

AccelStepper stepper2(AccelStepper::DRIVER, STEPPER2_STEP_PIN, STEPPER2_DIR_PIN);

void setup()
{
Serial.begin(9600);

pinMode(LimitSwitch_LEFT_Pin , INPUT);
pinMode(LimitSwitch_RIGHT_Pin , INPUT);

// Sets the two pins as Outputs
pinMode(STEPPER1_STEP_PIN,OUTPUT);
pinMode(STEPPER1_DIR_PIN,OUTPUT);

// Set Dir to Home switch
digitalWrite(STEPPER1_DIR_PIN,HIGH); // Enables the motor to move in a particular direction
// Set STEP to Home switch
digitalWrite(STEPPER1_STEP_PIN,HIGH); // Enables the motor to move in a particular direction

stepper1.setMaxSpeed(800.0);// translation laterale
stepper1.setAcceleration(200.0);
stepper1.moveTo(5000);

stepper2.setMaxSpeed(800.0);
stepper2.setAcceleration(400.0);
stepper2.moveTo(10000);
}

void loop() {{
stepper2.run();
int leftSw = digitalRead( LimitSwitch_LEFT_Pin);//declaration variable
int rightSw = digitalRead( LimitSwitch_RIGHT_Pin);//declaration variable

if( (leftSw == HIGH && (digitalRead(STEPPER1_DIR_PIN) == HIGH)) ||
(rightSw == HIGH && (digitalRead(STEPPER1_DIR_PIN) == LOW)) ){

AccelStepper stepper1(1);

}
else if( leftSw == LOW && (digitalRead(STEPPER1_DIR_PIN) == HIGH) ){
digitalWrite(STEPPER1_DIR_PIN,LOW);
delay(100);
}
else if( rightSw == LOW && (digitalRead(STEPPER1_DIR_PIN) == LOW ) ){
digitalWrite(STEPPER1_DIR_PIN,HIGH);
delay(100);
}

// if (stepper2.distanceToGo() == 0)
// stepper2.moveTo(stepper2.currentPosition());
stepper2.run();

// Change direction at the limits
// if (stepper1.distanceToGo() == 0)
// stepper1.moveTo(-stepper1.currentPosition());

stepper1.run();

}
}

DualMotorShield_polisseuse_limit_mixt1.ino (2.59 KB)

would you find a small drawing for that i want do

polisseuse.pdf (19.4 KB)

Please post your drawing as a JPG or PNG file so we can see it without downloading it. See this Simple Image Guide

To make it easy for people to help you please modify your post 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

Your code is too long for me to study quickly without copying to a text editor.

Also please use the AutoFormat tool to indent your code for easier reading.

The usual way to use a limit switch with a stepper motor is to run the motor up to the switch when the Arduino starts. That way the Arduino knows where to start counting steps.

…R

Hello everibody sorry for the delai i take time to change the format of the drawing in PNG and i try to use automatic format i hope is the correct format now

[code]

[code]#include <AccelStepper.h>

// The X Stepper pins
#define STEPPER1_DIR_PIN 3 // stepper 1 Rolling trolley

 #define STEPPER1_STEP_PIN 2
// The Y stepper pins

#define STEPPER2_DIR_PIN 7 // stepper 2 ROTATION continue
 #define STEPPER2_STEP_PIN 6

const int  LimitSwitch_LEFT_Pin  = 10;
 const int LimitSwitch_RIGHT_Pin = 11;

int leftSw  = digitalRead( LimitSwitch_LEFT_Pin);//declaration variable
  int rightSw = digitalRead( LimitSwitch_RIGHT_Pin);//declaration variable


// Define some steppers and the pins the will use
 AccelStepper stepper1(AccelStepper::DRIVER, STEPPER1_STEP_PIN, STEPPER1_DIR_PIN);

  AccelStepper stepper2(AccelStepper::DRIVER, STEPPER2_STEP_PIN, STEPPER2_DIR_PIN);

   void setup()
  {
   Serial.begin(9600);

    pinMode(LimitSwitch_LEFT_Pin , INPUT);
   pinMode(LimitSwitch_RIGHT_Pin , INPUT);

  // Sets the two pins as Outputs
      pinMode(STEPPER1_STEP_PIN, OUTPUT);
   pinMode(STEPPER1_DIR_PIN, OUTPUT);

  // Set Dir to Home switch
    digitalWrite(STEPPER1_DIR_PIN, HIGH); // Enables the motor to move in a particular direction
  // Set STEP to Home switch
  digitalWrite(STEPPER1_STEP_PIN, HIGH); // Enables the motor to move in a particular direction


    stepper1.setMaxSpeed(800.0);// translation laterale
    stepper1.setAcceleration(200.0);
    stepper1.moveTo(5000);

     stepper2.setMaxSpeed(800.0);
     stepper2.setAcceleration(400.0);
     stepper2.moveTo(10000);
  }


  void loop() {
    {
 
      int leftSw  = digitalRead( LimitSwitch_LEFT_Pin);//declaration variable
      int rightSw = digitalRead( LimitSwitch_RIGHT_Pin);//declaration variable

        if ( (leftSw  == HIGH && (digitalRead(STEPPER1_DIR_PIN) == HIGH)) ||
              (rightSw == HIGH && (digitalRead(STEPPER1_DIR_PIN) == LOW)) ) {

           AccelStepper stepper1(1);

         }
    else if ( leftSw == LOW && (digitalRead(STEPPER1_DIR_PIN) == HIGH) ) {
         digitalWrite(STEPPER1_DIR_PIN, LOW);
          delay(100);
       }
          else if ( rightSw == LOW && (digitalRead(STEPPER1_DIR_PIN) == LOW ) ) {
        digitalWrite(STEPPER1_DIR_PIN, HIGH);
        delay(100);
     }


    // if (stepper2.distanceToGo() == 0)
    // stepper2.moveTo(stepper2.currentPosition());
  
    // Change direction at the limits
    // if (stepper1.distanceToGo() == 0)
    // stepper1.moveTo(-stepper1.currentPosition());


    stepper1.run();
       stepper2.run();

  }

}[/code]
[/code]

Image from Reply #3 so we don't have to download it. See this Simple Image Guide

...R

The program in Reply #3 won't compile. There is a superfluous { on line 52

You should should not be writing values to the direction pin. The AccelStepper library will take care of that.

I don't know what is the intended purpose of this line (line 60)

           AccelStepper stepper1(1);

my guess is that it should be deleted.

...R

thank very much for your help tome i change my code but something wrong

[code]#include <AccelStepper.h>

// moteur N°1 Stepper1 pins
#define STEPPER1_DIR_PIN 3 // stepper 1 CHARIOT AVEC FIN DE COURSE
#define STEPPER1_STEP_PIN 2


// moteur N°2 Stepper2 pins  stepper 2 ROTATION continue

#define STEPPER2_DIR_PIN 7 // la pin dir ne sera pas branché
#define STEPPER2_STEP_PIN 6

// contacts fin de course Pins
const int  LimitSwitch_LEFT_Pin  = 10;
const int LimitSwitch_RIGHT_Pin = 11;


// Define some steppers and the pins the will use
AccelStepper stepper1(AccelStepper::DRIVER, STEPPER1_STEP_PIN, STEPPER1_DIR_PIN);
AccelStepper stepper2(AccelStepper::DRIVER, STEPPER2_STEP_PIN, STEPPER2_DIR_PIN);





void setup() {
 Serial.begin(9600);
 int leftSw  = digitalRead( LimitSwitch_LEFT_Pin);//declaration variable
 int rightSw = digitalRead( LimitSwitch_RIGHT_Pin);//declaration variable

 pinMode(LimitSwitch_LEFT_Pin , INPUT);
 pinMode(LimitSwitch_RIGHT_Pin , INPUT);

 // Sets  pins as Outputs

 pinMode(STEPPER1_DIR_PIN, OUTPUT);
 pinMode(STEPPER1_STEP_PIN, OUTPUT);

 // Set Dir to Home switch
 digitalWrite(STEPPER1_DIR_PIN, HIGH); // Enables the motor to move in a particular direction
 // Set STEP to Home switch
 digitalWrite(STEPPER1_STEP_PIN, HIGH); // Enables the motor to move in a particular direction

 stepper1.setMaxSpeed(800.0);// translation laterale avant arriere
 stepper1.setAcceleration(200.0);
 stepper1.moveTo(12000);

 stepper2.setMaxSpeed(800.0);//ROTATION continue
 stepper2.setAcceleration(400.0);
 stepper2.moveTo(12000 );
}

void loop() {
 // put your main code here, to run repeatedly:
 int leftSw  = digitalRead( LimitSwitch_LEFT_Pin);//declaration variable
 int rightSw = digitalRead( LimitSwitch_RIGHT_Pin);//declaration variable

 if ( (leftSw  == HIGH) && ( rightSw == LOW))
   digitalWrite(STEPPER1_DIR_PIN, HIGH);


}
else if (leftSw  == LOW &&  rightSw == LOW) {
 digitalWrite(STEPPER1_DIR_PIN, HIGH);


}
else if (leftSw  == LOW &&  rightSw == HIGH) {
 digitalWrite(STEPPER1_DIR_PIN, LOW);

}
{
 stepper1.run();
 stepper2.run()

[/code]

You seem to have changed the image in Reply #3 so that my Reply #4 no longer displays it.

I have given you a link to the instructions for displaying images. Please follow them.

...R

hello i am back with new sketch is really hard for french guy

PROCESS

1 leftswitch==HIGH && rightswitch LOW (state departure) motor1 run CW motor 2 run CW

2 leftswitch==LOW && rightswitch LOW (state right transfert) motor1 run CW motor 2 run CW

3 leftswitch==LOW && rightswitch HIGH (state stop) motor1 stop 100 motor 2 run CW 3

4 leftswitch==LOW && rightswitch HIGH (state reverse) motor1 run CCW motor 2 run CW

5 leftswitch==HIGH && rightswitch LOW (state departure) motor1 stop 100 motor 2 run CW

6 leftswitch==HIGH && rightswitch LOW (state departure) motor1 run CW motor 2 run CW

the problem when i test

state 1 left switch on == motor1 stop motor 2 stop

when i hit right switch == motor1 stop motor 2 stop

and if i put delais on the code great vibration for motor1&2

#include <AccelStepper.h>

// moteur N°1 Stepper1 pins
#define STEPPER1_DIR_PIN 3 // stepper 1 CHARIOT AVEC FIN DE COURSE
#define STEPPER1_STEP_PIN 2


// moteur N°2 Stepper2 pins  stepper 2 ROTATION continue

#define STEPPER2_DIR_PIN 7 // la pin dir ne sera pas branché
#define STEPPER2_STEP_PIN 6

// contacts fin de course Pins
const int  leftSw_Pin  = 10;
const int rightSw_Pin = 11;


// Define some steppers and the pins the will use
AccelStepper stepper1(AccelStepper::DRIVER, STEPPER1_STEP_PIN, STEPPER1_DIR_PIN);
AccelStepper stepper2(AccelStepper::DRIVER, STEPPER2_STEP_PIN, STEPPER2_DIR_PIN);





void setup() {
  Serial.begin(9600);
  int leftSw  = digitalRead( leftSw_Pin);//declaration variable
  int rightSw = digitalRead( rightSw_Pin);//declaration variable

  pinMode(leftSw_Pin, INPUT);
  pinMode(rightSw_Pin , INPUT);

  // Sets  pins as Outputs

  pinMode(STEPPER1_DIR_PIN, OUTPUT);
  pinMode(STEPPER1_STEP_PIN, OUTPUT);

  // Set Dir to Home switch
  digitalWrite(STEPPER1_DIR_PIN, HIGH); // Enables the motor to move in a particular direction
  // Set STEP to Home switch
  digitalWrite(STEPPER1_STEP_PIN, HIGH); // Enables the motor to move in a particular direction

  stepper1.setMaxSpeed(400.0);// translation laterale avant arriere
  stepper1.setAcceleration(200.0);
  stepper1.moveTo(800);

  stepper2.setMaxSpeed(1200.0);//ROTATION continue
  stepper2.setAcceleration(400.0);
  stepper2.moveTo(1200000 );// i don't know to do run always
}

void loop() {
  // put your main code here, to run repeatedly:
  int leftSw  = digitalRead( leftSw_Pin);//declaration variable
  int rightSw = digitalRead( rightSw_Pin);//declaration variable

  if ( (leftSw  == HIGH) && ( rightSw == LOW))
  {

    digitalWrite(STEPPER1_DIR_PIN, HIGH);

  }
  else if (leftSw  == LOW &&  rightSw == LOW)
  {
    digitalWrite(STEPPER1_DIR_PIN, HIGH);

  }
  else if (leftSw  == LOW &&  rightSw == HIGH)
  {
    
    digitalWrite(STEPPER1_DIR_PIN, LOW);



    stepper1.run();
    stepper2.run();
  }
}

thank for your help

Hello now its ok its working right
would find the code its working nice even when you shut down yhe powerhe goes back to the left the point zero

#include <AccelStepper.h>

// The Y stepper pins
#define STEPPER2_DIR_PIN 7
#define STEPPER2_STEP_PIN 6

// Define some steppers and the pins the will use
AccelStepper stepper2(AccelStepper::DRIVER, STEPPER2_STEP_PIN, STEPPER2_DIR_PIN);

// defines pins numbers
const int stepPin = 5;
const int dirPin = 2;
const int enPin = 8;// on peut la metre en option en clair pas racorde
const int LimitSwitch_LEFT_Pin  = 10;
const int LimitSwitch_RIGHT_Pin = 11;


void setup() {

  Serial.begin(9600);

  stepper2.setMaxSpeed(800.0);
  stepper2.setAcceleration(200.0);
  stepper2.moveTo(10000000);//max 100 000 000  nb implulsion rotation bougie moteur 2

  pinMode(10, INPUT_PULLUP);//LimitSwitch_LEFT_Pin
  pinMode( 11, INPUT_PULLUP);//LimitSwitch_RIGHT_Pin


  // Sets the two pins as Outputs
  pinMode(stepPin, OUTPUT); // declare les sorties moteur1 rotation vis mere
  pinMode(dirPin, OUTPUT);

  pinMode(enPin, OUTPUT);   // declare enable moteur 1 moteur1 rotation vis mere

  digitalWrite(enPin, LOW);

  // Set Dir to Home switch
  digitalWrite(dirPin, HIGH); // Enables the motor to move in a particular direction

}
void loop() {
  Serial.println();

  stepper2.run();// demarre moteur2 rotation bougie


  int leftSw  = digitalRead( 10); //declare les fin de course L et R
  int rightSw = digitalRead( 11);

  if ((leftSw  == HIGH && (digitalRead(dirPin) == HIGH)) || // condition et et ou
      (rightSw == HIGH && (digitalRead(dirPin) == LOW)) ) { // condition et et ou

    motorStep1(1);
  }
  else if ( leftSw == LOW && (digitalRead(dirPin) == HIGH) ) { // condition et et ou
    digitalWrite(dirPin, LOW);
    delay(220);//200 à 250 // tempo
  }
  else if ( rightSw == LOW && (digitalRead(dirPin) == LOW ) ) { // condition et et ou
    digitalWrite(dirPin, HIGH);
    delay(220);//200 à 250 tempo
  }
}
void motorStep1( int MAX) { // bidouille pour les pulsations moteur 1

  for (int x = 0; x < MAX; x++) { // declare les pulsations et le point zero
    digitalWrite(stepPin, HIGH);
    delayMicroseconds(50000);////49900,50000,50500 plage de vitesse inverse proportion
    digitalWrite(stepPin, LOW);
    delayMicroseconds(50000);//49900,50000,50500  plage de vitesse inverse proportion
  }
}