I have changed my code and setup to overcome this issue. Instead of using 4 button, I have put
1 button(for action),
1 potantiometer (to control acceleration) and
1 switch (for changing direction).
I will share the code below and connections is in this code;
/*
Pul+ goes to +5V
Pul- goes to Arduino Pin 9 (for nano pwm is Pin 6)
Dir+ goes to +5V
Dir- goes to to Arduino Pin 8
Enable+ to nothing
Enable- to nothing
*/
int sensorPin = A0;
int sensorValue = 0;
int button =4;
int durdurma = 5;
int buttonState;
int durdurma1;
int i=0;
int k;
void setup() {
Serial.begin(9600);
pinMode(8, OUTPUT); //direction pin
pinMode(6, OUTPUT); //step pin
pinMode(4,INPUT); //switch for motor direction
pinMode(5,INPUT); //push button for motor action
digitalWrite(8, LOW);
digitalWrite(6, LOW);
}
void loop() {
durdurma1 = digitalRead(durdurma);
if(durdurma1==HIGH)
{
digitalWrite(6, LOW);
i=i+1;
}
digitalWrite(6, LOW);
k=i%2;
while(k==1)
{
if(durdurma1==HIGH)
{
i=i+1;
}
k=i%2;
sensorValue = analogRead(sensorPin);
buttonState = digitalRead(button);
sensorValue = map(sensorValue,0,1023,0,15000);
digitalWrite(6, HIGH);
Serial.println(sensorValue);
delayMicroseconds(sensorValue);
digitalWrite(6, LOW);
delayMicroseconds(sensorValue);
if(buttonState==HIGH){
digitalWrite(8,HIGH);
}
else{
digitalWrite(8,LOW);
}
}
}