MKR Zero fails on 28th loop

The program below loops repeating the same audio file at timed intervals. It's designed to ID a ham radio repeater every 9.5 minutes. Program always hangs up on the 28th loop. Any ideas?

  Key input on pin 2
  Time input on pin 3 
  Key relay out on pin 4
  Audio relay out on pin 5


#include <SD.h>
#include <SPI.h>
#include <AudioZero.h>
boolean debounce1=true;
boolean debounce2=true;
  int delay_time = 10000;  // default 10 seconds
  int timer1 = 10000;  // 10 sec time delay
  int timer2 = 570000; // 9.5 min time delay
  int lcount = 1;     // loop counter - for trouble shooting loop

void setup() {

  // debug output at 1200 baud
  delay(2000); // 2 sec. time delay 
  // Initialize SD card
  // Serial.print("Initializing SD card..."); //SDCARD_SS_PIN
  if (!SD.begin(28)) {
    Serial.println(" failed!");
    while(true); // infinite loop if fail
  pinMode(2, INPUT_PULLUP); //Button 1 with internal pull up to sense key
  pinMode(3, INPUT_PULLUP); //Button 2 with internal pull up time select switch
  pinMode(4, OUTPUT);       // Transmit relay out.
  pinMode(5, OUTPUT);       // Audio relay out.
//  Serial.println(" done.");
void loop()
AudioZero.begin(88200);  //set sample rate
  // open wav file from sdcard
  File myFile ="test.wav", FILE_READ);

  if (!myFile) {
    // if the file didn't open, print an error and stop
    Serial.println("error opening test.wav");
    while (true);
  if (digitalRead(2)==HIGH  && debounce1 == true)     //low when keyed. Wait for high.
  Serial.println("keying XMTR");
  pinMode(4, HIGH); //Key transmitter
//  delay(200); // 200 ms delay
  pinMode(5, HIGH); //Close audio relay  
   // until the file is not finished;
  pinMode(5, LOW); // Open audio relay
  pinMode(4, LOW); // Unkey transmitter
  Serial.println("End of file. Thank you for listening!");
  lcount++; // to trouble shoot loop
  Serial.println(lcount);  // to trouble shoot loop
  if (digitalRead(3)==HIGH  && debounce2 == true) 
    delay_time = timer1;
    else delay_time = timer2;

Please edit your post, select all code and click the </> button above the reply window; next save your post. This will apply code tags to your code which will prevent the forum software from mangling your code, makes it easier to read and easier to copy.



so what do you see on serial monitor just before it hangs up?

But no sound.

My friend, I have bad news for you. Official AudioZero library is piece of crap, it is full of bugs and no one cares. I checked the github and well, buggy commits have been made, the system picked that up but no one gives a monkey. But the reason your wav stop playing is most likely due to memory leak. there is malloc but there is no free in the library. My advice, find a good 3rd party library written by someone who actually has a clue

Found a fix, dorky but it works. Added following to the end of the loop.

  if (lcount > 21){
    NVIC_SystemReset(); // FIX FOR LOOP PROBLEM


Ugly hack. Since you are using mkr zero and it has I2S, get an external DAC and use ArduinoSound library, much better written

This is really my second design. First was with an Arduino UNO with a SD card Module. Worked great. But space is somewhat limited so I was trying to get the size down. The MKR Zero is perfect form wise. But it has disappointed me. If I have to I'll go back to the UNO.

The obvious candidate would be the classic Nano; same processor as the Uno. Arduino Nano — Arduino Official Store

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.