// Define pins
const int dirPin = 2; // Direction pin
const int stepPin = 3; // Step pin
const int stepsPerSecond = 1000; // Steps per second
const int duration = 5000; // Duration in milliseconds (5 seconds)
void setup() {
pinMode(stepPin, OUTPUT);
pinMode(dirPin, OUTPUT);
}
void loop() {
// Clockwise rotation
digitalWrite(dirPin, HIGH); // Set direction to CW
unsigned long startTime = millis();
while (millis() - startTime < duration) {
digitalWrite(stepPin, HIGH);
delayMicroseconds(500); // 1000 steps/sec
digitalWrite(stepPin, LOW);
delayMicroseconds(500);
}
// Anticlockwise rotation
digitalWrite(dirPin, LOW); // Set direction to CCW
startTime = millis();
while (millis() - startTime < duration) {
digitalWrite(stepPin, HIGH);
delayMicroseconds(500);
digitalWrite(stepPin, LOW);
delayMicroseconds(500);
}
// Stop: do nothing
while (1); // Infinite loop to stop further motion
}