const int push_button = 8; // push button pin
const int LS1_NC = 10; // Limit switch 1 Normally Closed pin
const int LS1_NO = 5; // Limit switch 1 Normally Open pin
const int LS2_NC = 6; // Limit switch 2 Normally Closed pin
const int LS2_NO = 11; // Limit switch 2 Normally Open pin
const int step_pin = 3; // stepper motor pulse pin
const int dir_pin = 2; // stepper motor direction pin
const int enable_pin = 7; // stepper motor enable pin
// variables will change:
int buttonState = LOW; // the current state of LED
int lastButtonState; // the previous state of button
int currentButtonState; // the current state of button
int LS1_NC_state, LS1_NO_state, LS2_NC_state, LS2_NO_state;
void setup(){
pinMode(push_button, INPUT_PULLUP);
pinMode(LS1_NC, INPUT_PULLUP);
pinMode(LS1_NO, INPUT_PULLUP);
pinMode(LS2_NC, INPUT_PULLUP);
pinMode(LS2_NO, INPUT_PULLUP);
pinMode(step_pin, OUTPUT);
pinMode(dir_pin, OUTPUT);
pinMode(enable_pin, OUTPUT);
digitalWrite(dir_pin, LOW);
currentButtonState = digitalRead(push_button);
}
void loop(){
lastButtonState = currentButtonState; // save the last state
currentButtonState = digitalRead(push_button); // read new state
if(lastButtonState == HIGH && currentButtonState == LOW) {
// toggle state of LED
buttonState = !buttonState;
if(buttonState == HIGH){
digitalWrite(dir_pin, HIGH);
LS1_NO_state = digitalRead(LS1_NO);
while(LS1_NO_state != LOW){
digitalWrite(step_pin, HIGH);
delayMicroseconds(3000);
digitalWrite(step_pin, LOW);
delayMicroseconds(3000);
LS1_NO_state = digitalRead(LS1_NO);
}
}
else if(buttonState == LOW){
digitalWrite(dir_pin, LOW);
LS2_NO_state = digitalRead(LS2_NO);
while(LS2_NO_state != LOW){
digitalWrite(step_pin, HIGH);
delayMicroseconds(3000);
digitalWrite(step_pin, LOW);
delayMicroseconds(3000);
LS2_NO_state = digitalRead(LS2_NO);
}
}
}
delay(100);
}