0

Im newbie. I just wanted to ask how can i put my tmrpcm.play into loop. Im trying to put it in a loop but the problem is it didn't play. I also try adding a method and call the method into the loop but it give me the same problem also. It will just only play in the void setup.

Here's my code.

#include "SD.h"
#define SD_ChipSelectPin 4
#include "TMRpcm.h"
#include "SPI.h"

TMRpcm tmrpcm;

void setup(){
tmrpcm.speakerPin = 9;
Serial.begin(9600);
if (!SD.begin(SD_ChipSelectPin)) {
Serial.println("SD fail");
return;
}

tmrpcm.setVolume(5);
tmrpcm.play("Yeah.wav");
}

void loop(){  }
1

The library has a built in loop function:

audio.loop(1);             0 or 1. Can be changed during playback 
                           for full control of looping.

See WIKI page of the library.

Btw, it is unclear if you mean by 'a loop' a while(true) loop or the void loop() function. Although it should not make a difference.

0

The TMRpcm library is non-blocking. That means, when you start playing it keeps playing in the background. If you put the play function in a loop it will just keep restarting what you are playing over and over again and you won't get to hear anything.

As Michel says, you can get it to loop automatically (tmrpcm.loop(1);), in which case you just leave it in setup() and leave loop() empty.

If you want to do it manually you should check to see if the sound has finished playing or not. The simplest way is:

loop() {
    if (!tmrpcm.isPlaying()) {
        tmrpcm.play("Yeah.wav");
    }
}

That is - if it's not playing then start it playing. When it finishes it'll not be playing any more, so it will start playing again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.