[SOLVED] Controling speed of step motor by Microstep Driver DMA860H

Hi! I want to drive a step motor by 4 push-buttons which two of them controls the direction and two of them controls the speed. By the way, speed can also be controlled by potantiometer instead of two buttons.However,motor should move to CC direction or CW direction as the direction button is pressed,otherwise it shouldn't move.

I wrote the code below and motor is moving in two directions as I press the Turn Left and Turn Right buttons but I couldn't change the speed.

/*
  Pul+ goes to +5V
  Pul- goes to Arduino nano Pin 6 which is PWM output
  Dir+ goes to +5V
  Dir- goes to to Arduino Pin 8
  Enable+ to nothing
  Enable- to nothing
*/

int speed_dec_button = 3;
int speed_inc_button = 4;
int turn_right_button = 5;
int turn_left_button = 7;
int stepPin = 6;  //pwm step pin
int directionPin = 8; //direction pin
int speed_dec_state;
int speed_inc_state;
int turn_right_state;
int turn_left_state;
int speedCounter;

void setup() {
  Serial.begin(9600);
  pinMode(speed_dec_button, INPUT);
  pinMode(speed_inc_button, INPUT);
  pinMode(turn_right_button, INPUT);
  pinMode(turn_left_button, INPUT);
  pinMode(stepPin, OUTPUT);
  pinMode(directionPin, OUTPUT);

}

void loop() {
  speed_dec_state = digitalRead(speed_dec_button);
  speed_inc_state = digitalRead(speed_inc_button);
  turn_right_state = digitalRead(turn_right_button);
  turn_left_state = digitalRead(turn_left_button);

  if (speed_dec_state == HIGH) {

    speedCounter = speedCounter - 250 ;
    if (speedCounter < 0) {
      speedCounter = 0;
    }
    Serial.println(speedCounter);


    while (speed_dec_state == HIGH) {
      speed_dec_state = digitalRead(speed_dec_button);
    }
  }

  if (speed_inc_state == HIGH) {

    speedCounter = speedCounter + 250;
    if (speedCounter > 3600) {
      speedCounter = 3600;
    }
    Serial.println(speedCounter);


    while (speed_inc_state == HIGH) {
      speed_inc_state = digitalRead(speed_inc_button);

    }
  }

  if (turn_right_state == HIGH) {

    digitalWrite(directionPin, HIGH);
    digitalWrite(stepPin, HIGH);
    delayMicroseconds(speedCounter);
    digitalWrite(stepPin, LOW);
    delayMicroseconds(speedCounter);

    Serial.println("Turn right button is pressed");
    Serial.print(speedCounter);


  }

  if (turn_left_state == HIGH) {

    digitalWrite(directionPin, LOW);
    digitalWrite(stepPin, HIGH);
    delayMicroseconds(speedCounter);
    digitalWrite(stepPin, LOW);
    delayMicroseconds(speedCounter);
    Serial.println("Turn left button is pressed");
    Serial.print(speedCounter);

  }
}

Microstep Driver DMA860H:

Microstep Driver and Arduino connection:

Thanks for helps :slight_smile:

I think you need to lose this piece (in 2 places)

    while (speed_dec_state == HIGH) {
      speed_dec_state = digitalRead(speed_dec_button);
    }

...R
Stepper Motor Basics
Simple Stepper Code

Loosing that parts didnt work to adjust speed.

Also, when I delete that parts buttons sensivity is lost. I mean, when speed_dec_button or speed_inc_button is pressed, speedCounter value becomes suddenly 0 and 3600 since the loop works in miliseconds.

kbskl:
Also, when I delete that parts buttons sensivity is lost. I mean, when speed_dec_button or speed_inc_button is pressed, speedCounter value becomes suddenly 0 and 3600 since the loop works in miliseconds.

Your WHILE loop is not the way to solve that problem. A much better way is to read the buttons (say) 10 times per second and use millis() to manage that timing as illustrated in Several Things at a Time

Get the button-read timing right and see whether the problem goes away. If not post your revised program.

...R

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);
    }
}
  
 }