Connect "C" to the Arduino pin and NC to ground. Do not connect 5V. Do not connect NO.
digitalRead() will return LOW when the switch is not activated and HIGH when it is activated.
Using the NC connection in the switch is a better way because if the wires to the switch become detached, the motor will stop, which is a good safety feature.
I changed the code. When the switch is free the switch digital read returns HIGH and when I press the switch digital read returns LOW. But there is a problem. I have to hold the button to stop the stepper and when I release it the stepper keeps moving. I want after I press button and the release it the stepper don't work. Like 3D printers.
#include <Stepper.h>
const int stepsPerRevolution= 500;
Stepper myStepper(stepsPerRevolution,2,3);
int endstop=4;
void setup() {
myStepper.setSpeed(10);
pinMode(endstop,INPUT_PULLUP);
Serial.begin(115200);
}
void loop() {
if (digitalRead(endstop)==HIGH) {
Serial.println("stop");
myStepper.step(0);
}
if (digitalRead(endstop)==LOW)
{
Serial.println("move");
myStepper.step(stepsPerRevolution);
}
}
Yes. And when I upload the code in void setup, the result is different. the stepper keeps moving when I press the switch and I don't see 'stop' in serial monitor.
#include <Stepper.h>
const int stepsPerRevolution= 500;
Stepper myStepper(stepsPerRevolution,2,3);
int endstop=4;
void setup() {
myStepper.setSpeed(10);
pinMode(endstop,INPUT_PULLUP);
Serial.begin(115200);
if (digitalRead(endstop)==HIGH) {
Serial.println("stop");
myStepper.step(0);
}
if (digitalRead(endstop)==LOW)
{
Serial.println("move");
myStepper.step(stepsPerRevolution);
}
}
void loop() {
}
The Stepper library is very simple to use but can get complicated when trying to do what you want.
The AccelStepper or MobaTools libaries would be bettor.
Most people find MobaTools easier.
I'm working on a machine like 3D printer. I need to define the start point of the stepper motor when I execute the code and then I don't need the end stop until the machine finishes working. Do you have any idea? Do you offer AccelStepper library for this case? And I have to use A4988 but I think AccelStepper library is for AF motor shields right?