For Loop and Multiple Servos

I wonder if anyone can help to find out what is the probe with the for loop below.

I have Six servos connected to Pins (2,3,4,5,6 and 7). Then I made this for loop so that each servo rotates one after another.

But when I run the sketch: it runs servo at pin 2, then at pin 3, pin 4, and pin 5. but servos connected to Pins 6 and 7 o not rotate!!!

Any ideas - much appreciated.

#include <Servo.h>

 Servo myservo[6]; 
  
int pinAttch = 0;    // variable to store pin attachment
int servWrite = 0;    // variable to store servo writing

void setup() {
  Serial.begin(9600);
  for(pinAttch = 2; pinAttch < 7; pinAttch++)
    {                                                
    myservo[pinAttch].attach(pinAttch);
    }  
  }

void loop() {

for(servWrite = 2; servWrite < 7; servWrite++)
  {                                                
   
  myservo[servWrite].write(80);
  delay(2000);

  }
exit(0); 

}

I can only tell you why the servo on pin 7 is not working. Because you said “less than 7” here:

for(servWrite = 2; servWrite < 7; servWrite++)

What is with the “exit(0)” at the end of loop()? That is really loopy. :slight_smile: If you want your code to run only once, just put it in setup().

I think you meant:

myservo[pinAttch - 2].attach(pinAttch); // myservo 0..5

to prevent the bounds of myservo being exceeded.

Hi aarg and 6v6gt ---> thanks for the reply.

I tried what you suggested but still no luck.

  1. I increased the upper bound of the for loop to more than 7 but still the servos are stuck till servo No. 4 connected at Pin 5.

  2. I also tried your suggestion 6v6gt but no luck...!

any thoughts...! Thank you.

any thoughts...! Thank you.

Post your latest code.

You would also have had to change the following code

for(servWrite = 2; servWrite < 7; servWrite++)
  {                                               
   
  myservo[servWrite].write(80);
  delay(2000);

  }

Because you are attempting to use the same index to address a 6 element array which goes form 0–>5 and a set of pins which go from 2 to 7.

It should be

for(servWrite = 2; servWrite <= 7; servWrite++)
  {                                               
   
  myservo[servWrite - 2 ].write(80);
  delay(2000);

  }

... so I found a fix. :slight_smile: :slight_smile: I changed the line code below

Servo myservo[6];

To Be

Servo myservo[8];

The old code was parsing 6 integer location for the servos. Then because the loop is bonded to begin from 2, then no matter what the upper bound is, it will just count 4 times. That is why servo counts after 4 do not receive any signal.

:slight_smile: :slight_smile: Thank you a for the help.

The 'fix' lacks elegance, but good if it works for you.

Have a look at this recent Reply in another Thread.

…R

Hi,
It seems like you need to consider where you went wrong conceptually. From your code it seems that you had mixed up the array indexer with the servo pin that you want to attach to. You really need to loop from 0 to -1. This will allow you to keep your array at the correct size instead of having to add extra positions to it.

Now that your array and loop are in synchronization with each other, you need to correctly attach the pin to the servo. Since your pins start at 2, you could do one of two things to handle the issue:

  1. Declare another int that is initialized to 2 and increment it along with the loop (name it something like currentPin) and attach the servo using this variable. (this type of code is more self documenting and the extra variable really doesn’t waste that much space, it probably takes less space than the extra unneeded array positions)

  2. Do some magic number mathematics involving the loop counter to get the difference between 0 and the first pin then attaching the servo based on that. I don’t really like this method because it is slightly unclear what is happening and it looks like magic numbers even though there is a reason for it.

Anyhow sorry if I am butting in, I have just made this type of mistake before and it always helps me to think about what the root of the problem is.

… it is working fine now-I am glad for all your help and sharing the support.
Thank you Arduino forum for the help