help. Arduino is stuck

as I turn on the arduino and the 7 servos using PWM controlled by the potentiometers the servo freezes.
the 7 servo is powered by an external power supply

#include <Servo.h>   
Servo myservo;   // create servo object to control a servo 
Servo myservo1;
Servo myservo2;
Servo myservo3;
Servo myservo4;
Servo myservo5;




int potpin = 0;  // analog pin used to connect the potentiometer
int potpin1 = 1;
int potpin2 = 2;
int potpin3 = 3;
int potpin4 = 4;
int potpin5 = 5;
int val;    // variable to read the value from the analog pin 
void setup() 
{ 
  myservo.attach(3);  // attaches the servo digital input 
   myservo1.attach(5);
    myservo2.attach(6);
     myservo3.attach(9);
      myservo4.attach(10);
       myservo5.attach(11);
        
} 
void loop() 
{ 
  val = analogRead(potpin);            // reads the value of the potentiometer (value between 0 and 1023) 
  val = map(val, 0, 1023, 0, 179);    
  myservo.write(val);                   
  delay(15);                           // waits for the servo to get there 
    val = analogRead(potpin1);            // reads the value of the potentiometer (value between 0 and 1023) 
  val = map(val, 0, 1023, 0, 179);     // scale it to use it with the servo (value between 0 and 180) 
  myservo1.write(val);                  // sets the servo position according to the scaled value 
  delay(15);                           // waits for the servo to get there 
    val = analogRead(potpin2);            // reads the value of the potentiometer (value between 0 and 1023) 
  val = map(val, 0, 1023, 179, 0);     // scale it to use it with the servo (value between 0 and 180) 
  myservo2.write(val);                  // sets the servo position according to the scaled value 
  delay(15);                           // waits for the servo to get there 
    val = analogRead(potpin3);            // reads the value of the potentiometer (value between 0 and 1023) 
  val = map(val, 0, 1023, 179, 0);     // scale it to use it with the servo (value between 0 and 180) 
  myservo3.write(val);                  // sets the servo position according to the scaled value 
  delay(15);                           // waits for the servo to get there 
    val = analogRead(potpin4);            // reads the value of the potentiometer (value between 0 and 1023) 
  val = map(val, 0, 1023, 179, 0);     // scale it to use it with the servo (value between 0 and 180) 
  myservo4.write(val);                  // sets the servo position according to the scaled value 
  delay(15);                           // waits for the servo to get there 
    val = analogRead(potpin5);            // reads the value of the potentiometer (value between 0 and 1023) 
  val = map(val, 0, 1023, 0, 179);     // scale it to use it with the servo (value between 0 and 180) 
  myservo5.write(val);                  // sets the servo position according to the scaled value 
  delay(15);                           // waits for the servo to get there 
  
}

that's the code.

that's the code.

Why doesn't it have tags around it?

7 servos

I count 6.

sorry about that, newbie here. 2 servos are attached to pin 5. is that ok?

Servos do not have to be attached to PWM pins.

Servo myservo;   // create servo object to control a servo 
Servo myservo1;
Servo myservo2;
Servo myservo3;
Servo myservo4;
Servo myservo5;

Is that how you count? Nothing, one, two, three...?

You know you don't have to use PWM pins to drives servos using the Servo library?
Two servos on one pin should be fine.
Your code is about 30 lines too long.
Check your power wiring - make sure you've got a common ground and that the wires are beefy enough to carry the current.

Does it work if you unplug all but one of the servos ?

I'm sorry, I couldn't resist.

This is how you should (IMHO) be writing your program:

#include <Servo.h>   

// This is what we want in our servo settings array
typedef struct {
  byte servoPin;
  byte analogPin;
  byte angle;
} servoconfig;

// Array to store settings for servos
servoconfig servodata[6] = {
  {3, 0, 0},
  {5, 1, 0},
  {6, 2, 0},
  {9, 3, 0},
  {10, 4, 0},
  {11, 5, 0}
};

