Per my last post, I have gotten rid of the Adafruit motorshield and have been working on the circuit seen in the picture below. I am now attempting to code with the AccelStepper library to create the desired behavior for my stepper motor. I have attempted to piece together some code from examples, other projects etc. and I have gotten it to successfully turn at different speeds but that is about as far as I got. My desired stepper motor behavior is for the stepper to turn an [X] amount of steps and then hold for an [X] amount of time. The closest I have gotten is with the code pasted below. I was attempting to make the stepper move 8 steps and then delay for 1 second. The code, however, seems to make the delay between every step. I am not sure how to make the delay to be between a certain amount of steps. An additional benefit would be if I could get the stepper motor to move to a predetermined position at start up, however that is not necessary for my application, only the aforementioned behavior. I apologize in advance for any obvious mistakes as I have zero experience with coding.
Your code used the moveTo function. The moveTo function uses absolute coordinates. If you issue moveTo(8) the stepper will go to 8. If you issue moveTo(8) again, nothing will happen as the stepper is already there. The move function is relative so it will move x steps for each move(x).
You must call run, as often as possible (or any of the non-blocking run functions). The run function will return 0 when the motor has stopped moving at the commanded position.
Appreciate the response. I will try using this library tomorrow. Frankly, I’m only using AccelStepper because it was used in the tutorial I was following.
It is easy to understand and has a good mixture between explaining important concepts and example-codes to get you going. So give it a try and report your opinion about this tutorial.
writing code will become much easier for you and you will proceed faster if you have learned these basiscs.
I second to use the MobaTools library. The big advantage of the mobatools is that the step-pulses are created "in the background"
This enables to run code in parallel to the step-pulse-creation without the need to keep the loop looping fast.
do a single call to the stepper-motor-function and forget
Anyway learning to write non-blocking code is still essential to keep your code responsive to button-presses etc.
Here is a version of a stepper-demo with serial printing that makes visible what the code is doing.
The code has some very useful features like only print if a value has changed.
Such serial printing is essential to analyse the codes behaviour
this is what you see printed
Setup-Start
top of loop will rotate stepper for 8 steps
entering while-loop
distanceToGo()=8
distanceToGo()=7
distanceToGo()=6
distanceToGo()=5
distanceToGo()=4
distanceToGo()=3
distanceToGo()=2
distanceToGo()=1
distanceToGo()=0
while-loop done now waiting
waiting OVER
top of loop will rotate stepper for 8 steps
entering while-loop
distanceToGo()=8
distanceToGo()=7
distanceToGo()=6
distanceToGo()=5
distanceToGo()=4
distanceToGo()=3
distanceToGo()=2
distanceToGo()=1
distanceToGo()=0
while-loop done now waiting
I recommend you to take a look at the basics of how stepper motors work,if you are new to programming, so you understand what the libraries do.
//Need to nitialize stepPin as output.
for (int x = 0; x < steps; x++) {
digitalWrite(stepPin, HIGH);
digitalWrite(stepPin, LOW);
delayMicroseconds(tdelay); //Time between pulses(steps), determines the speed.
}
The code works as intended however I am encountering an issue. I have tested the code for over 30 mins and it worked fine moving the stepper 8 steps and then pausing for 1 second before repeating. However, it seems that upon connecting and disconnecting the com port the stepper motor has now become stuck. Whenever I plug in the arduino the stepper motor attempts to execute the code but the behavior displayed suggests the motor is "stuck" in a spot. It will wait 1 second then attempt to move but all I see is the stepper jiggle in place and then repeat. I have tried resetting the board as well as changing steps/delay but to no avail. Any ideas what might be causing this?
That is on the ragged edge for USB to supply. I really would suggest an external supply that can provide the necessary voltage and at least 1A current.
I believe I have figured it out. Hardware problem on my end. The stepper motor shaft is not completely free to turn on one of my prototype designs leading to the clicking stuck motion when attempting to turn. This suggests I lost some torque from my previous circuit design but I have a different prototype with more clearance. I have changed the speed to 150 steps/s. I have set delay to 30 seconds and will see how the stepper behaves >30 minutes run time.
Does your stepper driver have an adjustment for coil current limit (many modern drivers do)? If so, have you, properly, adjusted the coil current? It is important that you do so.
Stepper motors are effected, in varying degrees, by resonance effects. At certain step frequencies, the motor will jitter and miss steps and even completely stall. I have motors that, running unloaded, will just stop and buzz at 200 steps/sec with no microstepping, but run fine at other frequencies. Using microstepping can mitigate the resonance effects. I usually use at least x4 microstepping.
Also the mechanical drive system can help to damp out resonance. A motor running unloaded is more prone to resonance. Stiff drives like lead screws and gear trains provide less damping and belt drives provide better damping.
I have set the current limit for my driver using the formula (Current = Vref*5). My stepper draws .5A therefore my Vref should be .1 V correct? I am using the STSPIN220 low voltage stepper motor driver. Here is the link.
Ok, that is what I set it to when completing the circuit. As of right now, everything is working as intended. The board is running well over the desired time for my application. Thank you very much for your help, I can now continue with my research.