How can I add a condition inside the case. I’d like to add a function that if the result is less the 15cm the car stop, back and turn left or right.
if (Serial.available())
{ val = Serial.read();
switch(val)
{
case 'a':
servoX.write(lx);
qian(); // forward
break;
case 'b':
hou(); //back
break;
@Barmar is correct, there is nothing that stops you form nesting logic in your switch statement, so it is fine to add an if condition inside one of you case blocks.
On a side note: if this is your first run at a wall avoiding car pay attention to what happens to your signal when something is too close to the sensor, as with most sensors you will start to get readings indicating you are moving away as you move closer than it’s operating range. For this reason you need to either try and be smarter than just setting a limit, or set you limit a bit higher than the minimum operating range of the sensor. The ultrasonic sensors are better than the cheap IR distance sensors for this, but you still need to be careful as you pick up the speed. I would also recommend with the quite sensitive ultrasonic sensors that you require a couple of consistent readings in a row before reacting to them.