Servo servos[6];  // Array to store actual servos

// Get new ADC values and map them to angles.  Store them in the
// settings array
void getAngles()
{
  byte i;
  int dummyValue;
  for (i=0; i<6; i++) {
    dummyValue = analogRead(servodata[i].analogPin); // Dummy read and short delay to clear S&H cap and ensure good readings
    delay(10);
    servodata[i].angle = map(analogRead(servodata[i].analogPin), 0, 1023, 0, 179);
  }
}

// Update the servos with the angles in the config array
void updateServos()
{
  byte i;
  for (i=0; i<6; i++) {
    servos[i].write(servodata[i].angle);
  }
}

// Set everything up.
void setup()
{
  byte i;
  for (i=0; i<6; i++) {
    servos[i].attach(servodata[i].servoPin);
    servos[i].write(0);
  }
}

// Do the work
void loop()
{
  getAngles();
  updateServos();
}

Much cleaner and easier to understand, yes?

I have loaded it onto one of my UNOs and tested that it gives pulses out on the right pins when pots change. I don't have any servos to test it with though.

servoconfig servodata[6] = {

Going to be hard to fit all 7 servos in that array, no?

Sometimes these problems are fun to work in Bitlash. If you'll spot me moving the servos to D2..D7 so I can use the serial port it's a two-liner something like this:

// Six pots on A0..A5 control six servos on D2..D7
// (with Bitlash servo example sketch on Arduino)

function startup { run setservo; }
function setservo { servo(i+2, ar(i)*180/1024); if (++i>5) i=0; }

-br

Much cleaner and easier to understand, yes?

Cleaner, yes, but easier to understand, I am not so sure.
That is going to depend on what you know and understand already.
You know and appreciate structs, arrays and functions but I am willing to bet that the OP doesn't.

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

How quaint. XD

I think my external power supply is not working. is it ok to put the battery direct to the servos? I'm using 4 AA's =6v

PaulS:

servoconfig servodata[6] = {

Going to be hard to fit all 7 servos in that array, no?

Not when, as the OP states, two servos share one pin.

UKHeliBob:

Much cleaner and easier to understand, yes?

Cleaner, yes, but easier to understand, I am not so sure.
That is going to depend on what you know and understand already.
You know and appreciate structs, arrays and functions but I am willing to bet that the OP doesn't.

Then the OP can use it to learn about structs, arrays and functions.

Groove:

byte i;

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


How quaint. XD

What's quaint about that?

You'd rather for (byte i=0; ... ? Some versions of C (or the IDE's default settings) don't allow that kind of construct, so rather than try and remember which do and which don't, I just don't use it. Or is it the "i" that you find quaint? After decades of programming I have become used to "i" being "iterator" and I use it for pretty much all loops. Yes, it started out with systems that only allow single character variables (BASIC) back in the dark ages (when computers came in black cases)...

Yes, it started out with systems that only allow single character variables (BASIC)

sp. "FORTRAN", and "i" because it is (by FORTRAN convention) an integer and not a real.

Some versions of C (

But we're using C++, so it is never a problem.

back in the dark ages (when computers came in black cases)...

In 2012, my laptop came in a black case. It's still in it.

PaulS:

back in the dark ages (when computers came in black cases)...

In 2012, my laptop came in a black case. It's still in it.

Ok, I'll be more specific... Back when computers came in black cases and plugged into TVs through the aerial socket tuned to channel 36 and had 1-bit audio and an external tape player. Unless you were posh, when they came in a beige case, but still had the external tape player :wink:

back in the dark ages (when computers came in black cases)...

My old PDP-8 was a fetching shade of beige, brown and orange, and addressable units were 50% wider than the sort we have today.
The teletype was grey.
The KV storage tube was the closest we got to a TV
{sigh}

You were uber-posh...