For the independant operation of both servos the whole code must follow the non-blocking paradigma.
Non-blocking means:
the most outer loop which is
void loop() {
//top of loop
// any kind of code (can be THOUSANDs of lines of code)
// bottom of loop
}
must run down completely at high speed from
//top of loop
down to
// bottom of loop
to make sure that this is running down fast
all lines of code inbetween must be finished fast
This can be achieved by a coding-structure that makes sure to
quickly jump into a sub-function and quickly jump out again
void loop() {
myFunctionOne(); // quickly jump in / quickly jump out
myFunctionTwo(); // quickly jump in / quickly jump out
//....
myFunction10(); // quickly jump in quickly jump out
}
Not all code shall be executed at all times.
This means code-execution is made conditional
void myFunctionOne() {
if (ExecutionConditionONE == true) {
// execute code
}
else {
//doNothing
}
}
void myFunctionTwo() {
if (ExecutionConditionTwo == true) {
// execute code
}
else {
//doNothing
}
}
for-loops and while-loops must be avoided completely
counting variables up/down is done bye each new call of the sub-functions
void myFunctionOne() {
if (ExecutionConditionONE == true) {
posA = posA + 1;
}
else {
//doNothing
}
}
remember: loop() is looping at high speed all the time
This means the sub-function is called over an over again through void loop()
instead of a for-loop
Now changing the servo-position slowly requires non-blocking timing
which is explained here