// Define pin connections
const int STEP_PIN = 3;
const int DIR_PIN = 4;
// Variables to store the number of steps, pulse delay time, and direction
int steps = 0;
int pulse_delay_time = 0;
int direction = 0;
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 steps, pulse delay time and direction:"); // Prompt user for input
}
void loop() {
if (Serial.available() > 0) { // Check if there is serial input available
steps = Serial.parseInt(); // Read the number of steps from serial input
pulse_delay_time = Serial.parseInt(); // Read the pulse delay time from serial input
direction = Serial.parseInt(); // Read the direction from serial input
// Print the entered values to the serial monitor
Serial.print("Step: ");
Serial.print(steps);
Serial.print(" ");
Serial.print("Delay: ");
Serial.print(pulse_delay_time);
Serial.print(" ");
Serial.print("Direction: ");
Serial.print(direction);
Serial.println(" ");
// Check if the entered values are valid
if (steps > 0 && pulse_delay_time > 0 && (direction == 0 || direction == 1)) {
digitalWrite(DIR_PIN, direction); // Set the direction
for (int i = 0; i < steps; i++) { // Loop to generate the steps
digitalWrite(STEP_PIN, HIGH); // Set STEP_PIN high
delayMicroseconds(pulse_delay_time); // Wait for the pulse delay time
digitalWrite(STEP_PIN, LOW); // Set STEP_PIN low
delayMicroseconds(pulse_delay_time); // Wait for the pulse delay time
}
}
// Reset the variables
}
}