Hello,
I'm controlling a steppermotor via stepperdriver A4988 and a Nano.
I have two buttons, one that runs the motor and the other one changes direction. (and two leds)
Also there is a potentiometer that controls the motor speed.
Evereythings works just fine except that motor will not hold position when "run" button is released.
I think the problem is that the code below is sending a "High" command to the enable pin when button is released.
How can i get the steppermotor to stay enabled? For me the total opposite would be fine ==> motor Enabled as soon as power is on. I have tried to change code from LOW to HIGH and vice versa without success i this section:
"ButtonValue = digitalRead(Button);
if(ButtonValue == 0){
digitalWrite(Enable, HIGH);
}
else{
digitalWrite(Enable, LOW);
} "
I found below code and i'm sorry to say that i don't remember were
int ButtonValue = 0;
const int stepPin = 3;
int Button = 5;
int Button1 = 6;
int ledPin = 9;
int ledPin1 = 10;
int dirPin = 4;
int Enable = 7;
int brightness = 0;
// Variables will change:
int buttonPushCounter = 0; // counter for the number of button presses
int buttonState = 0; // current state of the button
int lastButtonState = 0; // previous state of the button
int customDelay,customDelayMapped; // Defines variables
void setup() {
pinMode(Button, INPUT);
pinMode(Button1, INPUT);
pinMode(Enable, OUTPUT);
pinMode(stepPin,OUTPUT);
pinMode(dirPin,OUTPUT);
pinMode(ledPin, OUTPUT);
pinMode(ledPin1, OUTPUT);
Serial.begin(9600);
}
void loop() {
brightness = digitalRead(Button); // read input value
if (brightness == HIGH) { // check if the input is HIGH (button released)
digitalWrite(ledPin, HIGH); // turn LED OFF
} else {
digitalWrite(ledPin, LOW); // turn LED ON
}
brightness = digitalRead(Button1); // read input value
if (brightness == HIGH) { // check if the input is HIGH (button released)
digitalWrite(ledPin1, HIGH);// turn LED OFF
} else {
digitalWrite(ledPin1, LOW); // turn LED ON
}
ButtonValue = digitalRead(Button);
if(ButtonValue == 0){
digitalWrite(Enable, HIGH);
}
else{
digitalWrite(Enable, LOW);
}
buttonState = digitalRead(Button1);
// compare the buttonState to its previous state
if (buttonState != lastButtonState) {
// if the state has changed, increment the counter
if (buttonState == HIGH) {
// if the current state is HIGH then the button went from off to on:
buttonPushCounter++;
Serial.println("on");
Serial.print("number of button pushes: ");
Serial.println(buttonPushCounter);
} else {
// if the current state is LOW then the button went from on to off:
Serial.println("off");
}
// Delay a little bit to avoid bouncing
delay(100);
}
// save the current state as the last state, for next time through the loop
lastButtonState = buttonState;
if (buttonPushCounter % 2 == 0) {
digitalWrite(dirPin, LOW);
} else {
digitalWrite(dirPin, HIGH);
}
customDelayMapped = speedUp(); // Gets custom delay values from the custom speedUp function
// Makes pules with custom delay, depending on the Potentiometer, from which the speed of the motor depends
digitalWrite(stepPin, HIGH);
delayMicroseconds(customDelayMapped);
digitalWrite(stepPin, LOW);
delayMicroseconds(customDelayMapped);
}
// Function for reading the Potentiometer
int speedUp() {
int customDelay = analogRead(A0); // Reads the potentiometer
int newCustom = map(customDelay, 0, 1023, 600,12000); // Convrests the read values of the potentiometer from 0 to 1023 into desireded delay values (500 (highest speed) to 12000 (lowest speed), just try)
return newCustom;
}
Hope soemone now a fix
br /Hans