How to get stepper motor to run for x time

Hello,

I have this code which I found on Google, which runs based on number of steps, but want to not use steps as a method of running. I want to get the stepper motor to stop after x amount of time that I set it to. Any suggestions on how best to accomplish this?

thanks!

void loop() 
{
  spin(150000,100);
  exit(0); 
}

void spin(unsigned long steps, int sspeed)
{
  while(steps>0)
  {
    digitalWrite(3, HIGH);
    delayMicroseconds(40);
    digitalWrite(3, LOW);
    delayMicroseconds(40);
    //delayMicroseconds(sspeed);
    steps--;
  }
}

Hello,

I have this code which I found on Google, which runs based on number of steps, but want to not use steps as a method of running. I want to get the stepper motor to stop after x amount of time that I set it to. Any suggestions on how best to accomplish this?

thanks!

void loop() 
{
  spin(150000,100);
  exit(0); 
}

void spin(unsigned long steps, int sspeed)
{
  while(steps>0)
  {
    digitalWrite(3, HIGH);
    delayMicroseconds(40);
    digitalWrite(3, LOW);
    delayMicroseconds(40);
    //delayMicroseconds(sspeed);
    steps--;
  }
}

Any suggestions on how best to accomplish this?

Save the millis() value at the time that you start the stepper. Then, each time through loop(), check whether the required period has elapsed by subtracting the start time from the millis() value now. If the period has elapsed then act accordingly and maybe save the start time for the next activity. If not, then go round loop() again, perhaps taking other actions and/or reading inputs, but don't block the free running of loop().

Look at the BlinkWithoutDelay example in the IDE and Several things at the same time

See the Blink Without Delay example in the Arduino IDE for the basic idea.
You use the built in timer to time events.

Currently it steps about every 80-some-odd microseconds. Let's call it 86. Divide the number of seconds you want to spin by 86 microseconds and that is the number of steps. Adjust the '86' until you get the right duration.

Do not double post.

but want to not use steps as a method of running.

Get over it. You bought a stepper BECAUSE you wanted to step precisely (or because you were an idiot, but I'm willing to accept that the former is true).

You could easily alter the spin() function so that it operates for an amount of time rather than a number of steps. Have a look at the demo Several Things at a Time which illustrates the use of millis() for timing.

A better solution would be to make a function that just moves 1 step and call it repeatedly until the time expires. That way your stepper function would not block other things that the Arduino may need to do.

The second example in this Simple Stepper Program is very similar to yout spin() function but it uses micros() rather than delayMicroseconds() for timing so that it does not block the Arduino.

...R
Stepper Motor Basics

PaulS:
Get over it. You bought a stepper BECAUSE you wanted to step precisely (or because you were an idiot, but I'm willing to accept that the former is true).

Be nice

UKHeliBob:
Save the millis() value at the time that you start the stepper. Then, each time through loop(), check whether the required period has elapsed by subtracting the start time from the millis() value now. If the period has elapsed then act accordingly and maybe save the start time for the next activity. If not, then go round loop() again, perhaps taking other actions and/or reading inputs, but don't block the free running of loop().

Look at the BlinkWithoutDelay example in the IDE and Several things at the same time

Thanks UKHeliBob!

So I started again from scratch to the simple code that just gets the motor started and runs continuously. I tried added the millis command, but now the motor doesn't move. Any suggestions as to what I should add or remove in my code?

If I leave the interval at 1000 which I believe is a second nothing happens. If I set it to 0 the stepper runs continuously.

Heres the code I am trying now:

#include "DualMC33926MotorShield.h"

int motorDirPin = 2;
int motorStepPin = 3;

unsigned long interval=1000;
unsigned long previousMillis=0;

void setup() {
// put your setup code here, to run once:

Serial.begin(115200);
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);

}

void loop() {
// put your main code here, to run repeatedly:

if ((unsigned long)(millis() - previousMillis) >= interval) {
previousMillis = millis();

digitalWrite(3, !digitalRead(3));

digitalWrite(3, HIGH);
delayMicroseconds(35);
digitalWrite(3, LOW);
delayMicroseconds(10);

} }

Robin2:
You could easily alter the spin() function so that it operates for an amount of time rather than a number of steps. Have a look at the demo Several Things at a Time which illustrates the use of millis() for timing.

A better solution would be to make a function that just moves 1 step and call it repeatedly until the time expires. That way your stepper function would not block other things that the Arduino may need to do.

The second example in this Simple Stepper Program is very similar to yout spin() function but it uses micros() rather than delayMicroseconds() for timing so that it does not block the Arduino.

...R
Stepper Motor Basics

Robin2,

I have written it back to basics and am now using the basic code that runs the stepper at the speed I want, I just can't get it to stop. I introduced the millis command however when I set it to 1 second 1000 in the interval it just doesn't move. When I set the interval value to 0 it runs but doesn't stop. What could I do in this example to make it work?

Thanks much for your input.

This is the code I am trying now:

#include "DualMC33926MotorShield.h"

int motorDirPin = 2;
int motorStepPin = 3;

unsigned long interval=1000;
unsigned long previousMillis=0;

void setup() {
// put your setup code here, to run once:

Serial.begin(115200);
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);

}

