// Define pin connections
const int STEP_PIN = 3;
const int DIR_PIN = 4;
// Variables to store the direction
int direction = 0;
bool rotate = false;
void setup() {
pinMode(DIR_PIN, OUTPUT); // Set DIR_PIN as output
pinMode(STEP_PIN, OUTPUT); // Set STEP_PIN as output
Serial.begin(9600); // Initialize the serial port
Serial.println("Enter direction:"); // Prompt user for input
}
void loop() {
if (Serial.available() > 0) { // Check if there is serial input available
int newDirection = Serial.parseInt(); // Read the number of steps from serial input
// Update the direction if a valid input is received
if (newDirection == 1 || newDirection == 0) {
direction = newDirection;
rotate = true;
// Print the entered direction to the serial monitor
Serial.print("Direction: ");
Serial.println(direction == 1 ? "Clockwise" : "Counter-Clockwise");
// Set the direction based on input
if (direction == 1) {
digitalWrite(DIR_PIN, HIGH); // Clockwise
} else if (direction == 2) {
digitalWrite(DIR_PIN, LOW); // Counter-Clockwise
}
}
}
// Check if the motor should rotate
if (rotate) {
// Generate the steps continuously
digitalWrite(STEP_PIN, HIGH); // Set STEP_PIN high
delayMicroseconds(3000); // Wait for the pulse delay time
digitalWrite(STEP_PIN, LOW); // Set STEP_PIN low
delayMicroseconds(3000); // Wait for the pulse delay time
}
}