Arduino board resets after Switch...Case?

Hello everyone-

So I’ve built a simple hexapod, and have started programming. I’m using an Arduino Mega 2560, with each servo command tied to a PWM pin (2-13), and the power being fed off the arduino GND & 5V (I also have 4 AA batteries plugged into the arduino to help in power). My code is below, but basically, when I tell the arduino to sit down, the USB loses connection. I think i’m pulling too much power, or not enough, not really sure. And I know my code is extrememly simple and inefficient, but its my starting point to figuring out what I’m doing. I dont have much of an electronics background, so any help would be appreciated. Thank you!

//  FL_Hip   pin   3
//  FL_Knee  pin   2
//  FR_Hip   pin   9
//  FR_Knee  pin   8
//  ML_Hip   pin   5
//  ML_Knee  pin   4
//  MR_Hip   pin   11
//  MR_Knee  pin   10
//  AL_Hip   pin   7
//  AL_Knee  pin   6
//  AR_Hip   pin   13
//  AR_Knee  pin   12  


//  = = = = = LIBRARIES = = = = =

#include <Servo.h>


// = = = = = DECLARATIONS = = = = =

Servo FL_Hip;
Servo FL_Knee;   
Servo FR_Hip;  
Servo FR_Knee;   
Servo ML_Hip;    
Servo ML_Knee;   
Servo MR_Hip;    
Servo MR_Knee;   
Servo AL_Hip;    
Servo AL_Knee;   
Servo AR_Hip;    
Servo AR_Knee;

int serialCommand = 0;

// = = = = = SETUP & ATTACHMENTS = = = = =

void setup()
{
  FL_Hip.attach(3);
  FL_Knee.attach(2); 
  FR_Hip.attach(9); 
  FR_Knee.attach(8); 
  ML_Hip.attach(5); 
  ML_Knee.attach(4); 
  MR_Hip.attach(11); 
  MR_Knee.attach(10); 
  AL_Hip.attach(7); 
  AL_Knee.attach(6); 
  AR_Hip.attach(13); 
  AR_Knee.attach(12); 

  Serial.begin(9600);
  //  delay(5000);    // This delay is to allow enough time to open the Serial Monitor.
  Serial.println("Reactor... Online.");
  Serial.println("Sensors... Online.");
  Serial.println("Weapons... Online."); 
  Serial.println("All systems nominal.");  
}


// = = = = = MAIN PROGRAM = = = = =

void loop()
{
 
  delay(1000);
  Serial.println("Awaiting command...");

  if (Serial.available())
  {
    serialCommand = Serial.read();
    Serial.println("Command received...");
  }
  
  switch(serialCommand)
  {
    case 48:
    break;
    
    case 49:
    Serial.println("Center...");
    motion_Center();
    break;
    
    case 50:
    Serial.println("Sit...");
    motion_Sit();
    break;
    
    case 51:
    motion_Leftstep();
    Serial.println("Left Step...");
    break;
  }
  
  serialCommand = 0;
  Serial.flush();
  

}

// = = = = = DEFINED FUNCTIONS = = = = =

void motion_Center()  
{

  Serial.println("Stand command received...");
  delay(1000);
  FL_Hip.write(90);
  FL_Knee.write(90); 
  FR_Hip.write(90); 
  FR_Knee.write(90); 
  ML_Hip.write(80); 
  ML_Knee.write(90); 
  MR_Hip.write(90); 
  MR_Knee.write(90); 
  AL_Hip.write(80); 
  AL_Knee.write(90); 
  AR_Hip.write(90); 
  AR_Knee.write(90); 

}

void motion_Sit()
{

  Serial.println("Sit command received...");
  delay(1000);
  FL_Hip.write(0);
  FL_Knee.write(0); 
  FR_Hip.write(180); 
  FR_Knee.write(180); 
  ML_Hip.write(0); 
  ML_Knee.write(0); 
  MR_Hip.write(180); 
  MR_Knee.write(180); 
  AL_Hip.write(0); 
  AL_Knee.write(0); 
  AR_Hip.write(180); 
  AR_Knee.write(180); 

}

void motion_Leftstep()
{
  
  Serial.println("Left step command received...");
  delay(1000);
  FL_Hip.write(40);    //
  FL_Knee.write(90);   //
  FR_Hip.write(40);    //
  FR_Knee.write(90);   
  ML_Hip.write(120);   //
  ML_Knee.write(90);   //
  MR_Hip.write(120); 
  MR_Knee.write(90); 
  AL_Hip.write(40);    //
  AL_Knee.write(90);   //
  AR_Hip.write(40); 
  AR_Knee.write(90); 
  
}

If you run the code without motors attached? does it reset too? start with 1, then 2 etc

And measure the supply voltage too... I'm not clear if you're running the servos or the Arduino or both from batteries.

First thing is to find a proper power source for your servos. The Arduino board was not designed to power heavy current users like servos. Even one servo can be problematic being powered from the Arduino's +5vdc pin. Find a good regulated (or batteries) voltage source of 4.8-6.0vdc for the servos. Be sure this new voltage source has a ground wire connected to a arduino ground pin. Budget 500ma to 1 amp per servos and then start testing your code again. Until the hardware is correct, further playing with the software is not to be fruitful.

Lefty

   case 51:
    motion_Leftstep();
    Serial.println("Left Step...");
    break;
  }
  
  serialCommand = 0;
  Serial.flush();

"case 51"

Isn't "case '3':" easier to read?

the power being fed off the arduino GND & 5V (I also have 4 AA batteries plugged into the arduino to help in power).

