#include <Button.h>
#define dirPin 2
#define stepPin 3
#define stepsPerRevolution 200
Button button1(4);
const int stepDelay = 500;
const int ledPin = 13;
void setup() {
// Declare pins as output:
pinMode(stepPin, OUTPUT);
pinMode(dirPin, OUTPUT);
pinMode(ledPin, OUTPUT); // Added pin mode for LED
button1.begin();
}
void loop() {
// Set the spinning direction clockwise:
digitalWrite(dirPin, HIGH);
// Check if the button is pressed
if (button1.pressed()) {
// Continuously step the motor while the button is pressed
while (button1.pressed()) {
digitalWrite(stepPin, HIGH);
delayMicroseconds(stepDelay);
digitalWrite(stepPin, LOW);
delayMicroseconds(stepDelay);
// Optional: add LED blink while button is pressed
digitalWrite(ledPin, HIGH);
delay(100);
digitalWrite(ledPin, LOW);
delay(100);
}
} else {
// Turn off LED when button is not pressed
digitalWrite(ledPin, LOW);
}
}