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.
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...
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:
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.
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?