How to run a stepper motor without delay?

  1. I want to add this code into my existing code but with out delay()

  2. Or can I use multiple loops? like; void loop() void loop1() void loop2()

Also, can I trigger each loop to start and stop independently with diffident delays in each but also let others run?

int delaylegnth = 1000;

void setup() {
  
  //establish motor direction toggle pins
  pinMode(12, OUTPUT); //CH A -- HIGH = forwards and LOW = backwards???
  pinMode(13, OUTPUT); //CH B -- HIGH = forwards and LOW = backwards???
  
  //establish motor brake pins
  pinMode(9, OUTPUT); //brake (disable) CH A
  pinMode(8, OUTPUT); //brake (disable) CH B


  
  
}

void loop(){

  digitalWrite(9, LOW);  //ENABLE CH A
  digitalWrite(8, HIGH); //DISABLE CH B

  digitalWrite(12, HIGH);   //Sets direction of CH A
  analogWrite(3, 255);   //Moves CH A
  
  delay(delaylegnth);
  
  digitalWrite(9, HIGH);  //DISABLE CH A
  digitalWrite(8, LOW); //ENABLE CH B

  digitalWrite(13, LOW);   //Sets direction of CH B
  analogWrite(11, 255);   //Moves CH B
  
  delay(delaylegnth);
  
  digitalWrite(9, LOW);  //ENABLE CH A
  digitalWrite(8, HIGH); //DISABLE CH B

  digitalWrite(12, LOW);   //Sets direction of CH A
  analogWrite(3, 255);   //Moves CH A
  
  delay(delaylegnth);
    
  digitalWrite(9, HIGH);  //DISABLE CH A
  digitalWrite(8, LOW); //ENABLE CH B

  digitalWrite(13, HIGH);   //Sets direction of CH B
  analogWrite(11, 255);   //Moves CH B
  
  delay(delaylegnth);

}
  1. I want to add this code into my existing code but with out delay()

You need to wait until the stepper has moved before you tell it to step again. How do you propose to do that?

  1. Or can I use multiple loops? like; void loop() void loop1() void loop2()

Sure, but you will need to call the others from inside loop().

Also, can I trigger each loop to start and stop independently with diffident delays in each but also let others run?

No. You need to read, understand, and embrace the blink without delay example.

You need to use the technique exemplified in the BlinkWithoutDelay example sketch - ie avoid using delay completely.

Brilliant! Thanks chaps 8)

Using an Mega2560 with Arduino Motor ShieldV3

I have a 8 wire stepper motor and all working fine with this code,
http://docs-europe.electrocomponents.com/webdocs/001c/0900766b8001c018.pdf

int delaylegnth = 60;

void setup() {
  pinMode(12, OUTPUT); //CH A -- HIGH = forwards and LOW = backwards???
  pinMode(13, OUTPUT); //CH B -- HIGH = forwards and LOW = backwards???
  pinMode(9, OUTPUT); //brake (disable) CH A
  pinMode(8, OUTPUT); //brake (disable) CH B
}

void loop(){
 
  digitalWrite(9, LOW);  //ENABLE CH A
  digitalWrite(8, HIGH); //DISABLE CH B
  digitalWrite(12, HIGH);   //Sets direction of CH A
  analogWrite(3, 255);   //Moves CH A 
  delay(delaylegnth);  
  digitalWrite(9, HIGH);  //DISABLE CH A
  digitalWrite(8, LOW); //ENABLE CH B
  digitalWrite(13, LOW);   //Sets direction of CH B
  analogWrite(11, 255);   //Moves CH B
  delay(delaylegnth);
  digitalWrite(9, LOW);  //ENABLE CH A
  digitalWrite(8, HIGH); //DISABLE CH B
  digitalWrite(12, LOW);   //Sets direction of CH A
  analogWrite(3, 255);   //Moves CH A 
  delay(delaylegnth);  
  digitalWrite(9, HIGH);  //DISABLE CH A
  digitalWrite(8, LOW); //ENABLE CH B
  digitalWrite(13, HIGH);   //Sets direction of CH B
  analogWrite(11, 255);   //Moves CH B
  delay(delaylegnth);
}

