I have trouble with my code and the millis function.
I have studied BlinkWithoutDelay and many other examples.
On it for 2 days now. So i hope some can help me out ... again.
Its a simple code i want the motor to run in one direction for 3000 steps.
Then wait for, lets say 5 seconds, and then run again 3000 steps in the same direction.
Issue: the motor runs the first 3000 steps but after the wait statement it does nothing.
What am i not getting here?
By the way i have tried using millis() directly without a code but get the same result!
#include <elapsedMillis.h>
#include <AccelStepper.h>
AccelStepper stepperA(AccelStepper::DRIVER, 3,4);
elapsedMillis timeElapsed; //declare global if you don't want it reset every time loop runs
// delay in milliseconds
unsigned int interval = 5000;
void setup()
{
}
void loop()
{
stepperA.setMaxSpeed(4000);
stepperA.setAcceleration(4000);
stepperA.moveTo(3000);
stepperA.run();
if (timeElapsed > interval)
{
stepperA.setMaxSpeed(4000);
stepperA.setAcceleration(4000);
stepperA.moveTo(3000);
stepperA.run();
timeElapsed = 0; // reset the counter to 0 so the counting starts over...
}
}
I suspect that using smarter names for variables would cause a light to go on. currentMillis is a stupid name for now. previousMillis is a stupid name for when some event happened.
Use names that mean something!
The run() function needs to be called on EVERY pass through loop(), NOT just when you feel like calling it.
PaulS:
I suspect that using smarter names for variables would cause a light to go on. currentMillis is a stupid name for now. previousMillis is a stupid name for when some event happened.
Looking at the code in Reply #2 I believe you need the line
previousMillis() = millis() at the start of the timing period. Something like this. And note that I have used a different name for the variable for added clarity
Try this version - and study the changes I have made
#include <AccelStepper.h>
AccelStepper stepperA(AccelStepper::DRIVER, 3,4);
unsigned long currentMillis;
unsigned long endFirstRunMillis;
const long interval = 1000;
boolean waitingToStartRun2 = false;
//==========
void setup() {
stepperA.setMaxSpeed(4000);
stepperA.setAcceleration(4000);
}
//==========
void loop() {
currentMillis = millis();
stepperA.moveTo(3000);
if (stepperA.distanceToGo == 0) {
endFirstRunMillis = currentMillis;
waitingToStartRun2 = true;
}
if (currentMillis - endFirstRunMillis >= interval and waitingToStartRun2 == true)
{
waitingToStartRun2 = false;
stepperA.moveTo(3000); // shouldn't this be 6000?
}
stepperA.run();
}
By the way I am not sure what you want to happen when run2 finishes and I have made no attempt to deal with that
If you want to use acceleration then the value for setAcceleration() should be much lower than the value for maxSpeed().
If you don't wish to use acceleration then use runSpeed() in place of run()
Thank for the relays and help guys! i appreciate it.
I'm understand the logic of your code Robin. (thanks for your time)
Now it seems that this code results in the same way my code did.
After the first run succesfully completes, the motor stops and doesn't move any more.
I can see with the serial monitor that the code does go throu first run. But never gets in the second run.
See code below. Yes it should be 6000, Robin. i was testing with move() there.
IT WORKS. Now i have to figure out how this version of your code works.
Have to implement it into my "real" skatch. But i think it should not be a problem.
Kumalix:
IT WORKS. Now i have to figure out how this version of your code works.
Good to hear.
I reckon the only unusual part of my program is the use of an ENUM. Google C++ ENUM and learn all about it.
If you Serial.print() the ENUM constants you will find that they just represent values 0,1,2,3 etc. But by giving them names it makes it more obvious what is going on.
...R
PS... Just ignore (and delete, if you wish) the comments at the top of my example. They are just instructions for the program I use to compile my code. I forgot to delete them before posting.