Run multiple 'functions' simultaneously in loop

Hi,
im trying to understand how to run more then one command in the loop. Or more specific run two steppers at the same time. This is the starting point. Later ill add 6 steppers each with a rotary encoder for positioning, all motors will be controlled with a sensor… But right now im trying to understand the basics of running various functions simultaneously. now each motor take x-amout of steps then the next motor is triggered.

How can i make the program under run both motors at once? (im using a motor shield (mshield) but will switch to A4988 Stepper Motor Driver Carrier. Perhaps the loop isn’t the way of working?
Best
G

#include <AFMotor.h>

AF_Stepper motor1(200, 1);
AF_Stepper motor2(200, 2);

int randNumber;
void setup() {

  Serial.begin(9600);           // set up Serial library at 9600 bps
  Serial.println("Stepper test!");

}

void motor1runforward() {
  randNumber = random(200, 2000);

  motor1.step( randNumber, FORWARD, SINGLE); 
  delay(1500);
  motor1runbackward();
}

void motor1runbackward() {
  randNumber = random(200, 2000);
  motor1.step( randNumber, BACKWARD, SINGLE); 
  delay(1500);
}

void motor2runforward() {
  randNumber = random(200, 2000);
  motor2.step( randNumber, FORWARD, SINGLE); 
  delay(1500);
  motor2runbackward();
}

void motor2runbackward() {
  randNumber = random(200, 2000);
  motor2.step( randNumber, BACKWARD, SINGLE); 
  delay(1500);
}

void loop() {
  motor1runforward();
  motor2runforward();
}

How can i make the program under run both motors at once?

Do not use delay. Do not use a motor driver library that will not cope with asynchronous operation ( I think that is all of them but as you haven't included a link to the one you are using I can't tell )

Write the code yourself based on the blink without delay technique.

http://www.ladyada.net/make/mshield/use.html link to the shield. Im not sure what you mean, will it help if i remove the delay? If i switch to the stepper driver, will it then work, i mean with the same principle of calling the funcitons in the loop?

You use

#include <AFMotor.h>

So I was asking for a link to this library to see if it is capable of driving more than one motor at the same time.

will it help if i remove the delay?

Just removing them no. Rewriting the code so they are not used yes providing your library is capable, which I doubt.

Or more specific run two steppers at the same time.

Study the blink without delay example code already built into your IDE. Study it, understand it and then
extend it to control two LEDs. In place of turning an LED on and off you step the appropriate motor.

My bad i read it wrong, thought you asked for the name of the shield. Ill study the blink example… I dont have the arduino next to me, so by curiosity ill ask this; would this run both funcitons at once, or would it wait untill the first function is executed then run the second?

void loop() {
  funcOne();
  funcTwo();
}

It would wait untill the first function is executed then run the second.

It does one, then the other. Read this:

http://www.gammon.com.au/blink

[quote author=Nick Gammon link=topic=151518.msg1137774#msg1137774 date=1362094215] It does one, then the other. Read this:

http://www.gammon.com.au/blink

[/quote]

thanks!! that was great, not sure i get it completely; but would this be correct, i can run multiple if statements in the loop at once? e.g

int var1;
int var2;
var1 = va2;

void loop() {
if (var1 == var2)
  funcTwo();
if (var1 == var2)
  funcOne();

}

would run them at the same time? or do i need to go back to the schoolbook?

Not at the same time. The idea is that you start things off and then monitor them (like cooking dinner). You start motor A. Then start motor B. Then read a sensor, etc.

The AccelStepper library can run steppers simultaneously. The others step them one at a time.

Thanks for all replies.

Im still struggling with understanding the part of monetoring the various functions.
This works, (kind of, the motor gets really hot and makes alot more noice then when i used the shield). Would someone be interested hint how to add a second stepper (to act as the first) based on the monetoring explenation Nick meantioned, as you see this is NOT as he explained.

So id like to add
Step_Two
Dir_Two

Best

  void loop() {
  unsigned int uS = sonar.ping(); // Send ping, get ping time in microseconds (uS).
  Serial.print("Ping: ");
  Serial.print(uS / US_ROUNDTRIP_CM); // Convert ping time to distance in cm and print result (0 = outside set distance range)
  Serial.println("cm");
  
  Speed_X = 200;  

  distance = uS / US_ROUNDTRIP_CM;

  if (distance<50&&distance>30){
    digitalWrite(Dir_One, HIGH);  // Set direction
    digitalWrite(Step_One,HIGH);
    delayMicroseconds(2);
    digitalWrite(Step_One,LOW);
    delayMicroseconds(Speed_X);
    digitalWrite(10, HIGH);   // set the LED on

  }

    if (distance<30&&distance>10) {
    digitalWrite(Step_One, LOW);  // Other direction
    digitalWrite(Dir_One,HIGH);
    delayMicroseconds(2);
    digitalWrite(Step_One,LOW);
    delayMicroseconds(Speed_X);
    digitalWrite(10, LOW);    // set the LED off

  }

}

No that code can not be changed to run two stepping motors at the same time. You are not understanding the basis of the blink without delay sketch.

blink without delay would result in nothing, since it's looped thru in rapid speed.!?

gerbsi: blink without delay would result in nothing, since it's looped thru in rapid speed.!?

The check to see if it's time to take action is "looped thru in rapid speed", but the actions taken aren't.

So the led would then be lit !? Since it doesent know when to take next action?

Sorry not sitting with arduino now. Did som tests earlier today and thought i got it. Right now im trying to understand the theory of if. Thanks for all input!

gerbsi: blink without delay would result in nothing, since it's looped thru in rapid speed.!?

No you do not understand it. The blink without delay makes a note of the time now and when something needs to be done. Then the loop goes very fast seeing if it is time to do the task. If it is you do the task and set the time to do it again. It mattes not if the task is blinking an LED or stepping a motor. If you do this with two tasks you have your two stepping motors running at the same time at speeds set interdependently.

gerbsi: So the led would then be lit !? Since it doesent know when to take next action?

Sorry not sitting with arduino now. Did som tests earlier today and thought i got it. Right now im trying to understand the theory of if. Thanks for all input!

You really should be playing around with the blink without delay example. Changing things and see how it affects the behavior. Only when you fully understand the concept, can you apply it to what you are trying to do.

gerbsi: blink without delay would result in nothing, since it's looped thru in rapid speed.!?

Look, if you are boiling an egg and frying some bacon "at the same time" that doesn't "result in nothing" because you are "looping through in rapid speed" does it?

You put the egg on. That's one thing. You put the bacon on. That's another thing. They are now cooking together. They only thing you do "rapidly" is check your watch to see if they are done.