const int dirPin = 2;
const int stepPin = 3;
const int startButtonPin = 4; // Pin for the start button
const int stopButtonPin = 5; // Pin for the stop button
void setup() {
pinMode(stepPin, OUTPUT);
pinMode(dirPin, OUTPUT);
pinMode(startButtonPin, INPUT_PULLUP); // Use INPUT_PULLUP to enable internal pull-up resistor
pinMode(stopButtonPin, INPUT_PULLUP);
}
void loop() {
if (digitalRead(startButtonPin) == LOW) {
startMotor();
}
if (digitalRead(stopButtonPin) == LOW) {
stopMotor();
}
}
void startMotor() {
// Set motor direction clockwise
digitalWrite(dirPin, HIGH);
// Spin motor until stop button is pressed
while (digitalRead(stopButtonPin) == HIGH) {
digitalWrite(stepPin, HIGH);
delayMicroseconds(2000);
digitalWrite(stepPin, LOW);
delayMicroseconds(2000);
}
// Delay for a short period before stopping
delay(100);
}
void stopMotor() {
// Stop the motor by setting both dirPin and stepPin LOW
digitalWrite(dirPin, LOW);
digitalWrite(stepPin, LOW);
// Delay to ensure the motor has stopped
delay(100);
// Reset the motor position counterclockwise
digitalWrite(dirPin, HIGH);
// Delay for a short period before stopping
delay(100);
}