Im trying to get a stepper motor to move one step every time I push a button,which I have but now I wish to use another button to send the motor the other way one step. Im using a A4988 stepper driver and when I tried to use the code in reverse to move the other way the motor seems to be it two minds of what to do. Both buttons work fine on their own so it must be somthing to do with the code,could someone please have a look at the following code and point out where iI have gone wrong?
const int steppin = 1;
const int dirpin = 2;
int switchpin = 8;
int switchpin1 = 9;
void setup() {
pinMode (steppin,OUTPUT);
pinMode (dirpin,OUTPUT);
pinMode (switchpin,INPUT);
pinMode (switchpin1,INPUT);
}
void loop()
{
if (digitalRead(switchpin) == HIGH)
{
digitalWrite(steppin,HIGH);digitalWrite(dirpin,LOW);
}
else
{
digitalWrite(steppin,LOW);digitalWrite(dirpin,HIGH);
}
if (digitalRead(switchpin1) == HIGH)
{
digitalWrite(steppin,HIGH);digitalWrite(dirpin,HIGH);
}
else
{
digitalWrite(steppin,LOW);digitalWrite(dirpin,LOW);
}
}
You should not be switching "dir" so frequently, only when you want to change direction
The driver will command the motor to take a step when the "step" input goes high. Normally, people immediately set it low again.
Keep in mind that switches bounce. The way you have it set up, with loop reading the switches as fast as possible, you are likely to get several steps and/or changes of direction each time you push a switch. To make sure that the motor takes only one step each time you push the switch, you will need to "debounce" the switch. There are code libraries that do that for you.
Leave it alone until you can get the motor to step consistently in one direction, each time you push the button.
You really need to sit down and study the code, to understand what each line does. It make absolutely no sense to do the following every time the loop() function executes:
After last week,I took what you had to say along with some reading and allowed it all to rattle around in my brain for 6 or so days. I have now come up with a sketch that allows for forward and backward motion of the stepper!
const int StepPin = 1;
const int DirPin = 2;
int SwitchPin = 9;
int SwitchPin1 = 8;
int OnTime = 10;
int OffTime = 10;
void setup() {
pinMode(DirPin,OUTPUT);
pinMode(StepPin,OUTPUT);
pinMode(SwitchPin,INPUT);
}
void loop() {
if (digitalRead(SwitchPin)==HIGH)
{
digitalWrite(DirPin,HIGH);
digitalWrite(StepPin,HIGH);
delay(OnTime);
digitalWrite(StepPin,LOW);
delay(OffTime);
}
else
{
digitalWrite(DirPin,LOW);
digitalWrite(StepPin,LOW);
}
if (digitalRead(SwitchPin1)==HIGH)
{
digitalWrite(StepPin,HIGH);
delay(OnTime);
digitalWrite(StepPin,LOW);
delay(OffTime);
}
else
{
digitalWrite(StepPin,LOW);
}
}
Can you have a look at what I have wrote and tell me if there is any redundant or missing code? so I can move to the next stage of my project.
Did you read the documentation for the A4988 driver? The minimum pulse width for the STEP input is 1 microsecond, but the following code has the processor doing nothing for 20 milliseconds.
Here`s the code with commentary as to what was going on in my head.
const int StepPin = 1; //set pin description and number
const int DirPin = 2; //set pin description and number
int SwitchPin = 9; //set pin description and number
int SwitchPin1 = 8; //set pin description and number
int OnTime = 10; //timer on and duration
int OffTime = 10; //timer off and duration
void setup() {
pinMode(DirPin,OUTPUT); //set pin function
pinMode(StepPin,OUTPUT); //set pin function
pinMode(SwitchPin,INPUT); //set pin function
pinMode(SwitchPin1,INPUT); //set pin function
}
void loop() { //start loop
if (digitalRead(SwitchPin)==HIGH) //read switch pin and if it is high do following clause
{ //start cluase
digitalWrite(DirPin,HIGH); //turn pin on
digitalWrite(StepPin,HIGH); //turn pin on
delay(OnTime); //amount of time to be on
digitalWrite(StepPin,LOW); //turn pin off
delay(OffTime); //amount of time to be off
} //end clause
else //if not follow next clause
{ //start clause
digitalWrite(DirPin,LOW); //turn pin off(make sure they are in of postition)
digitalWrite(StepPin,LOW); //turn pin off(make sure they are in of postition)
} //end clause
if (digitalRead(SwitchPin1)==HIGH) //read switch pin 1 and if it is high do following clause
{ //start clause
digitalWrite(StepPin,HIGH); //turn pin on
delay(OnTime); //amount of time to be on
digitalWrite(StepPin,LOW); //turn pin off
delay(OffTime); //amount of time to be off
} //end clause
else //if not follow next clause
{ //start clause
digitalWrite(StepPin,LOW); //turn pin off(make sure they are in of postition)
} //end clause
} //end loop
Do you know the direction pin should be high for turning in one direction and low for the opposite direction?
It seems from your code you don't know that.
jremington:
Setting a pin LOW does not turn it off.
Please explain the reason why you are setting DirPin and StepPin low in the section I asked about.
Hi,
Im getting ready for the next part of the code,you know when the pulse is sent to set it high. It wouldnt be a pulse if it was already high would it???
Grumpy_Mike:
Do you know the direction pin should be high for turning in one direction and low for the opposite direction?
It seems from your code you don't know that.
So that means you should not be setting the direction pin when you pulse the step pin. You code seems to be addressing the direction pin every time you alter the step pin.
The code will also keep pulsing these pins as long as the switch is held down. Do you need to produce one stepping pulse per press, that is when the button is pushed and then released? As it is you send pulses way too fast to the driver when the button is pressed.
You seem to be trying to do two things at once and mixing them up. What do you actually want to do with these switches?
Hi,
How have you got your switches wired, if you are switching the digital input to 5V, you will need a 10K resistor between that input and gnd to pull the input LOW when the switch is open.
Otherwise when you turn the switch OFF the input will still be read as HIGH.
Ive reed what you wrote and I decided to try the sketches from reply 3 and for some strange reason they dont work as described,I can hear the motor power up but nothing happens. I changed the step/direction and switch pins to work with my wiring setup but that is all I changed,the pins all correspond to the following sketch which seemed to work i.e. press button and it would move in one direction untill finger removed and then would stop.
const int StepPin = 1;
const int DirPin = 2;
int SwitchPin = 9;
int OnTime = 10;
int OffTime = 10;
void setup() {
pinMode(DirPin,OUTPUT);
pinMode(StepPin,OUTPUT);
pinMode(SwitchPin,INPUT);
}
void loop() {
if (digitalRead(SwitchPin)==HIGH)
{
digitalWrite(DirPin,HIGH);
digitalWrite(StepPin,HIGH);
delay(OnTime);
digitalWrite(StepPin,LOW);
delay(OffTime);
}
else
{
digitalWrite(DirPin,LOW);
digitalWrite(StepPin,LOW);
}
}
I have also attached my fritzing diagram so you can see my wiring,maybe thats the problem?
The code in #16 is the one I did and for some strange reason that works when neither yours did,I included
it so you could see what I was working with. Apart from GND is there anything else thats stopping your sketches working on my setup?
Is the following diagram effectively GND? Dont worry about the pins 0 and 1 Ill make a mental note to change them once I have got the GND done.