Servos moving unpredictably

Hello,

I am working on a project with the Arduino Mega using three servos. These are actually special-order multi-turn servos that rotate up to 6 revolutions. Here is the link to the product on Hobby King's website:

Also copied here is my code. What I am trying to do is send a character over the serial monitor and have one of the servos move according to the rules I set up for each character. For the most part, the code works and each servo behaves as expected. However, every so often, the servos just start moving on their own without any decipherable reason or pattern. Usually when this starts to happens it seems that the board just starts ignoring the input from the serial monitor and the servos continue to do their own thing.

I am somewhat a newbie so hopefully I have described my problem adequately. Any comments or help is greatly appreciated. Thank you.

#include <Servo.h> //include servo function
#define BUFFERSIZE 127
uint8_t inBuffer[BUFFERSIZE];
int inLength; // length of data in the buffer
int numLoop = 0; // number of times we looped
char val;

Servo servo_1;  //for servo function
Servo servo_2;  //for servo function
Servo servo_3;  //for servo function


void setup() {
Serial.begin(9600); //baud rate I set for bluetooth module is 38400
servo_1.attach(2); //servo 1 at pin 2?
servo_2.attach(3); //servo 2 at pin 3?
servo_3.attach(5); //servo 3 at pin 5?


// motor_reset(); // call function to reset the motor
}
void loop() {
// read string if available
if (Serial.available()) {
  inLength =  0;
  while (Serial.available()) {
    val = Serial.read();
  }
 
  Serial.print("Arduino received: "); //send the text to the android device
  Serial.write(val);
  Serial.println();
}


  if (val == 'a') 
  {
    servo_1.write(2000);
  }
  
   if (val == 'b')  
  {
    servo_1.write(1800);
  }
 
   if (val == 'c')  
  {
    servo_1.write(1600);
  }   
  
   if (val == 'd')  
  {
    servo_1.write(1400);
  }

   if (val == 'e')  
  {
    servo_1.write(1200);
  }

   if (val == 'f')  
  {
    servo_1.write(1000);
  }

   if (val == 'g')  
  {
    servo_1.write(0);
  }


  

   if (val == 'h')  
  {
    servo_2.write(2000);
  }

   if (val == 'i')  
  {
    servo_2.write(1800);
  }

   if (val == 'j')  
  {
    servo_2.write(1600);
  }

   if (val == 'k')  
  {
    servo_2.write(1400);
  }

   if (val == 'l')  
  {
    servo_2.write(1200);
  }

   if (val == 'm')  
  {
    servo_2.write(1000);
  }

   if (val == 'n')  
  {
    servo_2.write(0);
  }





   if (val == 'o')  
  {
    servo_3.write(2000);
  }

   if (val == 'p')  
  {
    servo_3.write(1800);
  }

   if (val == 'q')  
  {
    servo_3.write(1600);
  }

   if (val == 'r')  
  {
    servo_3.write(1400);
  }

   if (val == 's')  
  {
    servo_3.write(1200);
  }

   if (val == 't')  
  {
    servo_3.write(1000);
  }

   if (val == 'u')  
  {
    servo_3.write(0);
  }







   if (val == ' ')  
  {
    servo_1.write(0);
    servo_2.write(0);
    servo_3.write(0);
  }
  

  

}

Please read the "How to use this forum" post, and post your code properly, using code tags.

95% or more of servo problems reported on this forum are due to an inadequate servo power supply. You need a separate servo power supply, capable of providing 5V to 6V, and capable of delivering at least 1 Ampere per servo. Connect all the grounds together.

Thank you very much for the help- code is now posted with code tags.

I will work on the power issue to see if that resolves things. Will update with my findings shortly.

Have you tried it with only one servo connected? Does a single servo still creep?

Its annoying but most hobby servo datasheets say nothing about the peak stall current, which is the one
important figure you need to know to power them. If the supply cannot provide enough current for the peaks
the servo will typically crash/reset/go haywire.

You should never think of powering servos and motors from the logic 5V supply of the Arduino, that's totally
underpowered and risks damaging the Arduino from stray voltage spikes. Motor supplies and logic supplies
need to be separate for a well behaved system.

Thank you all for your time and your thoughtful replies. I am now powering the servos externally from a 6V 3A supply and it now works as expected. Good to know about the peak stall current- I will keep that in mind for all my servo projects going forward. Thanks again everyone- this is such a wonderful community and resource.