Controlling DC Motors and Steppers with different patterns simultaneously

Hi guys !

I'm learning Arduino programming for an Art project I have, but I have some troubles to understand how to get my motors to work simultaneously on different patterns.

The idea of my project is to get some fake plants to move on a platform, to make it like a choreography.

I have an "Arduino Mega" to pilot 4 "9V DC Motors (plugged on an Adafruit Motorshield V2)" and 5 "Chinese Steppers (plugged in 5 TB6600 drivers)".

What I achieve, for now, is to make a code that activate one motor after the other, with delay. But I think there is a way to write a different program for each motor and make them run simultaneously. I've learned about "blink without delay" and array. As I achieve to make it work with LED, I can't manage to transpose it for motors. Can you help ?

Thanks in advance !
Flo

Simple example of what I imagined :

Program 1 : DCMotor1 / on : forward - 10sec / off : 10 sec / on : backward - 5 sec / off : 20 sec
Program 2 : DCMotor2 / on : forward - 20sec / off : 20 sec
Program 3 : DCMotor3 / etc.
Program 4 : DCMotor4 / etc.

Program 5 : Stepper1 / on : forward - 400 steps - 2 sec / off : 10 sec / on : forward - 1000 steps - 5 sec / off : 20 sec
Program 6 : Stepper2 / on : forward - 600 steps - 3 sec / off : 30 sec / on : backward - 10000 steps - 20 sec / off : 60 sec / on : forward - 5000 steps - 20 sec
Program 7 : Stepper3 / on : forward - 20 steps - 0,5 sec / on : backward - 20 steps - 0,5 sec / on : forward - 20 steps - 0,5 sec / on : backward - 20 steps - 0,5 sec / on : forward - 20 steps - 0,5 sec / on : backward - 20 steps - 0,5 sec / off : 40 sec
Program 8 : Stepper4 / etc.
Program 9 : Stepper5 / etc.

Here is my code for now :

#include <Wire.h>
#include <Adafruit_MotorShield.h>

Adafruit_MotorShield AFMS = Adafruit_MotorShield();

Adafruit_DCMotor *myMotor = AFMS.getMotor(1); // Entrée
Adafruit_DCMotor *myMotor2 = AFMS.getMotor(2); // Haut Gauche Grande Plante
Adafruit_DCMotor *myMotor3 = AFMS.getMotor(3); // Coté Palmier
Adafruit_DCMotor *myMotor4 = AFMS.getMotor(4); // Milieu devant Grande Plante

const int stepPin = 46; //PUL -Pulse Palmier
const int dirPin = 49; //DIR -Direction
const int enPin = 48; //ENA -Enable
const int stepPin2 = 40; //PUL -Pulse Fougere
const int dirPin2 = 43; //DIR -Direction
const int enPin2 = 42; //ENA -Enable
const int stepPin3 = 28; //PUL -Pulse Grande Plante
const int dirPin3 = 31; //DIR -Direction
const int enPin3 = 30; //ENA -Enable
const int stepPin4 = 34; //PUL -Pulse Grosses Feuilles
const int dirPin4 = 37; //DIR -Direction
const int enPin4 = 36; //ENA -Enable
const int stepPin5 = 22; //PUL -Pulse Feuilles
const int dirPin5 = 25; //DIR -Direction
const int enPin5 = 24; //ENA -Enable

void setup(){
AFMS.begin();

pinMode(stepPin,OUTPUT);
pinMode(dirPin,OUTPUT);
pinMode(enPin,OUTPUT);
pinMode(stepPin2,OUTPUT);
pinMode(dirPin2,OUTPUT);
pinMode(enPin2,OUTPUT);
pinMode(stepPin3,OUTPUT);
pinMode(dirPin3,OUTPUT);
pinMode(enPin3,OUTPUT);
pinMode(stepPin4,OUTPUT);
pinMode(dirPin4,OUTPUT);
pinMode(enPin4,OUTPUT);
pinMode(stepPin5,OUTPUT);
pinMode(dirPin5,OUTPUT);
pinMode(enPin5,OUTPUT);
digitalWrite(enPin,LOW);
digitalWrite(enPin2,LOW);
digitalWrite(enPin3,LOW);
digitalWrite(enPin4,LOW);
digitalWrite(enPin5,LOW);
}

