a question with servo and motor

Hi, I am new with arduino,I wrote a programm, I want to control servo and motor by serial with arduino.However,only servo can work,motor do not work ,what is the problem?Can you help me?Thanks!The code is below

include <Servo.h>	//servo
//motor
int in1 = 7;//connect L298N for motor contrlo  
int in2 = 8;//connect L298N for motor contrlo  
int in3 = 12;//connect L298N for motor contrlo  
int in4 = 13;//connect L298N for motor contrlo  

//2 servos 
Servo servoX; //servo,move left and right
Servo servoY; //servo move up and down

//serial data
char val;

//car stop
void stop(){
  digitalWrite(in1,LOW);
  digitalWrite(in2,LOW);
  digitalWrite(in3,LOW);
  digitalWrite(in4,LOW);
}

//car move forward
void forward()
{
  digitalWrite(in1,HIGH);
  digitalWrite(in2,LOW);
  digitalWrite(in3,LOW);
  digitalWrite(in4,HIGH);
}

//car move backward
void back()
{
  digitalWrite(in1,LOW);
  digitalWrite(in2,HIGH);
  digitalWrite(in3,HIGH);
  digitalWrite(in4,LOW);
}

//car move left
void left()
{
  digitalWrite(in1,LOW);
  digitalWrite(in2,HIGH);
  digitalWrite(in3,LOW);
  digitalWrite(in4,HIGH);
}

//car move right
void right()
{
  digitalWrite(in1,HIGH);
  digitalWrite(in2,LOW);
  digitalWrite(in3,HIGH);
  digitalWrite(in4,LOW);
}

//servo turn left
void servo_left()
{
  int servotemp=servoX.read();//get the degree of servo
  servotemp-=1;//servo turn 1 degree
  if(servotemp<170&&servotemp>10) //servo's degree between 10 and 170 degrees
  servoX.write(servotemp);
}

//servo turn right
void servo_right()
{
  int servotemp=servoX.read();//get the degree of servo
  servotemp+=1;//servo turn 1 degree
  if(servotemp<170&&servotemp>10)//servo's degree between 10 and 170 degrees
  servoX.write(servotemp);
}

//servo turn up
void servo_up()
{
  int servotemp1=servoY.read();//get the degree of servo
  servotemp1+=1;//servo turn 1 degree
  if(servotemp1<170&&servotemp1>10)//servo's degree between 10 and 170 degrees
  servoY.write(servotemp1);
}

//servo turn down
void servo_down()
{
  int servotemp1=servoY.read();//get the degree of servo
  servotemp1-=1;//servo turn 1 degree
  if(servotemp1<170&&servotemp1>10)//servo's degree between 10 and 170 degrees
  servoY.write(servotemp1);
}


void setup()
{
  pinMode(7, OUTPUT); 
  pinMode(8, OUTPUT);
  pinMode(12, OUTPUT);
  pinMode(13, OUTPUT);
  Serial.begin(9600);//set serial band rate is 9600bps
  servoX.attach(10);//servo,move left and right connect 10 pin
  servoY.attach(11);//servo,move up and down connect 11 pin
  servoX.write(90);//set servo's first degree is 90 degrees
  servoY.write(90);//set servo's first degree is 90 degrees
}


void loop()
{
  val=Serial.read();
  switch(val)
 {
   case '0':stop(); break;
   case '1':forward(); break;
   case '3':back(); break;
   case '4':left(); break;
   case '2':right(); break;
   case '5':servo_left(); break;
   case '6':servo_right(); break;
   case '7':servo_up(); break;
   case '8':servo_down(); break;  
   default:
     stop();     
     break;
 }
}

Moderator edit: Code put into code box. Please use the # icon on the editor’s toolbar to get code tags when posting code.

val=Serial.read();
  switch(val)

Servos will drive themselves, but what if there is no character to read?
Your default is “stop()”

The motor probably is “working”, just not for very long.

Thanks for your reply?but I still do not understand,the default is "stop()",means if I do not send charactor ,the motor is "stop()",but if I send '1','3','2','4' from serial ,the motor should be work ,I do not think there is problem in the code,would you please tell me how to modify it for detail?

Think about how "loop" works. It loops, many, many times a second. It loops many times in the amount of time it takes to transmit a single character. Every time through loop, you perform a Serial.read, even if there isn't a character to be read. If there isn't a character to read, then Serial.read returns -1, which causes "stop()" to be called.

You should do the reading of the serial port and the "switch (val) ..." only if there is a character to read at the serial port. If you look into the arduino reference about the serial port you'll find a function that will tell you if there is a character to read.

AWOL and Njay,thank you ! Follow your reply, I have solved the problem,just add "if (Serial.available() > 0) " in loop,it is OK now