How to find out when the code always reads the input data, but it has to do again when the data satisfies a condition

Hi, I am sending Data from APP. in the loop the first stuff that is done is the reading of Data.

If data is 1 then the servo will move continuously based on the threshold of the imu.
If data is 2 then the servo will be turned off.
If data is 3 then I would like to let Arduino understand that it has to expect, after “3”, a string with the format #,#,#.Those 3 numbers(#,#,#) are respectively P,I,D.
Indeed the servo is Dynamixel: my desire is that pushing “1” (1 is sent to serial) the imu starts to read the acceleration and move the dynamixel basing on the thresholds; pushing “2” the imu and the dynamixel will be turned off; pushing three there is the possibility to change PID values (this could be or has to be the first step before the sending of “1”.
I created 2 codes, 1 to let arduino turning on and off the dynamixel (situation of reading 1 and 2), and another one that receives the format#,#,#
and knows that the first is P, the second is I and the last is D and they are set in the mechanical behaviour.
The problem is that I am not able to unify both of them.
Can you help me with the structure of the code, please?

#include <SoftwareSerial.h>
SoftwareSerial BTSerial(7,8);
#include <Arduino_LSM6DS3.h>
#include <DynamixelShield.h>

const uint8_t DXL_ID = 1;
const float DXL_PROTOCOL_VERSION = 2.0;

DynamixelShield dxl;
//This namespace is required to use Control table item names
using namespace ControlTableItem;
#define DEBUG_SERIAL Serial

char caracter;
String palabra;

String P;
String I;
String D;
int ind1;
int ind2;
int ind3;   
int PID_state=0;
int walk_state=0;
int read_acc=0;
float Object_position_X = 170; 
float Object_position_Y = 60;  

float Athr =  0.22;
float Bthr =  -0.1; 

void setup(){
  DEBUG_SERIAL.begin(57600);
   delay(2000);
  BTSerial.begin(57600);
  delay(2000);
    IMU.begin();
    delay(2000);
  

  
  dxl.begin(57600);
  // Set Port Protocol Version. This has to match with DYNAMIXEL protocol version.
  dxl.setPortProtocolVersion(DXL_PROTOCOL_VERSION);

  // Turn off torque when configuring items in EEPROM area
  dxl.torqueOff(DXL_ID);
  dxl.setOperatingMode(DXL_ID, OP_POSITION);
//  dxl.writeControlTableItem(CURRENT_LIMIT, DXL_ID, 15);
//  dxl.writeControlTableItem(POSITION_P_GAIN, DXL_ID, 200);
//  dxl.writeControlTableItem(POSITION_I_GAIN, DXL_ID, 4);
//  dxl.writeControlTableItem(POSITION_D_GAIN, DXL_ID, 25);
  dxl.torqueOn(DXL_ID);
  DEBUG_SERIAL.print("Connect the device via bluetooth") ; 
}


String Data;


void loop(){
  if (BTSerial.available()>0){
    Data=BTSerial.readString();
   }



   ///WALK///

   
if(walk_state==0 && Data == "1"){
  DEBUG_SERIAL.println(Data);
           DEBUG_SERIAL.println("FUNZIONE WALKING ABILITATA");
           PID_state=0;
           walk_state=1;
           read_acc=1;
      
              }
                    if(read_acc==1){
                         float x,y,z;
                
                    IMU.readAcceleration(x, y, z);
                    DEBUG_SERIAL.print(y);
                    DEBUG_SERIAL.print("\t");
                              if (y>Athr){
                              dxl.setGoalPosition(DXL_ID, 170,UNIT_DEGREE);
                                DEBUG_SERIAL.println("stance");
                              }
                              else if(y<Bthr){
                                dxl.setGoalPosition(DXL_ID, 60,UNIT_DEGREE);
                                DEBUG_SERIAL.println("swing");
                              }
                              else {
                              DEBUG_SERIAL.println();
                              }
                              }
if(walk_state==1 && Data == "2"){
   DEBUG_SERIAL.println(Data);
    DEBUG_SERIAL.println("FUNZIONE WALKING DISABILITATA");
    walk_state=0;
    read_acc=0;
    delay(1000);
    dxl.setGoalPosition(DXL_ID, 90,UNIT_DEGREE);
    
            
   
}

if(Data == "3" ){
    DEBUG_SERIAL.println(Data);
    DEBUG_SERIAL.println("define PID");
    PID_state=1;
    pidvalues();
}
    

}
void pidvalues() {
                 caracter = BTSerial.read();
                 palabra = palabra + caracter;
                 DEBUG_SERIAL.println(caracter);
                 DEBUG_SERIAL.println(palabra);
                 if(caracter == '*') {    
                 palabra = palabra.substring(0, palabra.length() - 1); // Delete last char *
                  Serial.println(palabra);
                  ind1 = palabra.indexOf(',');
                  P = palabra.substring(0, ind1);
                  ind2 = palabra.indexOf(',', ind1+1 );
                  I = palabra.substring(ind1+1, ind2);
                  ind3 = palabra.indexOf(',', ind2+1 );
                  D = palabra.substring(ind2+1);
          
                  Serial.print("P = ");
                  Serial.println(P);
                  Serial.print("I = ");
                  Serial.println(I);
                  Serial.print("D = ");
                  Serial.println(D);
                  Serial.println();
                  palabra = "";
                  delay(10);
             }
            
}