// A4988 Stepper Driver Demo
const int stepPin = 3;
const int dirPin = 4;
const int stepsPerRev = 200; // typical for 1.8° stepper motor
void setup() {
pinMode(stepPin, OUTPUT);
pinMode(dirPin, OUTPUT);
digitalWrite(dirPin, HIGH); // initial direction
}
void loop() {
// 1. Slow continuous rotation (forward)
digitalWrite(dirPin, HIGH);
for (int i = 0; i < stepsPerRev; i++) {
digitalWrite(stepPin, HIGH);
delayMicroseconds(1200);
digitalWrite(stepPin, LOW);
delayMicroseconds(1200);
}
delay(1000);
// 2. Reverse direction fast
digitalWrite(dirPin, LOW);
for (int i = 0; i < stepsPerRev; i++) {
digitalWrite(stepPin, HIGH);
delayMicroseconds(600);
digitalWrite(stepPin, LOW);
delayMicroseconds(600);
}
delay(1000);
// 3. Burst stepping (jerky movement demo)
digitalWrite(dirPin, HIGH);
for (int i = 0; i < 50; i++) {
digitalWrite(stepPin, HIGH);
delay(5);
digitalWrite(stepPin, LOW);
delay(5);
}
delay(1000);
// 4. Micro-stepping slow crawl
digitalWrite(dirPin, LOW);
for (int i = 0; i < stepsPerRev; i++) {
digitalWrite(stepPin, HIGH);
delayMicroseconds(2000);
digitalWrite(stepPin, LOW);
delayMicroseconds(2000);
}
delay(2000);
}