Trying the millies() and not having much luck.

int delaylegnth = 1000;
long previousMillis = 0;  
long interval = 1000; 

void setup() {
  pinMode(12, OUTPUT); //CH A -- HIGH = forwards and LOW = backwards???
  pinMode(13, OUTPUT); //CH B -- HIGH = forwards and LOW = backwards???
  pinMode(9, OUTPUT); //brake (disable) CH A
  pinMode(8, OUTPUT); //brake (disable) CH B 
}
void loop(){
   unsigned long currentMillis = millis();
    if(currentMillis - previousMillis > interval) {
   previousMillis = currentMillis;  
    }
  digitalWrite(9, LOW);  //ENABLE CH A
  digitalWrite(8, HIGH); //DISABLE CH B
  digitalWrite(12, HIGH);   //Sets direction of CH A
  analogWrite(3, 255);   //Moves CH A  
  millis();  
  digitalWrite(9, HIGH);  //DISABLE CH A
  digitalWrite(8, LOW); //ENABLE CH B
  digitalWrite(13, LOW);   //Sets direction of CH B
  analogWrite(11, 255);   //Moves CH B  
  millis();  
  digitalWrite(9, LOW);  //ENABLE CH A
  digitalWrite(8, HIGH); //DISABLE CH B
  digitalWrite(12, LOW);   //Sets direction of CH A
  analogWrite(3, 255);   //Moves CH A 
  millis();   
  digitalWrite(9, HIGH);  //DISABLE CH A
  digitalWrite(8, LOW); //ENABLE CH B
  digitalWrite(13, HIGH);   //Sets direction of CH B
  analogWrite(11, 255);   //Moves CH B
  millis();
}

What have I not done?

    if(currentMillis - previousMillis > interval) {
   previousMillis = currentMillis;  
    }

This is where you also need to take another step.

  millis();

Call a function to get the time. Discard the time that you just got. Doesn't seem very useful to me.

Somewhere above, I said you needed to read, understand, and embrace the blink without delay example. You seem to have managed only one of those three tasks.

You need a step() function that causes the stepper motor to take a step. That function needs to know which step it is at, so it can bang the right set of pins to make the next step happen. The rest of the code should NOT know that. Hint: a static variable in the function.

You need to call that method when the required amount of time, since the last step, has elapsed. That function needs to record when the last step happened.

Could you please give me an example Paul? I need to see the full code to work it out so I can understand it and learn more you.

Could you please give me an example Paul?

    if(currentMillis - previousMillis > interval)
    { // Moved down here where it belongs
       step();
       previousMillis = currentMillis;  // indented properly
    }
void step()
{
   static int lastStep = 0;
   switch(lastStep)
   {
      case 0:
         // First step code
         break;
      case 1:
         // Second step code
         break;
      case 2:
         // Third step code
         break;
      case 3:
         // Fourth step code
         break;
   }
   lastStep++;
   if(lastStep > 3)
     lastStep = 0;
}

You already have the first, second, third, and fourth step code. First step:

  digitalWrite(9, LOW);  //ENABLE CH A
  digitalWrite(8, HIGH); //DISABLE CH B
  digitalWrite(12, HIGH);   //Sets direction of CH A
  analogWrite(3, 255);   //Moves CH A

You can find and move other three step stuff.

Brilliant, thank you Paul. I'll return the full code in a sec for you moderation ;)

And here it is! It works perfectly, thanks.

int delaylegnth = 1000;
long previousMillis = 0;  
long interval = 2; 

