Looping audio with PCM.h problem

Hi there, I'm trying to mod a sonic screwdriver to do other cool bits but I'm stuck trying to get an arduino uno to loop audio using the PCM library I found here: http://highlowtech.org/?p=1963

Currently the speaker makes the noise repetitively and stops as soon as the button is released, but in between each of the sample loops there's a brief period where nothing is playing. I know it's not the sample because cutting off the audio at different points has the same result. I know it's possible using this library because I found a video of someone using this (who didn't link the code and has been inactive for 3 years, here's the video though: Link). Although I'm not sure if this is a coding error or the speaker turning off and then instantly on again (I have tried with a larger speakers and had the same result) but I've put the code below so if there's any obvious issue, I'd be very grateful for any suggestion. Thanks :slight_smile:

Heres a video of what I got: - YouTube

const int input = 2;
int buttonState = 0;
int timer = 0;

const unsigned char sample[] PROGMEM = { LOTS AND LOTS OF NUMBERS HERE };

void setup()
{
pinMode(input,INPUT);
}

void loop()
{
buttonState = digitalRead(input);

while (buttonState == HIGH) {

buttonState = digitalRead(input);

if (timer == 0) {
startPlayback(sample, sizeof(sample));
}

timer++;
delay(1);

if (timer == 200) {
timer = 0;
startPlayback(sample, sizeof(sample));
}
}
stopPlayback();
timer = 0;
}

What happens when you change the '200' to other values?
Does your button pin have the required pull-down resistor?

FYI: The 'playing()' function below should be able to tell you when playback is done

#include <PCM.h>


const int ButtonPin = 2;


const unsigned char samples[] PROGMEM = {   /*  LOTS AND LOTS OF NUMBERS HERE  */  };


void setup()
{
  pinMode(ButtonPin, INPUT);  // Requires pull-down resistor
}


boolean playing(void)
{
  return TIMSK1 & _BV(OCIE1A);
}


void loop()
{
  while (digitalRead(ButtonPin) == HIGH)
  {
    startPlayback(samples, sizeof(samples));
    while (playing()) ;  // Wait until playback is complete
  }
}

Thank you very much for replying, changing how long the playback is doesn't seem to effect whether or not there's a gap in between the samples (Here's a video showing that off: link, bit hard to hear the speaker sorry). Additionally the button definitely has a pull down resistor. I didn't realise that you could check whether the sample is still playing back so that's improved it one way or another, although it still didn't seem to work unfortunately (here's another video for that: link, still with hard to hear sonic).

Thanks again!