// test a stepper motor with a Pololu A4988 driver board, onboard led will flash at each step
// this version uses delay() to manage timing
byte directionPin = 8;
byte stepPin = 9;
const int ledPin = 3;
int numberOfSteps = 200;
int pulseWidthMicros = 20; // microsecondo
int millisbetweenSteps = 10; // milliseconds - or try 100 for slower steps
int statostart;
void setup() {
Serial.begin(9600);
Serial.println("Starting StepperTest");
digitalWrite(ledPin, LOW);
pinMode(directionPin, OUTPUT);
pinMode(stepPin, OUTPUT);
pinMode(ledPin, OUTPUT);
pinMode(2, INPUT);
pinMode(ledPin, OUTPUT); // set the LED pin as an output
}
void loop() {
// read the value of the potentiometer
int stepValue = digitalRead(stepPin);
// map the potentiometer value to a range suitable for the LED
int ledBrightness = map(stepValue, 0, 1023, 0, 255);
// set the brightness of the LED
analogWrite(ledPin, ledBrightness);
statostart=digitalRead(2);
if(digitalRead(2) == HIGH){
digitalWrite(directionPin, HIGH);
for(int n = 0; n < numberOfSteps; n++) {
digitalWrite(stepPin, HIGH);
delayMicroseconds(pulseWidthMicros);
digitalWrite(stepPin, LOW);
delay(millisbetweenSteps);
digitalWrite(ledPin, !digitalRead(ledPin));
}
delay(1000);
digitalWrite(directionPin, LOW);
for(int n = 0; n < numberOfSteps; n++) {
digitalWrite(stepPin, HIGH);
// delayMicroseconds(pulseWidthMicros); // probably not needed
digitalWrite(stepPin, LOW);
delay(millisbetweenSteps);
digitalWrite(ledPin, !digitalRead(ledPin));
}}
}