void setup() {
  pinMode(12, OUTPUT); //CH A -- HIGH = forwards and LOW = backwards???
  pinMode(13, OUTPUT); //CH B -- HIGH = forwards and LOW = backwards???
  pinMode(9, OUTPUT); //brake (disable) CH A
  pinMode(8, OUTPUT); //brake (disable) CH B 
}
void step()
{
  static int lastStep = 0;
  switch(lastStep)
  {
  case 0:
    digitalWrite(9, LOW);
    digitalWrite(8, HIGH);
    digitalWrite(12, HIGH);
    analogWrite(3, 255);
    break;
  case 1:
    digitalWrite(9, HIGH);
    digitalWrite(8, LOW);
    digitalWrite(13, LOW);
    analogWrite(11, 255);
    break;
  case 2:
    digitalWrite(9, LOW);
    digitalWrite(8, HIGH);
    digitalWrite(12, LOW);
    analogWrite(3, 255);
    break;
  case 3:
    digitalWrite(9, HIGH);
    digitalWrite(8, LOW);
    digitalWrite(13, HIGH);
    analogWrite(11, 255);
    break;
  }
  lastStep++;
  if(lastStep > 3)
    lastStep = 0;
}
void loop(){
  unsigned long currentMillis = millis();
  if(currentMillis - previousMillis > interval)
  { // Moved down here where it belongs: Got ya.
    step();
    previousMillis = currentMillis;  // indented properly: Thanks Paul
  }
}

Question? How dose myDelay work?

Would it be the same as delay();

so would,

delay(1000); be the same as, myDelay(1000);

I have now added an LED just see how the code would look.

How would I blink the LED in diffident intervals to the speed of the stepper motor, I have try'd but I'm stumped...

here is the same code but what I have try'd.

const int ledPin =  40;
int ledState = LOW;  
int delaylegnth = 1000;
long previousMillis = 0;  
long interval1 = 30; 
long interval2 = 30; 
void setup() {
  pinMode(ledPin, OUTPUT);   
  pinMode(12, OUTPUT); //CH A -- HIGH = forwards and LOW = backwards???
  pinMode(13, OUTPUT); //CH B -- HIGH = forwards and LOW = backwards???
  pinMode(9, OUTPUT); //brake (disable) CH A
  pinMode(8, OUTPUT); //brake (disable) CH B 
}
void step()
{
  static int lastStep = 0;
  switch(lastStep)
  {
  case 0:
    digitalWrite(9, LOW);
    digitalWrite(8, HIGH);
    digitalWrite(12, HIGH);
    analogWrite(3, 255);
    break;
  case 1:
    digitalWrite(9, HIGH);
    digitalWrite(8, LOW);
    digitalWrite(13, LOW);
    analogWrite(11, 255);
    break;
  case 2:
    digitalWrite(9, LOW);
    digitalWrite(8, HIGH);
    digitalWrite(12, LOW);
    analogWrite(3, 255);
    break;
  case 3:
    digitalWrite(9, HIGH);
    digitalWrite(8, LOW);
    digitalWrite(13, HIGH);
    analogWrite(11, 255);
    break;
  }
  lastStep++;
  if(lastStep > 3)
    lastStep = 0;
}
void loop(){
  unsigned long currentMillis = millis();
  if(currentMillis - previousMillis > interval1)
  if(currentMillis - previousMillis > interval2)
  { // Moved down here where it belongs: Got ya.
    step();
    previousMillis = currentMillis;  // indented properly: Thanks Paul
        if (ledState == LOW)
      ledState = HIGH;
    else
      ledState = LOW;

    // set the LED with the ledState of the variable:
    digitalWrite(ledPin, ledState);
  }
}

Thanks,

  if(currentMillis - previousMillis > interval1)
  if(currentMillis - previousMillis > interval2)

If you ALWAYS used curly braces after an if statement, you'd see the problem.

As in,

  if(currentMillis - previousMillis > interval1);
  if(currentMillis - previousMillis > interval2);

?? :)

These are curly braces: { and } This is not: ;