Using millis() instead of delay within a for loop

im attempting to control a motor for use in a watering system type thing. Anyways I need to use millis() instead of delay due to the number of devices being used throughout the bot. Im trying to do the standard subtraction method within a for loop to make a motor “sweep” speed up rather than jolt up and have to delay the loop each time, ive attached code im using. I run the code and nothing happens so i assume im doing something wrong. Any help?

Sweep1.ino (473 Bytes)

Hi 221970 (Are you one of those Beagle Boys ?), welcome.

Please take some time to read the forum manual (click !).
It explains how (and why) you can help us help you.

I took the effort to download your code and have a look at it.
This was an unnecessary step, because the code is short enough to read it in your post:

#include <Sabertooth.h>

Sabertooth ST(128);
bool go = true;
const unsigned long interval = 20;
unsigned long previousTime = 0;

void setup()
{
  SabertoothTXPinSerial.begin(9600);
  ST.autobaud();

}

void loop()
{
  int power;

  unsigned long currentTime = millis();


  for (power = 0; power <= 127; power ++)
  {
    if (currentTime - previousTime >= interval) {
      ST.motor(1, power);
      previousTime = currentTime;
    }
  }
}

I have no idea what this sabertooth thing is, but it seems to do some serial communication.

Using millis() AKA blink without delay(), isn’t just about avoiding the use of delay().
The code you built this far tells me you didn’t get what “blink without delay” really is about.
So before anything else, study that sketch (blink without delay, it’s in the examples that came with your IDE), make changes to it and see what those changes do (and why they do not work as you expected, which will teach you a lot).

Delay is known as a blocking code, because once this starts, your Arduino will do nothing else than breathe and have a heartbeat while watching the clock as times passes.
But it is not the only way to create a blocking code.
Codes that will lock in a loop are also blocking.
And that is exactly what you are doing in the very small piece of code you’re showing here.
This makes it totally useless to use millis within your for loop.

Delay(), a for… loop and a while… loop are typical examples of blocking code and i’m sure that there many, many more of those.

Once you’ve really mastered the blink without delay example, you’ll see how this can be a real problem in much larger sketches.

Nevertheless, your problem is that you are watching the clock and make a note of the time, then keep watching your note to try to see whether the wanted time has already passed.
How often do you expect the contents of your note to change ?

Once again; solving the problem in the paragraph above, doesn’t solve the problem you wanted to attack.
It will need for you to rethink how to build sketches in an efficient way.

Start practising / learning today, because you will need it in every sketch you’ll build in your future Arduino career.

Lots of success !

Thanks for the reply MAS3! The code i attached it simply a single part of a lot bigger sketch i have been writing but did not want to attach because it is very messy and works mostly outside of this one part im working on. Your right im very unfamiliar with blink without delay, this is something ive only recently started looking at due to this project because of the number of components built in it. I have a basic understanding of it, being get the current time and subtract it to figure how long a delay would be i correlation with how many millisecond from the start of the program. If i understand you correctly a for loop is also stopping? it didnt appear so in the larger program but that could be wrong. If thats the case i would have to drop for loops altogether right? The for loop is within a couple of if statements linked to a keypad to control the motor. I simply want to put space in between each jump up of power to make the motor be a slower start up and the example provided by the sabertooth (which is a controller that controls the motor power, sorry i didnt clarify in the first post) has the for loop and does that exact thing with a delay, im merely modifying the sweep so i can do it within the greater sketch without it interfering with other components. Would there be another way you could recommend me doing this?

Would appreciate the help, i apologize for my ignorance on the subject

Here are some other non-blocking programming (millis()) tutorials.

Several things at a time.
Beginner's guide to millis().

221970:
im attempting to control a motor for use in a watering system type thing. Anyways I need to use millis() instead of delay due to the number of devices being used throughout the bot. Im trying to do the standard subtraction method within a for loop to make a motor "sweep" speed up rather than jolt up and have to delay the loop each time, ive attached code im using. I run the code and nothing happens so i assume im doing something wrong. Any help?

If you are doing things the event-driven way, you don't use loops or delay.

You test for conditions and time-outs completing and take the appropriate actions. The code
is following a state-transition diagram.

loop() is always being called very frequently to do all the checks for transitions, and its the only
looping construct needed for event-driven code. In other contexts it would simply be called
the event loop.

Any other loop-like construct has to be coded up using variables and states explicitly. You can't
assume you have continuous control of the CPU, you have to share it with all the other state-machines
and so your code needs to be broken down into individual fragments for each state-transition.