Turn on and off sd card sound player randomly every 5 minutes

I am trying to randomly turn on and off my sd card player every 5 minutes. I don't know how to do that and that is why I am asking for guidance. I am trying to add it into the following code

/*
Sound for owl
*/
#include "SD.h"
#define SD_ChipSelectPin 10
#include "TMRpcm.h"
#include "SPI.h"

TMRpcm tmrpcm;

void setup()
{
tmrpcm.speakerPin=9;//speaker pin
Serial.begin(9600);
if(!SD.begin(SD_ChipSelectPin))
{
  Serial.println("SD fail");
  return;
}
tmrpcm.setVolume(5);//volume



 
}

void loop() {
 tmrpcm.play("per.wav");//name for peregrine falcon sound
  delay(2200);//plays for 22 seconds and repeats
  
}

These are not possible together. One is discrete, one is not.

Here is your code with three lines added to your code (and one removed) that will wait for five minutes before playing the file again... nothing random about it. Read the comments.

/*
  Sound for owl
*/
#include "SD.h"
#define SD_ChipSelectPin 10
#include "TMRpcm.h"
#include "SPI.h"

unsigned long timer, timeout = 5 * 1000UL; // ADD THIS LINE

TMRpcm tmrpcm;

void setup()
{
  tmrpcm.speakerPin = 9; //speaker pin
  Serial.begin(9600);
  if (!SD.begin(SD_ChipSelectPin))
  {
    Serial.println("SD fail");
    return;
  }
  tmrpcm.setVolume(5);//volume
}

void loop() {
  if (millis() - timer > timeout) { // if difference between "now" and "previous time check" is > timeout
    timer = millis(); // store a new "previous time check"
    tmrpcm.play("per.wav");//name for peregrine falcon sound
    // delay(2200);//plays for 22 seconds and repeats
  }
}

Read about millis() so you can make this program do what you want...

Actually that would wait for five seconds.

You need

timeout = 5 * 60 * 1000UL; // ADD THIS LINE

You could have both working together

void loop() {
  if (millis() - timer > timeout) {
    timer = millis();
    timeout = (4500ul + random(1000)) * 60; // chose time until next activity
    tmrpcm.play("per.wav");
    // delay(2200);
  }
}

Which would make the time to the next playing be random but between 4.5 and 5.5 minutes.

What was the 2.2 second delay ever for?

a7