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();
}
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.
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
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.