void loop(){
uint8_t i;

//Palmier Sens interieur 1/2
digitalWrite(dirPin,LOW);
for(int x = 0; x < 400; x++){
digitalWrite(stepPin,HIGH);
delayMicroseconds(200);
digitalWrite(stepPin,LOW);
delayMicroseconds(500);
}

myMotor->run(FORWARD);
for (i=0; i<255; i++) {
myMotor->setSpeed(i);
delay(10);
}
for (i=255; i!=0; i--) {
myMotor->setSpeed(i);
delay(10);
}

delay(6000);

// Grande Plante
digitalWrite(dirPin3,LOW);
for(int x = 0; x < 200; x++){
digitalWrite(stepPin3,HIGH);
delayMicroseconds(500);
digitalWrite(stepPin3,LOW);
delayMicroseconds(500);
}

delay(10000);

// Grosses Feuilles aller-retour
digitalWrite(dirPin4,LOW);
for(int x = 0; x < 200; x++){
digitalWrite(stepPin4,HIGH);
delayMicroseconds(500);
digitalWrite(stepPin4,LOW);
delayMicroseconds(500);
}
digitalWrite(dirPin4,HIGH);
for(int x = 0; x < 200; x++){
digitalWrite(stepPin4,HIGH);
delayMicroseconds(500);
digitalWrite(stepPin4,LOW);
delayMicroseconds(500);
}

myMotor2->run(FORWARD);
for (i=0; i<255; i++) {
myMotor2->setSpeed(i);
delay(10);
}
for (i=255; i!=0; i--) {
myMotor2->setSpeed(i);
delay(10);
}

myMotor3->run(FORWARD);
for (i=0; i<255; i++) {
myMotor3->setSpeed(i);
delay(10);
}
for (i=255; i!=0; i--) {
myMotor3->setSpeed(i);
delay(10);
}

delay(3000);

//Palmier sens extérieur
digitalWrite(dirPin,HIGH);
for(int x = 0; x < 800; x++){
digitalWrite(stepPin,HIGH);
delayMicroseconds(400);
digitalWrite(stepPin,LOW);
delayMicroseconds(500);
}

delay(10000);

//Feuilles vibrent
digitalWrite(dirPin5,HIGH);
for(int x = 0; x < 200; x++){
digitalWrite(stepPin5,HIGH);
delayMicroseconds(500);
digitalWrite(stepPin5,LOW);
delayMicroseconds(500);
}
digitalWrite(dirPin5,LOW);
for(int x = 0; x < 200; x++){
digitalWrite(stepPin5,HIGH);
delayMicroseconds(500);
digitalWrite(stepPin5,LOW);
delayMicroseconds(500);
}
digitalWrite(dirPin5,HIGH);
for(int x = 0; x < 200; x++){
digitalWrite(stepPin5,HIGH);
delayMicroseconds(500);
digitalWrite(stepPin5,LOW);
delayMicroseconds(500);
}
digitalWrite(dirPin5,LOW);
for(int x = 0; x < 200; x++){
digitalWrite(stepPin5,HIGH);
delayMicroseconds(500);
digitalWrite(stepPin5,LOW);
delayMicroseconds(500);
}
digitalWrite(dirPin5,HIGH);
for(int x = 0; x < 200; x++){
digitalWrite(stepPin5,HIGH);
delayMicroseconds(500);
digitalWrite(stepPin5,LOW);
delayMicroseconds(500);
}
digitalWrite(dirPin5,LOW);
for(int x = 0; x < 200; x++){
digitalWrite(stepPin5,HIGH);
delayMicroseconds(500);
digitalWrite(stepPin5,LOW);
delayMicroseconds(500);
}
digitalWrite(dirPin5,HIGH);
for(int x = 0; x < 200; x++){
digitalWrite(stepPin5,HIGH);
delayMicroseconds(500);
digitalWrite(stepPin5,LOW);
delayMicroseconds(500);
}
digitalWrite(dirPin5,LOW);
for(int x = 0; x < 200; x++){
digitalWrite(stepPin5,HIGH);
delayMicroseconds(500);
digitalWrite(stepPin5,LOW);
delayMicroseconds(500);
}
digitalWrite(dirPin5,HIGH);
for(int x = 0; x < 200; x++){
digitalWrite(stepPin5,HIGH);
delayMicroseconds(500);
digitalWrite(stepPin5,LOW);
delayMicroseconds(500);
}
digitalWrite(dirPin5,LOW);
for(int x = 0; x < 200; x++){
digitalWrite(stepPin5,HIGH);
delayMicroseconds(500);
digitalWrite(stepPin5,LOW);
delayMicroseconds(500);
}
digitalWrite(dirPin5,HIGH);
for(int x = 0; x < 200; x++){
digitalWrite(stepPin5,HIGH);
delayMicroseconds(500);
digitalWrite(stepPin5,LOW);
delayMicroseconds(500);
}
digitalWrite(dirPin5,LOW);
for(int x = 0; x < 200; x++){
digitalWrite(stepPin5,HIGH);
delayMicroseconds(500);
digitalWrite(stepPin5,LOW);
delayMicroseconds(500);
}

delay(4000);

//Palmier Sens interieur 2/2
digitalWrite(dirPin,LOW);
for(int x = 0; x < 400; x++){
digitalWrite(stepPin,HIGH);
delayMicroseconds(200);
digitalWrite(stepPin,LOW);
delayMicroseconds(500);
}

myMotor4->run(FORWARD);
for (i=0; i<255; i++) {
myMotor4->setSpeed(i);
delay(10);
}
for (i=255; i!=0; i--) {
myMotor4->setSpeed(i);
delay(10);
}

//Fougere
digitalWrite(dirPin2,LOW);
for(int x = 0; x < 3000; x++){
digitalWrite(stepPin2,HIGH);
delayMicroseconds(1000);
digitalWrite(stepPin2,LOW);
delayMicroseconds(500);
}

//Ten seconds delay
delay(10000);
}

I'm still a noob to the world of Arduino. So, I'll refrain from advice on a project as complex as this could be. But I'm interested in what the Gurus have to say.

So, following.

Look here: Useful links - check here for reference posts / tutorials - Programming Questions - Arduino Forum

and read the stuff under "General Design". Most of it is about how to do multiple things at once. Go ahead and study the part about state machines too.

FlorianViel:
What I achieve, for now, is to make a code that activate one motor after the other, with delay. But I think there is a way to write a different program for each motor and make them run simultaneously. I've learned about "blink without delay" and array. As I achieve to make it work with LED, I can't manage to transpose it for motors. Can you help ?

Please post the program that represents your best attempt. And keep it short to start with, with just two motors.

In general don't use delay() and don't use FOR or WHILE loops as they also block the Arduino.

Have a look at how the code is organized in Several Things at a Time

Note how each function runs very briefly and returns to loop() so the next one can be called. None of the functions tries to complete a task in one call. And there may be dozens of calls to a function before it is actually time for it to do anything.

When posting code please use the code button </>
codeButton.png

so your code 
looks like this

and is easy to copy to a text editor See How to use the Forum

Also please use the AutoFormat tool to indent your code for easier reading.

...R