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