If I tell it to move 10 steps, and then on the very next loop the conditions are met for it to move -5 steps, will it finish out the 10 before going back 5 steps in the other direction, or will it stop what it is doing and follow the newest command?
This function is blocking; that is, it will wait until the motor has finished moving to pass control to the next line in your sketch. For example, if you set the speed to, say, 1 RPM and called step(100) on a 100-step motor, this function would take a full minute to run. For better control, keep the speed high and only go a few steps with each call to step().
so in that case the answer to your question is yes it will finish the 10 before doing the -5, it will actually finish the 10 without giving a chance for your code to run anything else if you called myStepper.step(10);
If you have a loop() doing just single steps, then your code controls what happens by testing conditions and it’s up to you to interrupt the current flow to do something else
If you use AccelStepper then you can use API functions that never delay() or block. See how move(), moveTo() and run() work
If you use the built-in step() function then you’ll see that the doc states so in that case the answer to your question is yes it will finish the 10 before doing the -5, it will actually finish the 10 without giving a chance for your code to run anything else if you called myStepper.step(10);
If you have a loop() doing just single steps, then your code controls what happens by testing conditions and it’s up to you to interrupt the current flow to do something else
If you use AccelStepper then you can use API functions that never delay() or block. See how move(), moveTo() and run() work
I will have to look into this.
For now I am using the built-in stepper library.
I'm not home to grab the code from my laptop.
Mainly these are just to test, but one of them may end up in a practical application.
I'm using steppers to control flow mainly. A screw it attached to the stepper that moves in and out to block a flow path.
On one stepper I'm measuring temperature (cooling system)
On another I'm measuring flow.
The first one checks for temperature. I also set a target point.
If the temperature is above/below the target point, it will add/subtract 1 step via myStepper.step(1);
The problem is, the loop operates much faster than the stepper can react, so it quickly piles up the steps before the temperature hits the target, so the temperature overshoots the target, and then the process starts all over again in the opposite direction. No matter what I do the stepper motor quickly goes from minimum steps to maximum, and back again. That's why I asked if it would over-write.
I can't add a delay, because it would also delay the reading of the temperature, which must be done as quickly as possible.
JoeNova:
The problem is, the loop operates much faster than the stepper can react, so it quickly piles up the steps before the temperature hits the target, so the temperature overshoots the target, and then the process starts all over again in the opposite direction. No matter what I do the stepper motor quickly goes from minimum steps to maximum, and back again. That's why I asked if it would over-write.
This is the same problem as in your other Thread where I have already commented on it.
Please don't double post as it just wastes everyone's time.
Since I solved my motor issues in the motor section, and am on to the programming part, I figured it would make more sense to post in the programming questions section. I'm only wasting your time if you reply to both, not anyone else's.
And since the archaic security system here at work makes all of the web pages look text based, its hard to keep track of certain things, view images, attachments, bookmarks, etc.
JoeNova:
For now I am using the built-in stepper library.
JoeNova:
...it will add/subtract 1 step via myStepper.step(1);
The problem is, the loop operates much faster than the stepper can react, so it quickly piles up the steps before the temperature hits the target, so the temperature overshoots the target,....
Have you read what I wrote above? If you use the built in library the loop does not operate much faster than the stepper because the step call will wait for the stepper to complete its step before going back to the loop...
So you have a logic or other type of bug in your code which you have not posted so can’t help....
J-M-L:
Have you read what I wrote above? If you use the built in library the loop does not operate much faster than the stepper because the step call will wait for the stepper to complete its step before going back to the loop...
I meant physically. The loop is very quick, the stepper is physically slow.
I trimmed down my code a LOT to get rid of the useless parts, I hope I didn't butcher it too much.
Here is what I'm using.
The problem is that it keeps removing steps even though flow has started to fall. By the time I hit target, it has removed enough steps to overshoot the target. It then stops, and start working on the opposite direction. I've tried using delay(), but it also delays the reading of the flow, and I can't under any circumstance flow down the inputs.
Here is an image attached of the plotter. You can see the steps in blue go up/down.
The green is the target flow and red is the measured flow.
I tried implementing a loop counter, and set it so that the stepper will only be allowed to move every X loops.
It works somewhat. The fluctuations in flow aren't nearly as bad, but I still need a more solid way of keeping flow steady.
Yes running average could be good enough for what you do; you can see PID as running average on steroid
The other thing that you need to think about is if your stepper is not too coarse to drive the behavior you want? I don’t see the scale for your blue line in the sceeen capture above - seems that your flow undershoot with a very small change in the blue line and then there is tremendous inertia to go back down (is servo strong enough to block the flow?) -> how many ticks do you have between the low value and the blue peak ?
360 steps between low and peak. The blue line is the numbers of steps.
This stepper was originally used for this exact purpose. The old controller is burned out and there is no replacement available. I'm simply trying to drive the stepper that was already intended for the system.
PIDs will work better than running averages because I'm attempting to get a single nano to run the entire system, and all of the analog inputs are currently in use as well as a few digital ones. I just can't allow the delay() in the stepper movements because slowing down the inputs would be very very bad for the other things being done.