Cannot rotate the servo

Hi everyone, I am doing a project on an RC car with a claw to pick objects.

I used Bluetooth to send command to the Arduino and control the various hardware components.

I can control the car to move but I cannot rotate the servo of the claw.

When I try to rotate the servo, the servo only rotates a little and the led on pin 13 of the Arduino keeps blinking.

I have no idea of this problem, can anyone help me?

#include <Servo.h>

Servo myServo;
const int controlPin1 = 4;
const int controlPin2 = 5;
const int controlPin3 = 2;
const int controlPin4 = 3;
const int enablePin1 = 6;
const int enablePin2 = 11;

int motorEnabled1 = 0; // Turns the motor on/off
int motorEnabled2 = 0; // Turns the motor on/off
int motorSpeed = 110; // speed of the motor
int motorDirection1 = 0; // current direction of the motor
int motorDirection2 = 0; // current direction of the motor

String inputString = "";

void setup() {
  // intialize the inputs and outputs
  pinMode(controlPin1, OUTPUT);
  pinMode(controlPin2, OUTPUT);
  pinMode(enablePin1, OUTPUT);
  pinMode(controlPin3, OUTPUT);
  pinMode(controlPin4, OUTPUT);
  pinMode(enablePin2, OUTPUT);

  // pull the enable pin LOW to start
  digitalWrite(enablePin1, LOW);
  digitalWrite(enablePin2, LOW);

  myServo.attach(9);
  Serial.begin(9600);
}

void loop() {
  if (Serial.available()) {
    char inChar = (char)Serial.read();
    if (inChar == 'a') {
      motorEnabled1 = 1;
      motorEnabled2 = 1;
      motorDirection1 = 0;
      motorDirection2 = 0;
    } else if (inChar == 'b') {
      motorEnabled1 = 1;
      motorEnabled2 = 1;
      motorDirection1 = 1;
      motorDirection2 = 1;
    } else if (inChar == 'c') {
      myServo.write(0);
      delay(15);
    } else if (inChar == 'd') {
      myServo.write(180);
      delay(15);
    } else if (inChar == 'e') {
      motorEnabled1 = 1;
      motorEnabled2 = 1;
      motorDirection1 = 0;
      motorDirection2 = 1;
    } else if (inChar == 'f') {
      motorEnabled1 = 1;
      motorEnabled2 = 1;
      motorDirection1 = 1;
      motorDirection2 = 0;
    } else if (inChar == 'g') {
      motorEnabled1 = 0;
      motorEnabled2 = 0;
    } else if (inChar == 'h') {
      Serial.println(motorSpeed);
    } else if (inChar == 'i') {
      motorSpeed = inputString.toInt();
      inputString = "";
    } else {
            inputString += inChar;
    }
  }
      if (motorDirection1 == 1) {
        digitalWrite(controlPin1, HIGH);
        digitalWrite(controlPin2, LOW);
      }
      else {
        digitalWrite(controlPin1, LOW);
        digitalWrite(controlPin2, HIGH);
      }

      if (motorDirection2 == 1) {
        digitalWrite(controlPin3, HIGH);
        digitalWrite(controlPin4, LOW);
      }
      else {
        digitalWrite(controlPin3, LOW);
        digitalWrite(controlPin4, HIGH);
      }
      if (motorEnabled1 == 1) {
        analogWrite(enablePin1, motorSpeed);
      }
      else {
        analogWrite(enablePin1, 0);
      }
      if (motorEnabled2 == 1) {
        analogWrite(enablePin2, motorSpeed);
      }
      else {
        analogWrite(enablePin2, 0);
      }
}

If you are powering the servo from the Arduino 5 volt supply, the Arduino may be resetting due to the servo pulling the supply voltage down. Servos generally need about 1 amp, especially if they are loaded. Try with an external supply capable of supplying enough current for the servo. To see if the Arduino is reseting , put a Serial.print in setup(). The print will only show when the Arduino is reset.

groundfungus:
If you are powering the servo from the Arduino 5 volt supply, the Arduino may be resetting due to the servo pulling the supply voltage down. Servos generally need about 1 amp, especially if they are loaded. Try with an external supply capable of supplying enough current for the servo. To see if the Arduino is reseting , put a Serial.print in setup(). The print will only show when the Arduino is reset.

My Arduino is using a 9V supply via the Vin pin and the servo is connected to the 9V as well.

samcheng92:
the servo is connected to the 9V as well.

What voltage does the servo documentation say it needs?- most servos encountered on this site are 4.8-6V.

If your 9V supply is a little transistor radio battery (PP3 I think the code is) well those things can't provide any current to speak of.

Did you implement gf's idea of a print in setup()?

JimboZA:
What voltage does the servo documentation say it needs?- most servos encountered on this site are 4.8-6V.

If your 9V supply is a little transistor radio battery (PP3 I think the code is) well those things can't provide any current to speak of.

Did you implement gf's idea of a print in setup()?

I tried to use the USB cable to power the Arduino and a 9V cell to power the servo. This worked!
So I can know that it is due to power problem not about the coding. Thanks!

You are providing 9V to the servo? Is it a 9V servo? Most are 5-6 as I said, so you need to check if 9V is ok else you're going to damage the servo.