Accelstepper reverse direction issue

Hello all, I use accelstepper to run a stepper motor in one direction at a constant speed and then the other when a contact is done with a LimitSwitch.
The LimitSwitch seems to be ok : the LED 13 is on when a contact is done, but the stepper motor still rotate in the same direction (by a slightly different noise) while I reversed the value.
I do not understand why. Can you help me?

#include <AccelStepper.h>


int leBigMove = 20000;
int commande = 0; 

int led = 13;
int limSwitch = 9;
                                     
                                    
int motor1DirPin = 2; //digital pin 2   
int motor1StepPin = 5; //digital pin 3
AccelStepper stepper1(1, motor1StepPin, motor1DirPin); 

void setup() {
  pinMode(led, OUTPUT);
  pinMode (limSwitch, INPUT);
  Serial.begin(9600);
  stepper1.setSpeed(200);
}

void loop() {
  
   
   int switch1 = digitalRead(limSwitch); 
   digitalWrite (led, LOW);
   
   if (switch1 == LOW){ 
     commande = 0;
   }

 if(switch1 == HIGH){ // Contact
    digitalWrite (led, HIGH); 
    if (commande == 0){ 
     leBigMove = - leBigMove;
     commande = 1; 

    }
 } 


  stepper1.moveTo(leBigMove); 
  stepper1.setSpeed(200);
 stepper1.run();

  
}

I wonder are you getting repeated changes of direction for a single contact with the limit switch.

Instead of leBigMove = - leBigMove;

try something like stepperDestination = - leBigMove; and stepper1.moveTo(stepperDestination);

Of course, somewhere else in your code you will need stepperDestination = leBigMove; for the forward direction.

...R

   if (switch1 == LOW){ 
     commande = 0;
   }

 if(switch1 == HIGH){ // Contact
    digitalWrite (led, HIGH);

Are you planning to add code later for the other possible return values from digitalRead?

Thanks Robin & Paul,

Paul : No, I've not planned to add code in this place.
I just added this line to either activate the test below (switch1 == HIGH) when there is no contact. I did it precisely to avoid that if the limit switch is pressed for too long a time the value is reversed several times. Maybe it does better to do?

Robin (& Paul): Good idea. But I tried the following correction in the code, but I get the same thing. Yet I have "normal" and "reverse" that appear in the monitor when the contact is made. first contact: Reverse and second contact: normal ... Etc.

#include <AccelStepper.h>


int leBigMove = 20000;
int stepperDestination ;
int commande = 0; 

int led = 13;
int limSwitch = 9;
                                     
                                    
int motor1DirPin = 2; //digital pin 2   
int motor1StepPin = 5; //digital pin 3
AccelStepper stepper1(1, motor1StepPin, motor1DirPin); 

void setup() {
  pinMode(led, OUTPUT);
  pinMode (limSwitch, INPUT);
  Serial.begin(9600);
  stepper1.setSpeed(200);
  stepperDestination = leBigMove;
}

void loop() {
  
   
   int switch1 = digitalRead(limSwitch); 
   digitalWrite (led, LOW);
   
   if (switch1 == LOW){ 
     commande = 0;
   }

 if(switch1 == HIGH){ // Contact
    digitalWrite (led, HIGH); 
    if (commande == 0){ 
      if (stepperDestination < 0){
           stepperDestination = leBigMove;
           Serial.println("normal");
         
         } else {
           stepperDestination = - leBigMove;
            Serial.println("inverse");
         }

     commande = 1; 

    }
 } 


  stepper1.moveTo(leBigMove); 
  stepper1.setSpeed(200);
 stepper1.run();

  
}

What is strange is that if instead of using a constant speed, I use an acceleration (AccelStepper library), it already works a little better: It changes direction. Not when I want because I have to push the LimitSwitch for a long time but in any case it changes direction when I relaxes.

void setup() {
....
//stepper1.setSpeed(200);
  stepper1.setMaxSpeed(3000);
  stepper1.setAcceleration(1000);
}

....

void loop() {
....
stepper1.moveTo(stepperDestination);
  //stepper1.setSpeed(200);
stepper1.run();

}
   if (switch1 == LOW){ 
     commande = 0;
   }
 if(switch1 == HIGH){ // Contact

My point was that there are NOT other states. If the switch is NOT low, it MUST be high, so there is no need to test for that.

There IS a need to use Tools + Auto Format and clean up your piss-poor indenting so your code is readable.

There IS a need to use Serial.print() to see what your code IS doing.

stepper1.moveTo(leBigMove);

Looks like you forgot to update this line ?

...R

Ooops Robin, you're right, but in fact, I had corrected this line in my code ... just forgot to update it on the forum. And with this correction, the problem is the same

if (stepperDestination < 0){
			stepperDestination = leBigMove;
			Serial.println("normal");
		}
		else {
			stepperDestination = - leBigMove;
			Serial.println("inverse");
		}

I think you need to replace all of this with

stepperDestination = - leBigMove;
Serial.println("REverse");

At the moment your code is still toggling the direction when the limit switch is touched. All you should be doing is changing the direction to backwards.

...R