how to input delay in actuation - make it sequential?

Hello,

I am hoping this is not redundant; I am trying to actuate a few linear actuators IN A SEQUENTIAL manner.
I am using motor shield v2 and arduino mega 2560 with firgelli L12 -P.
I am using the following:

#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_PWMServoDriver.h"

// Create the motor shield object with the default I2C address
Adafruit_MotorShield AFMS = Adafruit_MotorShield(); 
// Or, create it with a different I2C address (say for stacking)
// Adafruit_MotorShield AFMS = Adafruit_MotorShield(0x61); 

// Select which 'port' M1, M2, M3 or M4. In this case, M1

Adafruit_DCMotor *myMotor = AFMS.getMotor(1);
Adafruit_DCMotor *myOtherMotor = AFMS.getMotor(2);
Adafruit_DCMotor *yourMotor = AFMS.getMotor(3);

// You can also make another motor on port M2
//Adafruit_DCMotor *myOtherMotor = AFMS.getMotor(2);

void setup() {
  Serial.begin(9600);           // set up Serial library at 9600 bps
  Serial.println("Adafruit Motorshield v2 - DC Motor test!");

  AFMS.begin();  // create with the default frequency 1.6KHz
  //AFMS.begin(1000);  // OR with a different frequency, say 1KHz
  
  // Set the speed to start, from 0 (off) to 255 (max speed)

  myMotor->setSpeed(100);
  myOtherMotor->setSpeed(100);
  yourMotor->setSpeed(100);
  myMotor->run(FORWARD);
  myOtherMotor->run(FORWARD);
  yourMotor->run(FORWARD);
  // turn on motor
  myMotor->run(RELEASE);
  myOtherMotor->run(RELEASE);
  yourMotor->run(RELEASE);
}

void loop() {
  uint8_t i;
  
  Serial.print("tick");

  myMotor->run(FORWARD);
 // delay();
  myOtherMotor->run(FORWARD);
 // delay();
  yourMotor->run(FORWARD);
  
  for (i=0; i<255; i++) {
    myMotor->setSpeed(i); 
    myOtherMotor->setSpeed(i);
    yourMotor->setSpeed(i);
    delay(750);
  }
  
  for (i=255; i!=0; i--) {
    myMotor->setSpeed(i);
    myOtherMotor->setSpeed(i);  
    yourMotor->setSpeed(i);
    delay(750);
  }
  
  Serial.print("tock");

  myMotor->run(BACKWARD);
  myOtherMotor->run(BACKWARD);
  yourMotor->run(BACKWARD);
  
  for (i=0; i<255; i++) {
    myMotor->setSpeed(i);
    myOtherMotor->setSpeed(i);
    yourMotor->setSpeed(i);
    delay(750);
  }
  
  for (i=255; i!=0; i--) {
    myMotor->setSpeed(i);  
    myOtherMotor->setSpeed(i);
    yourMotor->setSpeed(i);
    delay(750);
  }

  Serial.print("tech");
 
  myMotor->run(RELEASE);
  myOtherMotor->run(RELEASE);
  yourMotor->run(RELEASE);
 
  delay(100);
}

