// Using NO library, but blocking loop
#define dirPin 2
#define stepPin 3
#define stepsPerRevolution 200
#define buttonPin 4
bool buttonState = 0;
void setup() {
Serial.begin(115200);
pinMode(dirPin, OUTPUT);
pinMode(stepPin, OUTPUT);
pinMode(buttonPin, INPUT);
welcome();
}
void loop() {
// Press button to start
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH)
step();
}
void step()
{
int speed = 1900; // motor speed (min, max)
int rotations = 1; // number of rotations +1 for non-zero
int dir = 1; // direction 0 = CW or 1 = CCW
digitalWrite(dirPin, dir);
// Make one rotation
for (int i = 0; i < rotations * stepsPerRevolution; i++) {
digitalWrite(stepPin, HIGH);
delayMicroseconds(speed);
digitalWrite(stepPin, LOW);
delayMicroseconds(speed);
}
}
void welcome() {
Serial.println("Press the button for one revolution. ");
}