#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
}
}
}
nano:12
nano:11
nano:10
nano:9
nano:8
nano:7
nano:6
nano:5
nano:4
nano:3
nano:2
nano:GND.2
nano:RESET.2
nano:0
nano:1
nano:13
nano:3.3V
nano:AREF
nano:A0
nano:A1
nano:A2
nano:A3
nano:A4
nano:A5
nano:A6
nano:A7
nano:5V
nano:RESET
nano:GND.1
nano:VIN
nano:12.2
nano:5V.2
nano:13.2
nano:11.2
nano:RESET.3
nano:GND.3
drv1:ENABLE
drv1:MS1
drv1:MS2
drv1:MS3
drv1:RESET
drv1:SLEEP
drv1:STEP
drv1:DIR
drv1:GND.1
drv1:VDD
drv1:1B
drv1:1A
drv1:2A
drv1:2B
drv1:GND.2
drv1:VMOT
stepper1:A-
stepper1:A+
stepper1:B+
stepper1:B-
btn1:1.l
btn1:2.l
btn1:1.r
btn1:2.r
btn2:1.l
btn2:2.l
btn2:1.r
btn2:2.r
btn3:1.l
btn3:2.l
btn3:1.r
btn3:2.r