I am not sure where to put the delays (or not sure if putting delay is conventional when trying to make sequence of motions!?
any help would be appreciated very much! This does not seem to be so difficult but I am not getting the kind of motion I want.,

Regards,
Karim.

I have no idea what that code is supposed to do.
But the use of delay is not recommended, because it is blocking.
Unnecessary use of a for... loop is also blocking.
You are amplifying a blocking code by essentially doing absolutely nothing for .75 seconds, 255 times.
So that's 191.25 seconds of doing nothing in total.
And then do very similar things multiple times.
But that might be by design, i don't know what commands like setSpeed and run (something) actually do (i can think of something, and in that case this code does absolutely not what anyone would want).

To make things more clear, you should tell us what you EXPECTED to see and what you ACTUALLY DO see.
It doesn't help much if you tell us this code doesn't do what i expected, without telling us what you did expect.

Dear MAS3

I am sorry for the confusion. But here it is as per your suggestion:

I would like to actuate three Firgelli L12 -P series actuators in a sequential manner for a limited length extension (in other words I do not want to fully extend). I am ultimately hoping for a sequence of:

1st one ... 15 ms ... 2nd one
1st one ... 15 ms ... 3rd one
...

but for now any type of sequencing would be desirable.

Many thanks,
Karim

Ok, that is what you want.
And what does the code you got up to now actually do ?

I'll try and explain what might be wrong (later on today) if you can answer this.

Yes sir, Thank you.
OK so first I would like to put the new code (with some revisions) down here:

/* 
This is a test sketch for the Adafruit assembled Motor Shield for Arduino v2
It won't work with v1.x motor shields! Only for the v2's with built in PWM
control

For use with the Adafruit Motor Shield v2 
---->	http://www.adafruit.com/products/1438
*/

#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_PWMServoDriver.h"

// Create the motor shield object with the default I2C address
Adafruit_MotorShield AFMS = Adafruit_MotorShield(); 
// Or, create it with a different I2C address (say for stacking)
// Adafruit_MotorShield AFMS = Adafruit_MotorShield(0x61); 

// Select which 'port' M1, M2, M3 or M4. In this case, M1
Adafruit_DCMotor *my1stMotor = AFMS.getMotor(1);
// You can also make another motor on port M2
Adafruit_DCMotor *my2ndMotor = AFMS.getMotor(2);
Adafruit_DCMotor *my3rdMotor = AFMS.getMotor(3);

void setup() {
  Serial.begin(9600);           // set up Serial library at 9600 bps
  Serial.println("Adafruit Motorshield v2 - DC Motor test!");

 // AFMS.begin();  // create with the default frequency 1.6KHz
 AFMS.begin(1000);  // OR with a different frequency, say 1KHz
  
  // Set the speed to start, from 0 (off) to 255 (max speed)
  my1stMotor->setSpeed(150);
  my1stMotor->run(FORWARD);
  my1stMotor->run(RELEASE);
  
  delay(1000);
  
  my2ndMotor->setSpeed(150);
  my2ndMotor->run(FORWARD);
  my2ndMotor->run(RELEASE);

  delay(1000);
  
  my3rdMotor->setSpeed(150);
  my3rdMotor->run(FORWARD);
  my3rdMotor->run(RELEASE);
  
}

void loop() {
  uint8_t i;
  
  Serial.print("tick");

  my1stMotor->run(FORWARD);
  my2ndMotor->run(FORWARD);
  my3rdMotor->run(FORWARD);
  
  for (i=0; i<255; i++) {
    
    my1stMotor->setSpeed(i);  
    my2ndMotor->setSpeed(i);  
    my3rdMotor->setSpeed(i);  

    delay(10);
  }
  for (i=255; i!=0; i--) {
    
    my1stMotor->setSpeed(i);
    my2ndMotor->setSpeed(i);  
    my3rdMotor->setSpeed(i);  
  
    delay(10);
  }
  
  Serial.print("tock");

  my1stMotor->run(BACKWARD);
  my2ndMotor->run(BACKWARD);
  my3rdMotor->run(BACKWARD);
  
  for (i=0; i<255; i++) {
    
    my1stMotor->setSpeed(i);
    my2ndMotor->setSpeed(i);  
    my3rdMotor->setSpeed(i);  
    
    delay(10);
  }
  for (i=255; i!=0; i--) {
      
    my1stMotor->setSpeed(i);
    my2ndMotor->setSpeed(i);  
    my3rdMotor->setSpeed(i);  
    
    delay(10);
  }

  Serial.print("tech");
  
  my1stMotor->run(RELEASE);
  my2ndMotor->run(RELEASE);
  my3rdMotor->run(RELEASE);
  
  delay(100);
}

This code is supposed to run each actuator by 1 s delay.
It first turns the actuators on and then runs with increasing speed and then decreasing it with every increase in 10 ms delays.
Then it releases then (turns them off).
But now it does not even do he sequencing, but just moves them all at once!!

However besides that I do realize that putting delay is probably the worst approach in sequencing, but I am not sure about what the alternative is...

Regards,
Karim.

Hi.

This code is quite different from your first post.
And now i see why; this is the example that came with the shield.
You modified it to control 2 more motors (the example gives an option for a 2nd motor, but doesn't use it) plus some other stuff.
Did you run the test as is, controlling just 1 motor ?

The sketch offers very limited hints via serial monitor.
Open serial monitor in the IDE, and watch that while your sketch runs.
You should see 4 different texts come by during the first iteration.
After that the last 3 should keep repeating and you should see the motors move during that.

So what do you see in serial monitor while the motor runs ?

Reading your sketch, i see you are trying to set a speed, have the motor run, and then stop it.
You do that for 3 different motors, with an interval of 1 second.
And you do that before the loop starts.
That is what you intend to do here:

  my1stMotor->setSpeed(150);
  my1stMotor->run(FORWARD);
  my1stMotor->run(RELEASE);
  
  delay(1000);
  
  my2ndMotor->setSpeed(150);
  my2ndMotor->run(FORWARD);
  my2ndMotor->run(RELEASE);

  delay(1000);
  
  my3rdMotor->setSpeed(150);
  my3rdMotor->run(FORWARD);
  my3rdMotor->run(RELEASE);

This part of the sketch, first sets the speed (actually the current) to about 60 %.
Then it starts the motor.
After that, within about 1 nano second (a few clock cycles) it stops the motor.
How far do you think that motor will run or even achieve any speed ?

So perhaps waiting a bit between switching on and off your motor might help here.

Then the loop.
You are starting your motors all at once, without setting any speed at all.
Shouldn't you first define a speed and then start your motors ?
After they have started, you can alter their speeds (the example seems to do so, so you must be able to do so too).
This sketch ramps up the speed and after it has reached its maximum it will ramp down.
Then the motors are stopped, to start over again the next iteration.
Again the way it is now, they should do so all at the same time and only during setup you might see the motors move independently.

But don't forget that you already have moved those motors during setup (after fixing the sketch).
Be aware of the position of the motors before you get into trouble with that.

I think this might be of some help.
If i wasn't clear enough, ask about what you didn't understand.

Ok, so...
The tick - tock - tech runs obviously and beforehand the setup.
I tried to print a few more lines into the serial monitor, but first of all it closes ASA the new "upload" is being performed.
Now overall this is the code that I have slightly modified:

#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_PWMServoDriver.h"

Adafruit_MotorShield AFMS = Adafruit_MotorShield(); 

Adafruit_DCMotor *my1stMotor = AFMS.getMotor(1);
Adafruit_DCMotor *my2ndMotor = AFMS.getMotor(2);
Adafruit_DCMotor *my3rdMotor = AFMS.getMotor(3);

void setup() {
  Serial.begin(9600);           // set up Serial library at 9600 bps
  Serial.println("Adafruit Motorshield v2 - DC Motor test!");

  AFMS.begin(1000);  // OR with a different frequency, say 1KHz
  
  my1stMotor->setSpeed(150);
  Serial.print("Speed Set 1st Motor\n");
  my1stMotor->run(FORWARD);
  Serial.print("Turn On 1st Motor\n");
  delay(30);
  my1stMotor->run(RELEASE);
  Serial.print("Turn Off 1st Motor\n");
  my2ndMotor->setSpeed(150);
  Serial.print("Speed Set 2nd Motor\n");
  my2ndMotor->run(FORWARD);
  Serial.print("Turn On 2nd Motor\n");
  delay(30);
  my2ndMotor->run(RELEASE);
  Serial.print("Turn Off 2nd Motor\n");
  my3rdMotor->setSpeed(150);
  Serial.print("Speed Set 3rd Motor\n");
  my3rdMotor->run(FORWARD);
  Serial.print("Turn On 3rd Motor\n");
  delay(30);
  my3rdMotor->run(RELEASE);
  Serial.print("Turn Off 3rd Motor\n");
  }

void loop() 
{
  uint8_t i;
  Serial.print("tick");
  my1stMotor->setSpeed(150);
  my1stMotor->run(FORWARD);
   
  for (i=0; i<255; i++) 
  {
    my1stMotor->setSpeed(i);  
  }

  for (i=255; i!=0; i--) 
  {
    my1stMotor->setSpeed(i);
  }
  
  Serial.print("tock");
  my1stMotor->run(BACKWARD);

  for (i=0; i<255; i++) 
  {
    my1stMotor->setSpeed(i);
  }
  
  for (i=255; i!=0; i--) 
  {
    my1stMotor->setSpeed(i);
  }

  Serial.print("tech");
  delay(30);  
  my1stMotor->run(RELEASE);
  delay(1000);
  Serial.print("tick");
  my2ndMotor->setSpeed(150);
  my2ndMotor->run(FORWARD);

  for (i=0; i<255; i++) 
  {
    my2ndMotor->setSpeed(i);
  }

  for (i=255; i!=0; i--) 
  {
    my2ndMotor->setSpeed(i); 
  }
  Serial.print("tock");

  my2ndMotor->run(BACKWARD);
  
  for (i=0; i<255; i++) 
  {
    my2ndMotor->setSpeed(i); 
  }
  
  for (i=255; i!=0; i--) 
  {
    my2ndMotor->setSpeed(i);  
  }

  Serial.print("tech");
  delay(30);
  my2ndMotor->run(RELEASE);
  
  delay(1000);
  
  Serial.print("tick");
   my2ndMotor->setSpeed(150);
   my3rdMotor->run(FORWARD);
  
  for (i=0; i<255; i++) 
  {
    my3rdMotor->setSpeed(i);  
  }
  
  for (i=255; i!=0; i--) 
  {
    my3rdMotor->setSpeed(i);  
  }
  
  Serial.print("tock");

  my3rdMotor->run(BACKWARD);
  
  for (i=0; i<255; i++) 
  {
    my3rdMotor->setSpeed(i);  
  }
  
  for (i=255; i!=0; i--) 
  {
    my3rdMotor->setSpeed(i);  
  }

  Serial.print("tech");
  delay(30);
  my3rdMotor->run(RELEASE);
  
  delay(1000);
  
}

I feel like the hints you mentioned although very much appreciated but give, at least to me, very few insights towards what steps I specifically need to take. I would be grateful if more detailed description could be provided by you.
Regards,
Behzad.

Hi Behzad.

I understand that you'd like me to tell you "do this and do that", after which your sketch would run.
But if i do the thinking for you, what would you be laerning ?
Would you understand what's going on ?

That's why i can try to tell you what is going on in your sketch, and give you hints about a solution.
But i can't tell you what to do and be sure you understand what's going on.

I have no idea about that AFMS of yours, and do not plan studying it in depth.

You did actually improve your code for testing/debugging.
I doubt your motors will actually run, but they will make some noise.
Did you know how long a delay of 30 lasts ?
It is 30 milliseconds, or 0.03 seconds.
Your motor will not be able to start a movement in such short time period.
So try to use a delay of 1000, one second.
It should move, maybe just a little bit.

In the part where you are counting up and down to / from 255, you could use a short delay of a few (10, 20, 30 or so) milliseconds to have a better view at the motor speeding up and down.
You'd put that delay after the setspeed command, but within the { curly braces }.