Microphone and randNumber Difficulty

Hello All. This is my first post here, though I’ve benefitted from these forums since getting started with Arduino (Uno R3) a few months ago. I’ll do my best to adhere to the posting best-practices.

In the sketch that’s giving me fits, I’m trying to accomplish three goals: 1. Generate a random number between 5 and 15 (those figures are arbitrary); 2. Use that random number to define the number of seconds before the next random number is generated; and, 3. Constantly monitor the input from an electret microphone (and amp) and sound a buzzer when that input exceeds a specified threshold. All of this is related to a device I’ve made to help my dog with his very-bad separation anxiety–the timer is an approximation of a variable-ratio reinforcement schedule.

I have no problem with either the random number-delay or the mic -buzzer parts of the code when they’re separated into two sketches. When combined, however, the random number component works fine, but the mic only seems to take a reading at the same moment that the delay defined by the random number has elapsed. I.e., the mic’s input is not constant, like it’d need to be for this to work.

What am I doing wrong? Any help would be very much appreciated.

/* In this sketch I'm trying to accomplish three goals: 1. Generate a random number between 5 and 15; 2. Use the random number to 
define the number of seconds before the next random number is generated; and, 3. Constantly monitor the input from an electret microphone (and amp)
and sound a buzzer when that input exceeds a specified threshold.  */

int pos = 0;
int counter = 0;

int Microphone = 0;
int BuzzerPin = 3;
int led = 13; // This LED blinks so that I can gauge the delay.

void setup()

{
    pinMode(BuzzerPin, OUTPUT);
    pinMode (led, OUTPUT);
    randomSeed(analogRead(0)); 
    Serial.begin(9600);
    
}

void loop() {
  

for (counter=0; counter <= 29; counter++) {
  
    long randNumber = random(5000, 15001);
    digitalWrite(led, HIGH);   
    delay(500);               
    digitalWrite(led, LOW);   
    delay(500);    
    delay(randNumber); 

  
    Microphone = analogRead(A5);
    Serial.println(Microphone); 
    delay(50);

    if (Microphone > 700)
{
    tone(BuzzerPin, 5000);
    delay(100);
}
    else 
{
 
    noTone(BuzzerPin);

}

}

}

I'll do my best to adhere to the posting best-practices.

OK. One of them is to use Tools + Auto Format before posting code. Other than that, you did fine.

but the mic only seems to take a reading at the same moment that the delay defined by the random number has elapsed. I.e., the mic's input is not constant, like it'd need to be for this to work.

Because that's exactly what you've told it to do. If you want to monitor the microphone "continuously", you need to read, understand, and embrace the blink without delay example. No delay()s!

I am beginning to see the (small, red, continuously blinking) light. Thanks to you both.