// Define Constants
// Connections to A4988
const int dirPin = 2; // Direction
const int stepPin = 3; // Step
//buttons
const int BUTTON_STOP = 8;
const int BUTTON_CCW = 9;
const int BUTTON_CW = 10;
// Motor steps per rotation
const int STEPS_PER_REV = 200;
//motor state
bool motorRunning;
int delaytime;
void setup() {
Serial.begin(9600);
// Setup the pinsnand buttons
pinMode(stepPin,OUTPUT);
pinMode(dirPin,OUTPUT);
pinMode(BUTTON_STOP,INPUT);
pinMode(BUTTON_CCW,INPUT);
pinMode(BUTTON_CW,INPUT);
//set motor state to false
motorRunning = false;
}
void loop() {
if (digitalRead(BUTTON_CW) == HIGH && motorRunning == false) {
//motor is running
motorRunning = true;
// Set motor direction clockwise
digitalWrite(dirPin,HIGH);
}
else if (digitalRead(BUTTON_CW) == HIGH && motorRunning == false) {
//motor is running
motorRunning = true;
// Set motor direction clockwise
digitalWrite(dirPin,LOW);
}
while (motorRunning == true) {
if (digitalRead(BUTTON_STOP) == HIGH) {
motorRunning = false;
break; // Exit the while loop if the stop button is pressed
}
digitalWrite(stepPin,HIGH);
delayMicroseconds(2000);
digitalWrite(stepPin,LOW);
delayMicroseconds(2000);
}
}