Can move only 2 servos out of 6.

Hi Guys.
I have this problem that only 2 out of 6 servos are moving. Here is my code:

#include <Servo.h>

Servo s[6];

void setup() {
}

void loop() {

  int k=1;

  for(int i=0;i<6;i++){

    k=i;

    s[k].attach(i+2);
    
    s[k].write(45);             
    delay(1000); 
    s[k].write(90);          
    delay(1000);

    s[k].detach();
    }

  while(1);
}

when I go with k=i in for loop, only first 2 servos will move and the rest won't react but if i change so that k will be constant (for example k=1) every servo will go. Problem happens for k>=2. Does anyone had same issue or know what maybe a solution?

I'm using Arduino nano with Atmega168.

Thanks for the help

There is NO reason to keep attaching and detaching servos. So, stop doing it.

There is NO reason to have two variables with the same value in the for loop. So, stop doing it.

What Arduino are you using? How are the servos powered?

By the way, array indices start at 0, not 1.

I know both about the stupid things in that code :smiley:

The problem is that only first 2 declared servos are reacting no matter what. For example if I go with s[0].attach(6) servo will work but s[3].attach(6) won't.

Attach your servos in setup() and just give them values with write() in loop()

See if that works.

...R

#include <Servo.h>

Servo s[6];

void setup() {
   s[0].attach(2);
   s[1].attach(3);
   s[2].attach(4);
   s[3].attach(5);
   s[4].attach(6);
   s[5].attach(7);
}

void loop() {
  for(int i=0;i<6;i++){
    s[i].write(45);             
    delay(1000); 
    s[i].write(90);          
    delay(1000);
    }
delay(5000);
}

Still only first 2 work

As PaulS asked, what board are you using and how are your servos powered?

Yes, a wiring diagram will help us help you.
Get out a pencil and paper, show how things are attached, take a picture of it and post it here.
While you have your camera out, snap a picture of your project and post that too.

And put some Serial.print() statements into your program so you can watch how it works its way through your code.

If you are drawing too much power from the Arduino when the 3rd servo tries to move the Arduino may reset.

Have you tried changing the order in which the servos are used - for example

for(byte i = 5; i >= 0; i--){

...R

So I checked everything as you said and what I found using Serial.print(s*.attached()) that for some reason only servos 0 and 1 are attached. Before you ask i have updated servo library. I'm arduino nano expansion board so it maybe power supply issue but I doubt that. Can it be like that because of me using arduino nano with Atmega168?*

from the documentation

Servo motors have three wires: power, ground, and signal. The power wire is typically red, and should be connected to the 5V pin on the Arduino board. The ground wire is typically black or brown and should be connected to a ground pin on the Arduino board. The signal pin is typically yellow, orange or white and should be connected to a digital pin on the Arduino board. Note that servos draw considerable power, so if you need to drive more than one or two, you'll probably need to power them from a separate supply (i.e. not the +5V pin on your Arduino). Be sure to connect the grounds of the Arduino and external power supply together.

flamaster:
Before you ask i have updated servo library.

Now he tells us.

I have been assuming all along that you are using the standard servo library.

It looks like your changes have broken it.

What changes have you made?

...R

I said that because old ver of library support ony 2 servos.

Problem is even if I hook 1 servo (to pin 4) but declare 6 servos as above if i go with s [1].attach (4) it works but s [5].attach (4) won't.

flamaster:
I said that because old ver of library support ony 2 servos.

What version of the IDE are you using? Are you still in the dinosaur period?

...R

Hi,

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Can you please post a picture of your project so we can see your layout.

Thanks.. Tom... :slight_smile:

Hi,
Can you try this code..

#include <Servo.h>

Servo s0;
Servo s1;
Servo s2;
Servo s3;
Servo s4;
Servo s5;

void setup() {
  s0.attach(2);
  s1.attach(3);
  s2.attach(4);
  s3.attach(5);
  s4.attach(6);
  s5.attach(7);
}

void loop() {

  s0.write(45);
  delay(1000);
  s0.write(90);
  delay(1000);

  s1.write(45);
  delay(1000);
  s1.write(90);
  delay(1000);

  s2.write(45);
  delay(1000);
  s2.write(90);
  delay(1000);

  s3.write(45);
  delay(1000);
  s3.write(90);
  delay(1000);

  s4.write(45);
  delay(1000);
  s4.write(90);
  delay(1000);

  s5.write(45);
  delay(1000);
  s5.write(90);
  delay(1000);

  delay(4000);
}

Tell me what it does.

Thanks.. Tom.. :slight_smile:

I tried your code TomGeorge and only servos on pins 2 and 3 are working. What's more I changed to

void setup() {
  s0.attach(7);
  s1.attach(6);
  s2.attach(5);
  s3.attach(4);
  s4.attach(3);
  s5.attach(2);
}

and now only servos on pins 7 and 6 are working. From here I tried to change order

void setup() {
  s5.attach(7);
  s4.attach(6);
  s3.attach(5);
  s2.attach(4);
  s1.attach(3);
  s0.attach(2);
}

And here again only servos on pins 2 and 3 are working.

@flamaster, you say that you modified the servo library.
Can you post that code?
Where did you get the servo library that you started from? Do you still have that is its original form? Attach that as well.

I only said that i have the latest version of library and i didnt edit it in any way.

flamaster:
Before you ask i have updated servo library.

Ah, my bad then, I thought that you had modified the servo library.
You said you updated it.

I think he meant he updated it the library to the latest available version