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(){ }
asked Apr 16, 2018 at 13:26
0

2 Answers 2

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.

answered Apr 16, 2018 at 14:04
1

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.

answered Apr 16, 2018 at 14:49

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.