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()
{
}
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.
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?
how fast is the motion? Can servos move multiple degrees per update, or must the update always be a 1 degree step?
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.