Can I stop the animation but still change amount of tiles?

Can I, in this example of code, somehow stop the animation of the tiles changing but still be able to change the amount of tiles (with an Arduino pot)?
If yes, how?

Right now, when I stop the loop, the ability to change anything else disappears.

Thanks!

//GRID
   tilesX = serialInArray[0];
  tilesY = serialInArray[1];

  float tileW = width / tilesX;
  float tileH = height / tilesY;

  for (int x = 0; x<tilesX; x++) {
    for ( int y = 0; y<tilesY; y++) {
      int posX = int(tileW *x);
      int posY = int(tileH *y);


   
      //WAVE ROOD
      float wave = cos(radians(frameCount *x +y));
      float mappedWave = map(wave, -1, 1, 0, 16);
      int selector = int(mappedWave);

      //GRID ROOD
      pushMatrix();

     
      translate(posX, posY);
      if (selector == 0) {
        fill(#ff0000);
        rect(0, 0, tileW, tileH);
        //image(img, 200, 0, tileW, tileH);
      } else if (selector == 1) {
        fill(#ff0000);
        rect(0, 0, tileW, tileH);
        // image(img, width/2, height/2, tileW, tileH);
      } else if (selector == 2) {
        push();
        //noFill();
        rect(0, 0, tileW, tileH);
        pop();
      } else if (selector == 3) {
        fill(#ff0000);
        arc(0, 0, tileW*2, tileH*2, radians(0), radians(90));
      } else if (selector == 4) {
        fill(#ff0000);
        arc(tileW, 0, tileW*2, tileH*2, radians(90), radians(180));
      } else if (selector == 5) {
        fill(#ff0000);
        arc(tileW, tileH, tileW*2, tileH*2, radians(180), radians(270));
      } else if (selector == 6) {
        fill(#ff0000);
        arc(0, tileH, tileW*2, tileH*2, radians(270), radians(360));
      } else if (selector == 7) {
        fill(#ff0000);
        ellipse(tileW/2, tileH/2, tileW, tileH);
      } else if (selector == 8) {
        fill(#ff0000);
        triangle(0, tileH, 0, 0, tileW, tileH);
      } else if (selector == 9) {
        fill(#ff0000);
        triangle(0, tileH, tileW, 0, tileW, tileH);
      } else if (selector == 10) {
        fill(#ff0000);
        triangle(0, 0, tileW, 0, tileW, tileH);
      } else if (selector == 11) {
        fill(#ff0000);
        triangle(0, 0, tileW, 0, 0, tileH);
      } else if (selector == 12) {
        fill(#ff0000);
        arc(tileW/2, 0, tileW, tileH, radians(0), radians(180));
      } else if (selector == 13) {
        fill(#ff0000);
        arc(tileW/2, tileH, tileW, tileH, radians(180), radians(360));
      } else if (selector == 14) {
        fill(#ff0000);
        arc(tileW/2, tileH/2, tileW, tileH, radians(90), radians(270));
      } else if (selector == 15) {
        fill(#ff0000);
        arc(0, tileH/2, tileW, tileH, radians(270), radians(450));
      }
     popMatrix();

If your pot value works by polling the input, then you will have to leave Processing looping in order to keep testing the input. The alternative is to have an event callback such as how keyPressed() works and have it call redraw() while otherwise in noLoop().

For polling, put your drawing code in its own function and, from draw(), only call that function when your pot value changes. So, create a global variable that remembers the last pot value. draw() then checks if the current pot value is different and if so, remember the new value and redraw by calling your function.

Try making the float ‘wave’ a global variable, also create a global boolean for a paused state

boolean paused;
float wave;

then have in your for loops

if (paused == false){
  wave = cos(radians(frameCount * x + y));
}

then use a key press for the paused state.

void keyPressed(){
  if (key == ' '){
    paused = !paused;
  }
}

this is a simple way to have the spacebar be a pause button without interfering with all the other stuff, if you need it to pause other parts just apply the same if statement there, you could also use a mousePressed function instead of the keyPressed

hope that helps