Need some help for a science project.

Wait a second... How do I add a servo? Here's the line of code.

// attach the servos
for(int nServo = 0;nServo < CONNECTED_SERVOS;nServo++)
{
myServos[nServo].attach(SERVO_TO_PIN(nServo));
}

At the top of DuaneB's code it says this...

#include <Servo.h>
// Sample sketch for driving 12 Servos from an Arduino UNO, servos are attached to digital pins 2,3,4,5,6,7,8,9,10,11,12,13

#define CONNECTED_SERVOS 12

It sweeps a whole load of servos on those pins....

So to attach servos to the board, you control them by putting their yellow / orange wires into those pins.

(The #define CONNECTED_SERVOS 12 tells it there are 12 and you could reduce that number I guess to just have however servos you have)

This part:

 // attach the servos
  for(int nServo = 0;nServo < CONNECTED_SERVOS;nServo++)
  {
    myServos[nServo].attach(SERVO_TO_PIN(nServo));
  }

... uses the value of "CONNECTED_SERVOS" which is 12 if you didn't alter it at the top as the upper limit of a counter in a loop and runs the "attach" line as many times as you have servos to as many pins as there are servos.

This is an automated way of doing what I showed you a few posts back where I had something like myservo.attach and anotherservo.attach. The myServos[nServo] in Duane's code is a way of automating say myServos5 and myServos9 or whatever where nServo is a variable instead of hardcoding the same line a stack of times each with a different number.

This is all so confusing, but it has to get done...

Falchion:
This is all so confusing, but it has to get done...

After seven pages I've lost track of what you have got working, and where you're stuck.

What's the current problem?

It's cool. Here's where I am right now... I built the hand itself, but I need to get multiple servo's running at once, so I can tie the strings on the fingers into the holes in the servo motor and I'm having some trouble getting 1 to run on external power.

Falchion:
I'm having some trouble getting 1 to run on external power.

Here's my suggestion then:

1- Put DuaneB's multi servo sketch aside for now.
2- Reload sweep, which I think u said was working before?
3- Carefully check the wiring* of the one servo, your Fritzing pic showed you had the red and black confused. Keep the Arduino powered from the PC...
4- Run sweep to verify that the servo runs from external power.
5- Then and only then, edit sweep along the lines of my suggestion many posts back, to have a second servo ("myOtherServo" for instance) on another pin and run that new sketch to verify two servos

Then we'll talk some more...

  • wiring is battery +ve to servo red, -ve to servo black and also Arduino ground, yellow / orange to Arduino pin to which servo is attached in sketch. Check the voltage is what the servo needs, probably 4.8 to 6, not the 9 you were using

Something along the lines of the Fritz attached

And two would be thus....

Make sure the yellow wires go to the pins you attach the servos to in the skecth

Will a battery holder that can hold 6 batteries work with only 4 inside?

Not normally, no

Thanks. It works fine with 6. :slight_smile:

Falchion:
Will a battery holder that can hold 6 batteries work with only 4 inside?

Only if you jumper out the two empty spaces with a soldered jumper wires, or find solid brass rods of the same size as the batteries to use as 'dummy cells'.

Lefty

Hmmm... I got one servo running from external power on pin 9, but the code I tried to write (newbie programmer here) doesn't seem to work. I looked at the reference library on the Arduino website too.

, but the code I tried to write (newbie programmer here) doesn't seem to work

That's sad.
(Hint)

#include <Servo.h>

Servo myservo; // create servo object to control a servo
// a maximum of eight servo objects can be created

int pos = 0; // variable to store the servo position

void setup()
{
myservo.attach(9); // attaches the servo on pin 9 to the servo object
}

void loop()
{
for(pos = 0; pos < 180; pos += 1) // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
for(pos = 180; pos>=1; pos-=1) // goes from 180 degrees to 0 degrees
{
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
}

OK, so what does it do that it shouldn't and what doesn't it do that it should?
Code tags would be good too.

It doesn't move at all when I upload it.

And everything is plugged in correctly too.

I setup the 2nd servo just like in the picture, and the power is somehow routed to the first one... Why?

the power is somehow routed to the first one..

how?