how to make individual stepper motors delay at different times, same loop

Ok I have a program where I am controlling 6 individual stepper motors, I'm using 3 motors per arduino, each having their own driver. The program is working fine but I'm wanting to have the motors delay/stop for a set amount if time, each being different. If I use the DELAY function all six follow the same delay.

Is there any way to individually delay each one in the same loop?

Look at the "blink without delay" example in the IDE (File, Examples, Digital). This method is great for timing multiple events (not just LEDs) at different times.

Sounds like you need timers to control your motors. I am surprised that the Arduino libraries don't include software timers. One of the first things I did when I first got my UNO was to write a timer library.

Start by looking at the blink without delay example.

http://arduino.cc/en/Tutorial/BlinkWithoutDelay

I wrote an extended example of the Blink Without Delay technique in the first post in this Thread.

...R

Hi everyone i have one problem with relay because i want turn on two realy in different time in example turn on first relay delay(1000) turn on second but when i use delay in betwenn relay delay function stopped function my menu unitl delay finished i used this statment form:

if(digitalWrite(relayOne,HIGH)){ delay(1000); digitalWrite(relayTwo,HIGH);

} any Ideas how to do this thanks!

Have you actually read this thread ? Have you looked at the BlinkWithoutDelay example in the IDE ? Perhaps rename it RelaysWithoutDelay and set up two intervals instead of one.

Same issue as my earlier post, how to delay each motor individually in the same loop.

See my attachment/code written for this program. Program works as designed. I just need help delaying each one individually.
Ive read the replies to my earlier post but like i said im new to C language and code. Trying hard to learn.

Thank everyone for the support.

#include <AccelStepper.h>

int motorSpeed = 500; //maximum steps per second (about 3rps / at 16 microsteps)
int motorAccel = 300; //steps/second/second to accelerate
int SWilson = 500;
int count = 0;

int motorDirPin1 = 3; //digital pin 3
int motorStepPin1 = 2; //digital pin 2

int motorDirPin2 = 7; 
int motorStepPin2 = 6;

int motorDirPin3 = 11; 
int motorStepPin3 = 10;



//set up the accelStepper intance
//the "1" tells it we are using a driver
AccelStepper stepper1(1, motorStepPin1, motorDirPin1);

AccelStepper stepper2(1, motorStepPin2, motorDirPin2);

AccelStepper stepper3(1, motorStepPin3, motorDirPin3);

void setup(){
  stepper1.setMaxSpeed(motorSpeed);
  stepper1.setSpeed(motorSpeed);
  stepper1.setAcceleration(motorAccel);
  
  stepper2.setMaxSpeed(motorSpeed);
  stepper2.setSpeed(motorSpeed);
  stepper2.setAcceleration(motorAccel);
  
  stepper3.setMaxSpeed(motorSpeed);
  stepper3.setSpeed(motorSpeed);
  stepper3.setAcceleration(motorAccel);
  
 pinMode(4, OUTPUT); // Optical Switch for Driver 1
 pinMode(4, HIGH);
 
 pinMode(8, OUTPUT); // Optical Switch for Driver 2
 pinMode(8, HIGH);
 
 pinMode(12, OUTPUT); // Optical Switch for Driver 3
 pinMode(12, HIGH);
 
 pinMode(5, OUTPUT);   // Enable, stop energizing
 pinMode(9, OUTPUT);
 pinMode(13, OUTPUT);
 
 

}

void loop()
{

  if(count < SWilson){
    
    
   //STEPPER 1
   

    stepper1.runToNewPosition(0);
    stepper2.runToNewPosition(0);
    stepper3.runToNewPosition(0);

    digitalWrite(4, HIGH);  // optical switch for driver
    digitalWrite(8, HIGH); 
    digitalWrite(12, HIGH); 
    


    delay(500);
    digitalWrite(5, LOW);      // stop energizing 
    digitalWrite(9, LOW); 
    digitalWrite(13, LOW);   
    delay(900000);               // 15 minutes 900000     OFF
    digitalWrite(5, HIGH);       // begin energizing
    digitalWrite(9, HIGH);
    digitalWrite(13, HIGH);
  
    
    
    stepper1.runToNewPosition(20);
    stepper2.runToNewPosition(140);
    stepper3.runToNewPosition(140);



    delay(500);
   digitalWrite(5, LOW);      // stop energizing 
    digitalWrite(9, LOW); 
    digitalWrite(13, LOW);   
    delay(1800000);               // 15 minutes 900000       HIGH
    digitalWrite(5, HIGH);       // begin energizing
    digitalWrite(9, HIGH);
    digitalWrite(13, HIGH);
    
    
    
    stepper1.runToNewPosition(175);
    stepper2.runToNewPosition(0);
    stepper3.runToNewPosition(0);
   
   
    delay(500);
   digitalWrite(5, LOW);      // stop energizing 
    digitalWrite(9, LOW); 
    digitalWrite(13, LOW);   
    delay(900000);               // 15 minutes 900000     LOW
    digitalWrite(5, HIGH);       // begin energizing
    digitalWrite(9, HIGH);
    digitalWrite(13, HIGH);




    count++; //add (1) to our count
  }

}

