Arduino reset issue

Whenever I run code that has servo commands in it My arduino either does half of them then resets or it judt flshed the pin 13 led repeatedly.

Here is the code:

#include <Servo.h>

//Servos
Servo base;
Servo right;
Servo left;

//LEDs
int led = 13;
int led2 = 4;

// the setup routine runs once when you press reset:
void setup()
{
pinMode(led, OUTPUT); // initialize the digital pin as an output.
pinMode(led2, OUTPUT);

base.attach(10);
right.attach(9);
left.attach(8);

}

// the loop routine runs over and over again forever:
void loop() {
digitalWrite(led2, HIGH);
delay(1);
digitalWrite(led2, LOW);
delay(1);

digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second

base.write(90);
}

I have no idea why it crashes. When I run the example sketch sweep it works only when plugged directly into my computers port. I am using a 9v battery pack. How can I stop this from happening?

Do you really have a servo attached to the smiley face pin? Which pin is that?

How are you powering the servos? Not from the Arduino, I hope.

I am using a 9v battery pack.

Not one of those rectangular "least current for the most money" jobs, I hope.

How can I stop this from happening?

Use a real battery.

Would it be wrong to power the servo from the 5v pin? and its just a 9v. nothing special. and smiley face is pin 8

Would it be wrong to power the servo from the 5v pin?

Depends on the servo, but, in general, yes. Servos take a fair amount of current (a lot of current relative to what the Arduino can provide).

and its just a 9v. nothing special.

There are smoke detector 9V batteries and there are real 9V batteries. Which "nothing special" kind is it? Smoke detector batteries can provide very little current, which is important when trying to power a motor (as in a servo).

Its a smoke detector battery. How do you suggest I wire the servos?

Probably the way you have it now, but swap the battery for a better pack. Like mentioned, servo's have current demands which probably cannot be met by your current 9v battery. If im correct, Ni based batteries are good for bursts of current, which is why they are used in cameras for example.

Point is, each battery has a different chemistry and capacity, which in turn will feed specific needs. For example, Im working on a sensor node which measures periodically, possibly for a number of years on one battery. Average current is very low, so I use an alkaline battery, which are known for flat discharge curves and self-discharge just a few percent per year, whereas for example Li based batteries will self-discharge up to 10% a month! (but can meet higher current demands again!)

In short, map your current needs, read up on battery types and find the right kind for your project. Ill see if I can find some links in my bookmarks...

good luck!

I used two extra batteries for the left and right servos. thanks for the advice works fine now. will look into a better battery pack.

ashtonmehrle: I used two extra batteries for the left and right servos. thanks for the advice works fine now. will look into a better battery pack.

Bottom is a typical servo battery setup. Also #7 below will help with code posting:

http://arduino.cc/forum/index.php/topic,148850.0.html