user83030: I'm controlling 2 servo motors but would like to adjust their variables separately, specifically  their upper and lower values, and their speeds. This code is one that I had found online then separated, since the original code didn't have adjustable variables for both. I'm using an adafruit servo
controller
for (uint16_t microsecL = mainServoClosedL; microsecL < mainServoOpenL; microsecL +=5) 
for (uint16_t microsecR = mainServoClosedR; microsecR < mainServoOpenR; microsecR -=5) {
        pwm.writeMicroseconds(mainServoL, microsecL);
        pwm.writeMicroseconds(mainServoR, microsecR);
      }


user313: The micro-controller can't do any more than one thing at a time. What it can do is do that one thing way faster than your servos need it to. So it has (more than) enough time to evaluate each motor's condition and needs and to do it fast enough that you won't notice and they won't be affected by the difference.
The one thing your code mustn't do is to wait for a single something to happen, unless that is absolutely the only thing of interest that could possibly happen next. That is seldom the case in the real world, so the controller must check for all possible events of interest, over and over again, to be able to respond to whichever one does happen next.
In your code snippet, you increase microsecL and decrease microsecR, but not at any particular rate(s). Your loop code probably needs to look at the execution-time clock (the functions `millis()' and 'micros()' will read that for you), and when a suitable interval has passed, adjust one or both of those values and do anything else that might need doing, such as stopping a motor when a time has elapsed or it has reached a final position.

user4143: The answer is simple, but the solution isn't.
The simple part: don't use for loops.
The harder part: you have one loop() function which runs repeatedly. For each iteration of that function you make decisions about what to do during that loop and act on them. Decisions like "Is it time to increase this PWM value?" or "Has this value reached the limit I want for it?" etc.

83030: <p>I'm controlling 2 servo motors but would like to adjust their variables separately, specifically  their upper and lower values, and their speeds. This code is one that I had found online then separated, since the original code didn't have adjustable variables for both. I'm using an adafruit servo
controller</p>
<pre><code>for (uint16_t microsecL = mainServoClosedL; microsecL &lt; mainServoOpenL; microsecL +=5) 
for (uint16_t microsecR = mainServoClosedR; microsecR &lt; mainServoOpenR; microsecR -=5) {
        pwm.writeMicroseconds(mainServoL, microsecL);
        pwm.writeMicroseconds(mainServoR, microsecR);
      }
</code></pre>
 313: <p>The micro-controller can't do any more than one thing at a time. What it <em>can</em> do is do that one thing <em>way</em> faster than your servos need it to. So it has (more than) enough time to evaluate each motor's condition and needs and to do it fast enough that you won't notice and they won't be affected by the difference.</p>
<p>The one thing your code <em>mustn't</em> do is to wait for a single something to happen, unless that is absolutely the only thing of interest that could possibly happen next. That is seldom the case in the real world, so the controller must check for all possible events of interest, over and over again, to be able to respond to whichever one <em>does</em> happen next.</p>
<p>In your code snippet, you increase microsecL and decrease microsecR, but not at any particular rate(s). Your loop code probably needs to look at the execution-time clock (the functions `millis()' and 'micros()' will read that for you), and when a suitable interval has passed, adjust one or both of those values and do anything else that might need doing, such as stopping a motor when a time has elapsed or it has reached a final position.</p>
 4143: <p>The answer is simple, but the solution isn't.</p>
<p>The simple part: don't use for loops.</p>
<p>The harder part: you have one <code>loop()</code> function which runs repeatedly. For each iteration of that function you make decisions about what to do during that loop and act on them. Decisions like &quot;Is it time to increase this PWM value?&quot; or &quot;Has this value reached the limit I want for it?&quot; etc.</p>