The Arduino will only use USB or power from the Vin (the DC Jack). Not both. 4 AA Alkaline batteries at 1.5V each will only provide 6V--best case. So 1) that's not really enough for the on-board regulator to function properly (it needs to see a minimum of 7V), especially under load and 2) the auto-switch circuit will always use USB over the batteries.

When you do resume playing with the software, might I humbly suggest you change this:

    case 48:

My keyboard does not have a 48 key on it. Now, strangely enough it does have a 0 key on it.

    case '0':

This is, as far as the compiler is concerned, exactly the same statement. But, if I were reading your code to figure out what to send from the Serial Monitor, I couldn't tell, without a reference card. With my example, I can easily tell.

Dang, groove beat me to it. Got to learn to type faster...

Hello everyone, and thank you-

Groove: "case 51" Isn't "case '3':" easier to read?

I tried doing this, but since it takes the serial input as ASCII instead of DEC, I had to convert flip it around. I'll write in a function to convert the ASCII to DEC later, but thanks!

retrolefty: First thing is to find a proper power source for your servos. The Arduino board was not designed to power heavy current users like servos. Even one servo can be problematic being powered from the Arduino's +5vdc pin. Find a good regulated (or batteries) voltage source of 4.8-6.0vdc for the servos. Be sure this new voltage source has a ground wire connected to a arduino ground pin. Budget 500ma to 1 amp per servos and then start testing your code again. Until the hardware is correct, further playing with the software is not to be fruitful.

Right now, I'm using a 4-pack of AA's plugged into the arduino, as well as the USB cable for power. I know when I start running this without the USB i'll need a bigger powerpack for it, and I'm already looking around for that. And please forgive my ignorance, but where would I plug the power pack into?

MarkT: And measure the supply voltage too... I'm not clear if you're running the servos or the Arduino or both from batteries.

See the above, I'm running both right now.

robtillaart: If you run the code without motors attached? does it reset too? start with 1, then 2 etc

[/

robtillaart: I didnt even think of that. Tried running without the power to servos, and it doesn't cut out, so I'm drawing too little, too much power? Also, I noticed, it only died on CASE 2, the sit command, so if i told it to stand, then left move, then stand, it would run fine. But, when I enter the sit command, that puts it to sleep.

Also, one last thing, the LED on an arduino is hooked up to pin 13. When you're transferring a program, the LED pin blinks to indicate activity. Unfortunately, my rear right leg is tied to 13 as well, so when I transfer a program, the leg starts having fits (it almost looks like the leg is going up and taking a leak on my desk). Is there any way to fix this or disable it? Thanks!

BTW, you're all awesome, a million thanks for your help!

So I get that I'm pulling too much power via USB to supply all the servos properly, hence it cutting out. Is there a way to boost the power while its on USB?

Also, every time I plug this in, all the servos power up, and it stands up. Is this right, or do I have a mixed wire somewhere?

I tried doing this, but since it takes the serial input as ASCII instead of DEC, I had to convert flip it around

I don't understand this statement.

ttkoshi: So I get that I'm pulling too much power via USB to supply all the servos properly, hence it cutting out. Is there a way to boost the power while its on USB?

Also, every time I plug this in, all the servos power up, and it stands up. Is this right, or do I have a mixed wire somewhere?

All the servos red wires (+5vdc) need to be connected to a independent power source, not the arduino board, not USB power.

Groove:

I tried doing this, but since it takes the serial input as ASCII instead of DEC, I had to convert flip it around

I don’t understand this statement.

I figured it out.

I didn’t realize I could put a number in single quotes to get it to read properly. If i just had Case 0, nothing would happen, so I looked up the ascii of 0 and thats what I had the Case set as

Yes definitely don't run the servos from the Arduino's power, have them separately powered by the batteries (but connect grounds together). That way if the batteries aren't powerful enough the servos will wimp out but the Arduino won't be affected. And look at the battery voltage to see if they are struggling - rechargeable batteries are recommended for better current-handling.

Thank you all so infinitely much, especially MarkT, that got everything working proper. Now, hopefully a quick question. I have 12 Hitec HS-422 servos, each 4.8-6.0 volts, and 8-150 mA. So, for a battery source, what should I be looking for? Is it simply multiplying the numbers by 12:

4.8-6.0 volts x 12 servos = 57.6-72.0 volts 8-150 mA x 12 servos = 96-1800 mA

So a 72 volt battery at max 1800 mA? Does this sound right, or am I missing some crucial electricity element here? Thanks!

Again, my apologies that my electronic skills are noob-like, but I'm hoping to go to school for engineering in the next 2 years, so hopefully by then I'll be able to help others as well.

Is it simply multiplying the numbers by 12:

If they're all moving at the same time...maybe. Depends how fast they're moving, what load they have. Not simple.

Well those were the min/max numbers i pulled off the spec sheet:

http://www.trossenrobotics.com/images/productdownloads/hitec/hs422.pdf

So is that the battery I should be using? Thanks!

You need to multiply the current requirements by 12, NOT the voltage . If the servos operate between 4.8 volts and 6.0 volts, you need a battery, or batteries, the measure between 4.8 volts and 6.0 volts.

Those batteries need to be able to supply the maximum current needed for all the servos to run at the same time, so the current requirement of one servo gets multiplied by 12.

Only multiply the current by 12 as all are in parallel... So about 2A max current - alas many battery sellers aren't interested in making their battery specifications easy to find, but I think decent high-capacity high-quality NiMH's will provide that, perhaps as C-cells rather than AA's. Good alkaline C-cells ought to be happy too I reckon - there's no substiture for testing though (different manufacturers cells are different - go for high-current batteries designed for cameras etc.)

Of course the size of battery is also affected by the battery life you want - which depends on the duty cycle of all the servos...