Confused about accessing accelerometer sensor values in loop()

SO I have an ESP32 and BLA423 sensor. I can run this simple code in the loop and continuously access the sensor value in and display it in the serial terminal.

int a =0;

void loop()
{

        sensor->getAccel(acc); 
        
        if( a<5000 && sensor->getAccel(acc) == true)
          {
                       
              snprintf(report, sizeof(report),"%d,%d,%d",acc.x, acc.y, acc.z);
              Serial.println(report);
              //SerialBT.println(report);
              a++;
          }
  
    }

But I want to start displaying the accelerometer value when I send “Start” from serial and stop displaying when I send “Stop”.

However, if I create something like this in the loop function

sensor->getAccel(acc); 
if (SerialBT.available())
  {
    char incomingChar = SerialBT.read();
    
    if (incomingChar == '1')
    {
       if( a<5000 && sensor->getAccel(acc) == true)
          {
                       
              snprintf(report, sizeof(report),"%d,%d,%d",acc.x, acc.y, acc.z);
              Serial.println(report);
              //SerialBT.println(report);
              a++;
          }    
  
    }
  }

I need to enter the Start every loop.

Also if I create some function

void loopThis()
{
  int a =0;
  bool y = true;
  sensor->getAccel(acc); 
        
        for (int a = 0; a <100; a++)
                       {
              snprintf(report, sizeof(report),"%d,%d,%d",acc.x, acc.y, acc.z);
              Serial.println(report);
              SerialBT.println(report);
              a++;
              //delay(1000);
          }
          //Serial.println(a);
}

and run this function in loop()
it will only display a single value in a loop.

TLDR: I want to start displaying the accelerometer value when I send “Start” from the Serial Monitor and keep on displaying until I send “Stop” to the ESP32 from the Serial Monitor.

when you read start or stop you set/reset a boolean which is used to read the sensor, e.g.

void setup() {
   Serial.begin(115200);
}

void loop() {
  static bool readSensor=false;
  char ch;
  if(Serial.available())
     if((ch=Serial.read())=='1')  // start reading sensor?
        readSensor=true;
     else
     if(ch=='2')                  // stop reading?
        readSensor=false;
   if(readSensor) {
      Serial.println("readsensor");
      delay(1000);
      }
}

on entering 1 *readsenso*r is displayed once per second enter 2 to stop it etc etc

Cheers. You saved my ass. It works. And I understood where I was confused. :) Thanks