// define arduino D6, D5 and D4 pins as forward, reverse and step angle buttons
const int forwardButton = 6;
const int reverseButton = 5;
// define arduino D2 and D3 pins as direction and Pulse
int direction = 2;
int pulse = 3;
int count; // A variable for counting pulses
// ------------------------------- Change the variable depending on your desired speed and angle ------------------------------
int time = 13000; // Set a pulse lenght of the stepper or speed of the stepper motor in microsecond
//---------------------------------- End ---------------------------------------------------------------------------------------
void setup() {
// declared pins as input/output
pinMode(forwardButton, INPUT_PULLUP);
pinMode(reverseButton, INPUT_PULLUP);
pinMode(direction, OUTPUT);
pinMode(pulse, OUTPUT);
// set first time pulse and direction pins as low
digitalWrite(direction, LOW);
digitalWrite(pulse, LOW);
}
void loop() {
// read the status of three buttons
int forwardState = digitalRead(forwardButton);
int reverseState = digitalRead(reverseButton);
// check the forward button status
if(forwardState == LOW){
delay(10);
if(forwardState == LOW){
digitalWrite(direction, LOW); // set the stepper motor clockwise direction
do{
digitalWrite(pulse, HIGH);
delayMicroseconds(time);
digitalWrite(pulse, LOW);
delayMicroseconds(time);
forwardState = digitalRead(forwardButton); // read the forward button status again
}
while(forwardState != HIGH);
}
}
// check the reverse button status
if(reverseState == LOW){
delay(10);
if(reverseState == LOW){
digitalWrite(direction, HIGH); // set the stepper motor anti-clockwise direction
do{
digitalWrite(pulse, HIGH);
delayMicroseconds(time);
digitalWrite(pulse, LOW);
delayMicroseconds(time);
reverseState = digitalRead(reverseButton); // read the reverse button status again
}
while(reverseState != HIGH);
}
}
}