// Define Constants
// Connections to A4988
const int STEP_M1 = 13; // Step
const int DIR_M1 = 12; // Direction
const int STEP_M2 = 11; // Step
const int DIR_M2 = 10;
//buttons
const int BUTTON_CW = 9;
const int BUTTON_CCW = 8;
const int BUTTON_STOP = 7;
// 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(STEP_M1,OUTPUT);
pinMode(DIR_M1,OUTPUT);
pinMode(STEP_M2,OUTPUT);
pinMode(DIR_M2,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(DIR_M1,HIGH);
digitalWrite(DIR_M2,HIGH);
}
else if (digitalRead(BUTTON_CW) == HIGH && motorRunning == false) {
//motor is running
motorRunning = true;
// Set motor direction clockwise
digitalWrite(DIR_M1,LOW);
digitalWrite(DIR_M2,LOW);
}
while (motorRunning == true) {
if (digitalRead(BUTTON_STOP) == HIGH) {
motorRunning = false;
break; // Exit the while loop if the stop button is pressed
}
digitalWrite(STEP_M1,HIGH);
digitalWrite(STEP_M2,HIGH);
delayMicroseconds(2000);
digitalWrite(STEP_M1,LOW);
delayMicroseconds(2000);
digitalWrite(STEP_M2,LOW);
}
}