Record while checking for condition

Hi,

I'm using SpikenzieLab's VoiceShield to record and play audio. What I am trying to do is trigger a recording whenever the input level of my mic exceeds a certain threshold, and then keep recording until the input level goes below the threshold:

 if (micLeve l> 400 && rec == 0) {
  vs.ISDRECORD(1); // record to slot number 1
  Serial.println("Recording");
  rec = 1;
    }
  
  else if (micLevel < 400)  {
   delay(200);
    vs.ISDPLAY_to_EOM(1); // play audio from slot number 1
   Serial.println("Playing");
   rec = 1;
    }

As you can see, what's happening at the moment is that whenever the mic level exceeds 400, a recording will be triggered. However, as long the condition is true (i.e. mic level is > 400) it would continuously loop through, re-trigger the recording and thereby interrupt the continuity of the recording. What I am actually looking to achieve is that it will start recording whenever mic level is above threshold, and then continue recording uninterruptedly until the condition is false (i.e. mic level < 400).

How should this be done?

check the scope of your variable "rec".