const int BUTTON_PIN = 4;
const int DIR = 3;
const int STEP = 2;
const int SPR = 200; // stepsperrev
// Arduinopinconnected to button's pin
// variables will change:
// the current state of LED
int lastButtonState; // the previous state of button
int currentButtonState; // the current state of button
void setup() {
Serial.begin(9600); // initialize serial
pinMode(STEP, OUTPUT);
pinMode(DIR, OUTPUT);
pinMode(BUTTON_PIN, INPUT_PULLUP);//set arduino pin to input pull-up mode
currentButtonState = digitalRead(BUTTON_PIN);
}
void loop() {
lastButtonState = currentButtonState;// save the last state
currentButtonState = digitalRead(BUTTON_PIN);//read new state
digitalWrite(DIR, HIGH);
if(lastButtonState == HIGH && currentButtonState == LOW) {
Serial.println("The button is pressed");
for(int i = 0; i<SPR; i++)
{
digitalWrite(STEP, HIGH);
delayMicroseconds(2000);
digitalWrite(STEP, LOW);
delayMicroseconds(2000);
}
delay(1000);
}
}