Loop running when for condition not met

I have a sketch (currently testing with Tinkercad) that is meant to sweep 4 servos simultaneously to 90. A 5th servo should then sweep to 90 and back to 0 and repeat this loop. The code initially works but instead of the first 4 servos staying at 90, they reset to 0 and sweep to 90. Since the position variable at the end of the loop is 90, wouldn't that make the for statement false and thus proceed to the sweep loop for the 5th servo? Thanks for your help.

#include <Servo.h> 

Servo myServo1;
Servo myServo2;
Servo myServo3;
Servo myServo4; 
Servo myServo5; 

int pos = 0;
int pos1 = 0;
void setup() {
  myServo1.attach(2);
  myServo2.attach(4);
  myServo3.attach(6);
  myServo4.attach(8);
  myServo5.attach(10);

  
}

void loop() {
  
 
 for (pos = 0; pos <= 90; pos += 1) {
    myServo1.write(pos);
    myServo2.write(pos);
    myServo3.write(pos);
    myServo4.write(pos);
    delay(40);
   
}
  for (pos1 = 0; pos1 <= 90; pos1 += 1) {
    myServo5.write(pos1);
    delay(15);
  }
  for (pos1 = 90; pos1 >= 0; pos1 -= 1) {
    myServo5.write(pos1);
    delay(15);
  }
 
}

I don't have any servos handy, but I amended your code as below with some serial prints, and the output is as expected.... the value of pos goes to 90, then pos1 goes to 90, and pos1 goes to 0. Then pos starts again...

#include <Servo.h>

Servo myServo1;
Servo myServo2;
Servo myServo3;
Servo myServo4;
Servo myServo5;

int pos = 0;
int pos1 = 0;
void setup() {
  Serial.begin(9600);
  Serial.print("setup ");
  myServo1.attach(2);
  myServo2.attach(4);
  myServo3.attach(6);
  myServo4.attach(8);
  myServo5.attach(10);
  Serial.println("done");


}

void loop() {


  for (pos = 0; pos <= 90; pos += 1) {
    Serial.print("pos "); Serial.println(pos);
    myServo1.write(pos);
    myServo2.write(pos);
    myServo3.write(pos);
    myServo4.write(pos);
    delay(40);

  }
  for (pos1 = 0; pos1 <= 90; pos1 += 1) {
    Serial.print("pos1 going up "); Serial.println(pos1);
    myServo5.write(pos1);
    delay(15);
  }
  for (pos1 = 90; pos1 >= 0; pos1 -= 1) {
    Serial.print("pos1 going down "); Serial.println(pos1);
    myServo5.write(pos1);
    delay(15);
  }

}

are you suggesting the value of "pos" shouldn't change after the first iteration of loop()

in each iteration of loop, servos 1-4 are swept from 0 to 90. when loop() repeats, the for loop immediately moves them to 0 and then steps them to 90.

"pos" in the for loop cycled from 0-90 each time the for loop is executed in loop()

When loop() starts again and you enter the 'for' loop the second time, it re-runs the "pos = 0" part and starts counting up from 0. If you want it to remember that the 'for' loop was done, remove the initialization part:

 for ( ; pos <= 90; pos += 1) {
    myServo1.write(pos);
    myServo2.write(pos);

The global variable 'pos' will retain the value 91 after the first time through the loop.

Ah! I have been misunderstanding the for statement. I didn't realise the first part set value, I thought it only checked the value.

I tried the change and it does work however now servo5 starts sweeping at the same time as serovos1-4 instead of starting to sweep after.

Thks

The for (pos = 0..... ) part does that, when the for starts at the beginning of loop() each time.

The very first time, pos=0 sets pos to 0, then it stays in the for until pos is 90, then goes to the two fors that take care of servo5. It's still in the first pass through loop().

Then loop() runs again, and the first time it sees for, pos goes back to 0 straightaway when it sees the pos=0. Every time any for loop is encountered it initialises counter to what that first parameter says, pos=0 in your case. It's the way a for is written under the hood: on entry, initialise the counter.

It's really bad form to delete a post like that... I was in the middle of replying. Now anyone reading my previous will wonder where the quote came from....

Sorry. I realised I had answered my question shortly after posting. It was only a few minutes so wasn't expecting a response so soon. Thanks for the answer though.

Can you explain why servo 5 starts sweeping at the same time as servos 1-4? I'm stumped on that one.

It shouldn't unless you changed some other part of your sketch. Please show the latest version.

I think it may be glitch in Tinker Cad circuits. Servo 5 swings quickly (much faster than the code dictates) to 90. It stays at 90 then starts sweeping back and forth after servos 1-4 have moved to 90. I inserted a print line Pos1 stays at 0 while Pos moves to 90 so can only assume a glicth?

Thanks for the input.

#include <Servo.h> 

Servo myServo1;
Servo myServo2;
Servo myServo3;
Servo myServo4; 
Servo myServo5; 

int pos = 0;
int pos1 = 0;

void setup() {
  myServo1.attach(2);
  myServo2.attach(4);
  myServo3.attach(6);
  myServo4.attach(8);
  myServo5.attach(10);

 Serial.begin(9600);
}

void loop() {
  
 
 for (; pos <= 90; pos += 1) {
    myServo1.write(pos);
    myServo2.write(pos);
    myServo3.write(pos);
    myServo4.write(pos);
    delay(40);
    Serial.println(pos);
   Serial.println(pos1);
}
  for (pos1 = 0; pos1 <= 90; pos1 += 1) {
    myServo5.write(pos1);
    delay(15);
  }
  for (pos1 = 90; pos1 >= 0; pos1 -= 1) {
    myServo5.write(pos1);
    delay(15);
  }
 
}

The code looks correct. I see no way the code could execute the second loop before finishing the first loop.

Put more prints in like I did in #2 and monitor both pos and pos1....

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.