Serial Command to Arduino activating Ping Sensors

Hello All, Good Day! :)

I am a bit new to Arduino.

What I want to do is activate a loop within the arduino code when a Serial command is activated. I want to control a DC motor with my PC keyboard, and I also want the motors to be controlled with Ping Sensors, but the ping sensor part would only be activated when a particular button lets say 'z' is pressed on my laptop keyboard.

Attached is the code I have so far. Note: I haven't added anything concerning the sensors as yet.

const int motor =8;
const int motor2 =9;
const int motor3 =10;

void setup() {
  pinMode(motor, OUTPUT);
  pinMode(motor2, OUTPUT);
  pinMode(motor3, OUTPUT);
  Serial.begin(9600);
  Serial.println("Motor Control");
}

void loop() {
  if (Serial.available() > 0) {
    int data = Serial.read();
    switch(data)
    {
        case 'a' :  digitalWrite(motor, HIGH); Serial.print("Vertical Push Down - ON");break ; 
    case 'b' :  digitalWrite(motor, LOW); Serial.print("Vertical Push Down - OFF"); break ;
        case 'c' :  digitalWrite(motor2, HIGH); Serial.print("Motor One - Activated"); break;
        case 'd' :  digitalWrite(motor2, LOW); Serial.print("Motor One - OFF"); break;
        case 'e' :  digitalWrite(motor3, HIGH); Serial.print("Motor Two - Activated"); break;
        case 'f' :  digitalWrite(motor3, LOW); Serial.print("Motor Two - OFF"); break; 
    }
  }
}

You need a state vaiable than can remember what state you are in currently based on events in the past.

#define SENSOROFF 0
#define SENSORON 1

int sensorState = SENSOROFF; 

void setup() {
 ...  // keep as is
}

void loop() 
{
    ...
    switch(data)
    {
        case 's' : sensorState = SENSORON;   break;
        case 'z' : sensorState = SENSOROFF;   break;
    }
    if (senssorState == SENSORON)
    {
        /Do something with your sensor here. when you send an 's' via serial, this part will continusly execute every time in the loop until you send a 'z'.
    }
}