#include <ezButton.h>
// define arduino D6, D5 and D4 pins as forward, reverse and step angle buttons
const int forwardButton = 6;
const int reverseButton = 5;
const int stepAngleButton = 4;
// 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 pulseCount = 500; // Enter the number of pulses that will rotate at a certain angle the stepper with a single press
int time = 3000; // Set a pulse lenght of the stepper or speed of the stepper motor in microsecond
//---------------------------------- End ---------------------------------------------------------------------------------------
ezButton button(stepAngleButton); // initialize button 3 pin
void setup() {
// declared pins as input/output
pinMode(forwardButton, INPUT_PULLUP);
pinMode(reverseButton, INPUT_PULLUP);
pinMode(stepAngleButton, INPUT_PULLUP);
pinMode(direction, OUTPUT);
pinMode(pulse, OUTPUT);
// set first time pulse and direction pins as low
digitalWrite(direction, LOW);
digitalWrite(pulse, LOW);
button.setDebounceTime(50); // button debounce time
}
void loop() {
button.loop(); // a functional loop
// read the status of three buttons
int forwardState = digitalRead(forwardButton);
int reverseState = digitalRead(reverseButton);
int stepAState = digitalRead(stepAngleButton);
// 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);
}
}
// check the stepAngleButton button status
if (stepAState == LOW){
if(button.isPressed()){
digitalWrite(direction, HIGH); // set the stepper motor clockwise direction
for(count=0; count<pulseCount; count++){
digitalWrite(pulse, HIGH);
delayMicroseconds(time);
digitalWrite(pulse, LOW);
delayMicroseconds(time);
}
}
if(button.isReleased()){
digitalWrite(pulse, LOW); // stopped the stepper motor
}
}
}