Driving 8 Servos with For Loop Reed Switch

Hello, I have created a for loop function:

  1. IN
    for(pos = 92; pos < 110; pos += 1)
    {
    myservoI.write(pos);
    delay(10);
    }
    for(pos = 110; pos >= 92; pos -= 1)
    {myservoI.write(pos);
    delay(10);
    }
    delay(360);

  2. OUT
    for(pos = 92; pos >= 74; pos -= 1)
    {
    myservoI.write(pos);
    delay(10);
    }
    for(pos = 74; pos < 92; pos += 1)
    {myservoI.write(pos);
    delay(10);
    }
    delay(360);

The desired action involves running the 8 servos under this “in-and-out” actuation function, however dividing the servos into pairs of 2 for 4 initialization timing phases. When a permanent magnet passes a reed switch, I need the 1st pair of servos to run the 1 function, then 180 milliseconds later run the 2nd pair, then 180 ms later run the 3rd pair, then another 180 ms run the 4th pair. After, for instance, 4 repetitions of the 1 “IN” function, then there needs to be 4 repetitions of the “OUT” function. I definitely appreciate some help with this, because I am in out of my head. For starters, I need code where a reed switch makes a single servo “twitch” once under either of the above functions at each revolution like a bicycle speedometer.
Thanks,
Devin

It sounds as if you want to have several servo movements controlled independently with the whole lot initiated by an input state change. The best way to do that is to restructure your sketch to be non-blocking. The blink without delay example sketch shows how to carry out a timed action without blocking. In that example, the action is to blink an LED. In your case the action would be to move a servo a small amount. For each servo you would need a variable recording the current position, and a variable recording the time it was moved to that position.

If the different servo movements are directly related to each other it may be that you don't need to define them completely independently and may be able to reduce the amount of data you need, but for a simple first version I would assume they are completely independent.

What PeterH said and also look up 'Finite State Machine'.

May anyone give me a lead on how to structure this sketch?

I am not really sure what you want to do so this is my best guess at a "structure". It may also give you a basis from which to describe your requirement more clearly.

Create a global variable called (say) servoXon and set it to 0 in setup().

Create a function for each servo that makes the servo go through its motions if the servoXon variable contains the number of the servo. (It would be neater to do this using an array of servo objects and a single function - but this simple approach will be clear)

When the reed switch is triggered it will set servo1on = 1 to start the process.

If the function for servo1 sees that servoXon == 1 it will go through its routine and at the end of it will set servoXon = 2 to trigger the next servo

The code for the servo2 function is identical except that it checks whether servoXon == 2 and when finished sets servoXon = 3 etc

The last servo function will set servoXon = 0 to stop the process waiting for the next reed switch.

Then the code in loop() will be like this pseudo code

loop() {
   checkReedSwitch();
   servo1function();
   servo2function();
  // etc
}

...R

is it possible to describe motion sequens and speed with a diagram?

The diagram is a nice idea. The concept I suggested in Reply #4 can easily match that.

...R

Okay, that makes sense. I'm a novice programmer, but I'll shoot with the pseudo code.