New_Drivers_2.ino (2.73 KB)

Same issue as my earlier post, how to delay each motor individually in the same loop.

So why start another thread ?

Get rid of all the delays. Use the principle exemplified by the BlinkWithoutDealy example in the IDE. Note the start time and check every time through the loop() function whether it is time to do take action. If not do something else such as checking whether it is time to take another action, or 5

I'm hoping the moderator will merge your new Thread with this one. It just confuses everyone to split the discussion.

Looking at the code in your other Thread you don't appear to have taken on board the concept in the Blink Without Delay (BWoD) sketch. If you don't understand something tell us and we will try to help.

To manage the timing of several different things will require a significant rethink of how you design your program but it is hard to explain how until you start thinking in terms of BWoD. I reckon that the example I referred to earlier could be modified to deal with your requirement without too much trouble.

I suggest you write a short sketch to control one motor using the BWoD technique and then extend it to deal with extra motors.

...R

i really dont understand the concept of using these : could someone break these down, like i said im new to programming my apologies for the ignorance

long previousMillis = 0; why do we have to use long, what does it represent

long interval = 1000; -----

unsigned long currentMillis = millis(); ------

if(currentMillis - previousMillis > interval) { ------

previousMillis = currentMillis; -----

again thanks for the help

Huskywonderboff: long previousMillis = 0; why do we have to use long, what does it represent

Actually, you should use unsigned long. Have you read about millis() in the Reference section?

Anyway, a call to millis() will return an unsigned long, and the value it returns started at 0 at the time of the latest reset. Eventually it will overflow. Using a long (16 bit variable) means that millis() will overflow after 4.294.967.295 milliseconds, becoming zero, after about 49.7 days. We use unsigned because we don't ever want it to become negative. A long (signed) will become negative as soon as the high order bit becomes 1, which it will after a count of 2,147,483,648.

So, here's how you do it

Suppose you want to turn on a LED when something happens (perhaps a switch event), and turn it off 4.3 seconds after the switch is released. For this example we will assume no switch bounce, and that it is hels down for less than 1 second. Because of the way unsigned arithmetic works, the correct interval will be right even though there was an overflow between the start time and start time + interval.

int interval = 43000; // 43,000 ms = 4.3 seconds
unsigned long currentMillis;
const byte switchPin = 5;
const byte LEDPin = 13;

void setup() {
  pinMode(switchPin,INPUT_PULLUP);
  pinMode(LEDPin, OUTPUT); // Arduino Uno has LED on pin 13
}

void loop() {
  if (switchPin == LOW) {     // switch pressed
    digitalWrite(LEDPin, HIGH);
    currentMillis = millis(); // time when switch was last LOW
  }
  
  if (millis() - currentMillis >= interval) {
    // after the switch was released, we stopped putting new values into currentMillis
    // we will test elapsed milliseconds continuously in loop()
    // and eventually, we will land here after 43000 ms have passed
    digitalWrite(LEDPin, LOW);  // turn LED off
  }
  
  // here we can have other things that are done at various times.
  // for each thing we want to time, we will need two variables.
  // One will hold the starting millis() value
  // and the other will hold the interval.
}

Huskywonderboff: i really dont understand the concept of using these : could someone break these down, like i said im new to programming my apologies for the ignorance

long previousMillis = 0; why do we have to use long, what does it represent

long interval = 1000; -----

unsigned long currentMillis = millis(); ------

if(currentMillis - previousMillis > interval) { ------

previousMillis = currentMillis; -----

again thanks for the help

The word before a variable definition (in this case long or unsigned long) tells the compiler what sie the variable should be. A byte use 1 byte of space and can store values from 0 - 255. An int uses 2 bytes. A long uses 4 bytes and can store a value from -2,147,483,648 to +2,147,483,648. An unsigned long also uses 4 bytes but only stores positive numbers from 0 to 4,294,967,295.

The millis() function returns an unsigned long value so it keeps increasing its count up to 4294967295 after which it "rolls over" to 0 and starts counting up again.

The general timing idea using millis() is the same as noting the time when you put a chicken in the oven and checking from time to time if the cooking time has elapsed. That's what "currentMillis - prevMillis > interval" does. Interval is equivalent to the cooking time and prevMillis was the ime you put the chicken in the oven :)

By using subtraction (as in currentMillis - prevMillis) you get the correct answer even when millis() rolls over back to 0.

It's marginally more accurate to use "prevMillis = prevMillis + interval" rather than "prevMillis = currentMillis" if you need to update the time for successive timing periods.

...R