#define STEP_PIN 2
#define DIR_PIN 3
#define ENABLE_PIN 4
#define RESET_PIN 5
bool motorRunning = false;
void setup() {
pinMode(STEP_PIN, OUTPUT);
pinMode(DIR_PIN, OUTPUT);
pinMode(ENABLE_PIN, OUTPUT);
pinMode(RESET_PIN, OUTPUT);
digitalWrite(ENABLE_PIN, LOW); // Enable the motor driver
digitalWrite(RESET_PIN, HIGH); // Ensure the motor is not in reset mode
Serial.begin(9600); // Start serial communication for virtual terminal
}
void loop() {
if (Serial.available() > 0) {
String command = Serial.readStringUntil('\n'); // Read command from terminal
command.trim(); // Remove any extra spaces or newlines
if (command.equalsIgnoreCase("start")) {
Serial.println("Motor started...");
motorRunning = true;
digitalWrite(ENABLE_PIN, LOW); // Enable the motor driver
}
else if (command.equalsIgnoreCase("stop")) {
Serial.println("Motor stopped.");
motorRunning = false;
digitalWrite(ENABLE_PIN, HIGH); // Disable the motor driver
}
else if (command.equalsIgnoreCase("enable")) {
Serial.println("Motor enabled.");
digitalWrite(ENABLE_PIN, LOW); // Enable the motor driver
}
else {
Serial.println("Unknown command.");
}
}
if (motorRunning) {
stepMotor(1); // Rotate motor one step continuously
}
}
void stepMotor(int steps) {
for (int i = 0; i < steps; i++) {
digitalWrite(STEP_PIN, HIGH);
delayMicroseconds(500);
digitalWrite(STEP_PIN, LOW);
delayMicroseconds(500);
}
}