I want to run two for-loops at the same time

Hi. I can't run two or more for loops with opposite directions at the same time. Can anyone help me?
This is my code:

#include "HCPCA9685.h"
 
/* I2C slave address for the device/module. For the HCMODU0097 the default I2C address
 
is 0x40 */
 
#define  I2CAdd_1 0x40
#define  I2CAdd_2 0x41
int pos1=0;
int pos2=180;
int pos3=220; 
int pos6=140;
int pos5=150;
const float pos4=80.0/pos5;

/* Create an instance of the library */
 
HCPCA9685 HCPCA9685_1(I2CAdd_1);
HCPCA9685 HCPCA9685_2(I2CAdd_2);
 
void setup()
{
  /* Initialise the library and set it to 'servo mode' */
 
 // Initialise both modules
  HCPCA9685_1.Init(SERVO_MODE);
  HCPCA9685_2.Init(SERVO_MODE);
 
  // Wake both devices up
  HCPCA9685_1.Sleep(false);
  HCPCA9685_2.Sleep(false);
 
  unsigned int Pos;

for(Pos = 180; Pos< 220; Pos++)
   {
HCPCA9685_1.Servo(0, Pos);
delay(10);
}

for(Pos = 180; Pos> 140; Pos--)
   {
 HCPCA9685_1.Servo(6, Pos);
 delay(10);
 }

}
 
void loop()
{

}

Get rid of the for-loops and the delay in favour of a millis() based approach.

Sources:

And various others.

Tank you but I can't use millis().

For the particular values chosen, the two motions travel the same number of degrees. Will that always be the case? If so,

pos = 180;
for(int i = 0; i <=40; i++) {
HCPCA9685_1.Servo(0, pos + i);
HCPCA9685_1.Servo(6, pos - i);
}

And use delay() if you must.

Why?

  • Who’s holding you back from doing so :scream:

Tank you. And what can I do for these for loops? I want to run all of them at the same time:

#include "HCPCA9685.h"

/* I2C slave address for the device/module. For the HCMODU0097 the default I2C address

  is 0x40 */

#define  I2CAdd_1 0x40
#define  I2CAdd_2 0x41
int pos1 = 0;
int pos2 = 180;
int pos3 = 220;
int pos6 = 140;
int pos5 = 150;
const float pos4 = 80.0 / pos5;

/* Create an instance of the library */

HCPCA9685 HCPCA9685_1(I2CAdd_1);
HCPCA9685 HCPCA9685_2(I2CAdd_2);

void setup()
{
  /* Initialise the library and set it to 'servo mode' */

  // Initialise both modules
  HCPCA9685_1.Init(SERVO_MODE);
  HCPCA9685_2.Init(SERVO_MODE);

  // Wake both devices up
  HCPCA9685_1.Sleep(false);
  HCPCA9685_2.Sleep(false);

  unsigned int Pos;

  for (Pos = 120; Pos > 0; Pos--)
  {
    HCPCA9685_1.Servo(1, Pos);
    delay(10);
  }

  for (Pos = 100; Pos > 10; Pos--)
  {
    HCPCA9685_1.Servo(7, Pos);
    delay(10);
  }

  for (Pos = 180; Pos < 220; Pos++)
  {
    HCPCA9685_1.Servo(6, Pos);
    delay(10);
  }

  for (Pos = 300; Pos > 150; Pos--)
  {
    HCPCA9685_1.Servo(0, Pos);
    delay(10);
  }

}

void loop()
{

}

Because my code is very long and it makes this code much longer and more complicated

In that case there might be something wrong with your design.

No, it will definitely not make it more complicated. Longer? Maybe but you haven't shown us your long code.

Solved? can you share plz how can I solve such issue with mine too

Not yet

No, I'll not entertain your fishing expedition any further. Make an attempt to code your whole job, or thoroughly describe what it is to do, and we'll go from there.

You have a number of things to sort out.

  1. must they both begin and end moving at the same time? I.e. if one moves 5 degrees, and the other moves 30, is it acceptable that one finishes before the other?
  2. how fast is the motion? Can servos move multiple degrees per update, or must the update always be a 1 degree step?
  3. you've told us nothing about the real application. That would help us begin to guide you. I assure you, not using millis() is a recipe for problems; I answered your initial question with a delay-based solution because it was sufficient for the use case I proposed.
    Ball's in your court. I may be back in a few hours to check in.

It should radically simplify your code.
This is a fantastic set of tutorials to help you learn how to multitask properly. Once you make the adjustment your code becomes easier to read, debug, and maintain.

I recommend you give it a shot. Plenty of people here will help, provided you put in the effort first.

It is important to each for servos begin to move at one specific time and ending time isn't important.

Are you using two usernames? Same code with 180, 220, Pos (capital P)...

No.

I edited the code as bellow. Is it true?

int pos1 = 0;
int pos2 = 180;
int pos3 = 260;
int pos6 = 100;
int pos7=100;

for(int i = 0; i <=200; i++) {
HCPCA9685_1.Servo(0, pos3);
HCPCA9685_1.Servo(6, pos6);
HCPCA9685_1.Servo(1, pos1);
HCPCA9685_1.Servo(7, pos1);
delay(10);
}

Did you look at post #4?