Hi , I have a trouble with 2 limit switch for nema 17 operation ( A4988 module ). I need motor change direction when it touch to each limit switch but it not working , anyone could help me about it , thank you so much
Note :
On limit switch1 ,
ON pin connect to digital 2
COM pin connect to GRN
On limit switch2 ,
ON pin connect to digital 3
COM pin connect to GRN
const int dirPin = 5;
const int stepPin = 4;
const int lmswitch1 = 2;
const int lmswitch2 = 3;
const int enable = 8;
const int stepsPerRevolution = 1000;
void setup()
{
// Declare pins as Outputs
Serial.begin(9600);
pinMode(stepPin, OUTPUT);
pinMode(dirPin, OUTPUT);
pinMode(lmswitch1 , INPUT);
pinMode(lmswitch2 , INPUT);
pinMode(enable, OUTPUT);
digitalWrite(enable,LOW);
digitalWrite(dirPin, LOW);
}
void loop()
{
int ltm1 = digitalRead(lmswitch1);
int ltm2 = digitalRead(lmswitch2);
if ((ltm1 == HIGH) && (ltm2 == LOW)) {
motorStep(1000);
} else if ((ltm1 == LOW) && (ltm2 == LOW)) {
digitalWrite(dirPin, HIGH);
delay(2000);
} else if ((ltm1 == HIGH) && (ltm2 == HIGH)) {
motorStep(1000);
} else if ((ltm1 == LOW) && (ltm2 == HIGH)) {
digitalWrite(dirPin, LOW);
delay(2000);
}
if ( (digitalRead(dirPin) == HIGH) ) {
motorStep(1000);
digitalWrite(dirPin, HIGH);
}
else if ( (digitalRead(dirPin) == LOW) ) {
motorStep(1000);
digitalWrite(dirPin, LOW);
}
}
void motorStep( int MAX) {
for (int x = 0; x < MAX; x++) {
digitalWrite(stepPin, HIGH);
delayMicroseconds(200);
digitalWrite(stepPin, LOW);
delayMicroseconds(200);
}
}
Please follow the advice given in the link below when posting code. Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination
This makes no sense to me - firstly I've no clue what sense the limit switches are (active high or active low) so its unclear what's what.
Secondly you are conflating the two limit switches with each other which is odd - you are either at one limit or at the other, you can't be at both!! I'd expect two clauses, one for limit1, one for limit2. If neither is active nothing should happen, yet this code always does something no matter what.
I think i don't understand about limit switch.
On limit switch ,
ON pin connect to digital 2
COM pin connect to GRN
and i try this code to test limit switch but i don't understand why it always show "1" on Serial Monitor although i press into limit switch. So this limit switch cannot change state? , anyone could show me why ?
Hi,
If your limit swtiches are connected between the input pins and gnd, do you have pullup resistors for each of the switches?
This is to pull the respective input high when the switch is open.
OR.
You can turn the internal pullups by declaring your pinMode this way.
Hi,
Thanks for the image, BUT we need a circuit diagram, not a Fritzy image.
Please post a hand drawn diagram with what you are using for a power supply, not a 9V smokedetector battery.
Why have you got BOTH limit switches wired to the SAME pin?
Wire each switch to its own input pin like you had before. CHANGE;