Start Sync Motors

hi,

i'm trying to write code for (now) 2 motors, but will be expanding it to 6. i'm only using 1 direction hence InBx and 1 PWM.

int InB1 = 1;
int PWM1 = 3;  

int InB2 = 2;
int PWM2 = 5;


void setup() {
  Serial.begin(9600);
  pinMode(InB1, OUTPUT);
  pinMode(PWM1, OUTPUT);

  pinMode(InB2, OUTPUT);
  pinMode(PWM2, OUTPUT);

}

void loop() {

  for (int PWM1_val, PWM2_val = 0; PWM1_val, PWM2_val < 120; PWM1_val++, PWM2_val++){  // increases from 0 to max speed
      digitalWrite(InB1, HIGH);
      digitalWrite(InB2, HIGH);
      analogWrite(PWM1, PWM1_val);
      analogWrite(PWM2, PWM2_val);
      delay (100);    // increase this number for a slower ramp
    }

  for (int PWM1_val, PWM2_val = 120; PWM1_val, PWM2_val > 60; PWM1_val--, PWM2_val--){ 
    digitalWrite(InB1, HIGH);
    digitalWrite(InB2, HIGH);
    analogWrite(PWM1, PWM1_val);
    analogWrite(PWM2, PWM2_val);
    delay (120);    // increase this number for a slower ramp
  }
  
    for (int PWM1_val, PWM2_val = 60; PWM1_val, PWM2_val < 100; PWM1_val++, PWM2_val++){ 
    digitalWrite(InB1, HIGH);
    digitalWrite(InB2, HIGH);
    analogWrite(PWM1, PWM1_val);
    analogWrite(PWM2, PWM2_val);
    delay (10);    // increase this number for a slower ramp
  }

  for (int PWM1_val, PWM2_val = 100; PWM1_val, PWM2_val > 50; PWM1_val--, PWM2_val--){ 
    digitalWrite(InB1, HIGH);
    digitalWrite(InB2, HIGH);
    analogWrite(PWM1, PWM1_val);
    analogWrite(PWM2, PWM2_val);
    delay (200);    // increase this number for a slower ramp
  }

}
  1. This is what i have written so far. First how do I write the code for starting in sync for all the motors (now only 2 but later total of 6)?

  2. Is there any better way of writing the above code?

  3. I would need to at some point let the motor run at a certain speed. Do i write it this way? and then to drop down to say 80 (PWM) and stay at 80 for 5 sec?

for (int PWM1_val = 120;){
digitalWrite(InB1, HIGH);
analogWrite(PWM1, PWM1_val);
delay(2000);

for (int PWM1_val = 120; PWM1_val > 80; PWM1_val--, PWM2_val--){
digitalWrite(InB1, HIGH);
analogWrite(PWM1, PWM1_val);
delay(50);

for (int PWM1_val = 80;){
digitalWrite(InB1, HIGH);
analogWrite(PWM1, PWM1_val);
delay(5000);

is this right?

  1. How do i write the code for say Motor1, PWM from 60 to 100 in 50ms and staying at 100 for 2 secs and Motor2, PWM from 50 to 80 in 200ms and staying at 80 for 10 secs? Both motors starting at the same time?

thanks!

p.s. i can't check if the codes work fine, cause there no internet at where i'm working. So i'm writing and testing later.

for (int PWM1_val, PWM2_val = 0; PWM1_val, PWM2_val < 120; PWM1_val++, PWM2_val++)

This declares two variables, PWM1_val and PWM2_val. It only initializes one of them.

The conditional statement is "PWM1_val, PWM2_val < 120". This is not the same as "PWM1_val < 120 && PWM2_val < 120".

If PWM1_val and PWM2_val always have the same values, there is no reason to have two variables.

There is no reason to set InB1 and InB2 HIGH on every pass through loop. Once set, the pin values stays that way until explicitly changed.

InXX as the name for a variable used for OUTPUT is not very smart.

thanks for the fast reply, but i don't really understand how i should write 2 variable with the same value. Can you show me an example code?

and if i want to start both motors at the same time, but different values, do i write it this way?

for (int PWM1_val = 0 && PWM2_val = 60; PWM1_val < 120 && PWM2_val < 140; PWM1_val++, PWM2_val++)
digitalWrite(InB1, InB2, HIGH);
analogWrite(InB1, InB2, PWM1_val, PWM2_val);
delay(100);
digitalWrite(InB1, HIGH);
digitalWrite(InB2, HIGH);
for(int val1 = 0, val2 = 60; val1 < 120 && val2 < 140; val1++, val2++)
{
   analogWrite(PWM1, val1);
   analogWrite(PWM2, val2);
   delay(100);
}

Notice that this will only loop 80 times (while val2 is less than 140), terminating when val1 is only 80.

You can't make one loop that iterates different numbers of times. If you want to stop writing a larger value to the PWM2 pin when the 2nd value exceeds some limit, that need to involve an if test inside the for loop.

for(int val1 = 0, val2 = 60; val1 < 120; val1++, val2++)
{
   analogWrite(PWM1, val1);
   if(val2 < 140)
      analogWrite(PWM2, val2);
   delay(100);
}

i roughly get what you mean. maybe i try another way of asking.

bascially i'm going use 6 motors. These motors will start at the same time, but they have different PWM values.

e.g.
Motor 1-6 start all at PWM_val = 0.
Motor 1 will PWM from 0 to 120 in 50ms
Motor 2 will PWM from 0 to 80 in 100ms
....
Motor1 will stay at 120 for 4 secs
Motor2 will PWM from 80 to 60 in 10ms and stay for 5 secs.
....

Motor1 will PWM from 120 to 160 in 5ms
Motor2 will PWM from 60 to 255 in 1ms

as you see there are different values and different timings.

could you please give me an example of the code for at least 2 lines.

thanks for your patience. i'm really new to writing codes in arduino...

You will need to get rid of the delay calls to make this happen. Instead you have to keep track of 6 pwm values and 6 times - one for each motor.

On each pass through loop, you use millis() to see what time it is now.

Then, for each motor, you see if it is time to change the pwm value for that motor. If it is, you change the value, write the value to the motor, and record the time that the pwm value for that motor was last changed. You have intervals for ramping up, for steady state, and for ramping down. It is easy to check which phase you are in for each motor, and set the pwm value accordingly.