void loop() {
// put your main code here, to run repeatedly:

if ((unsigned long)(millis() - previousMillis) >= interval) {
previousMillis = millis();

digitalWrite(3, !digitalRead(3));

digitalWrite(3, HIGH);
delayMicroseconds(35);
digitalWrite(3, LOW);
delayMicroseconds(10);

} }

 if ((unsigned long)(millis() - previousMillis) >= interval)

Most of the time this statement will not be true when interval equals 1000 so the code associated with it will not be executed and the motor will not run. When interval is zero it will be true all of the time so the motor will run all of the time.

You need to keep the motor running by continuously sending the appropriate commands until the interval has elapsed. Unless the motor shield provides this facility you will need to move the motor yourself.

PaulS:
Get over it. You bought a stepper BECAUSE you wanted to step precisely (or because you were an idiot, but I'm willing to accept that the former is true).

You get an A for technical skills, but an F for social skills.
Asperger's, perhaps?

UKHeliBob:

 if ((unsigned long)(millis() - previousMillis) >= interval)

Most of the time this statement will not be true when interval equals 1000 so the code associated with it will not be executed and the motor will not run. When interval is zero it will be true all of the time so the motor will run all of the time.

You need to keep the motor running by continuously sending the appropriate commands until the interval has elapsed. Unless the motor shield provides this facility you will need to move the motor yourself.

UKHeliBob,

Im not sure how to change that statement so that it works. Any chance you can point me in the right direction?Or another way to set a time on the current code that would work?

You need to keep the motor moving if the period has not elapsed. Something like this pseudo code

set up timing variables
save the start time
start of loop()
  if the period has not elapsed
    move the motor one step
  end if
end of loop()

I am not going to contribute any more to this until you get the Moderator to merge it with your other duplicate Thread. Or if that is not possible get one of the Threads locked so everyone can concentrate on the other one. Click "report to moderator"

...R

Robin2:
I am not going to contribute any more to this until you get the Moderator to merge it with your other duplicate Thread. Or if that is not possible get one of the Threads locked so everyone can concentrate on the other one. Click "report to moderator"

...R

Robin2,

Done.

@jpmc, do not cross-post. Threads merged.

Robin2:
I am not going to contribute any more to this until you get the Moderator to merge it with your other duplicate Thread. Or if that is not possible get one of the Threads locked so everyone can concentrate on the other one. Click "report to moderator"

...R

Robin2,

Now that the thread is merged. Can you help me come up with a way to stop this pump?

jpmc:
Now that the thread is merged. Can you help me come up with a way to stop this pump?

I will certainly try. To avoid confusion it would be a good stepping off point if you now post your latest code and describe what it does and what it should do. And please use the code button </> so your code looks like this and is easy to copy to a text editor.

...R