Arduino wav file play in loop?

Hi all :slight_smile:
I making an airplane engine sound emulator for my RC plane , and i make a simple wav player on arduino using TMRpcm lib, and SD card. Im using this code for playing a file:

// ---------------------------------------------------------------------------------
// DO NOT USE CLASS-10 CARDS on this project - they're too fast to operate using SPI
// ---------------------------------------------------------------------------------

#include <SD.h>
#include <TMRpcm.h> 
TMRpcm tmrpcm; 

File root;
File entry;

// ---------------------------------------------------------------------------------
// set chipSelect to '10' if using the $2 SD card module or '4' if using the  
// Ethernet shield's microSD card instead.
const int chipSelect = 12;    
// ---------------------------------------------------------------------------------

const int oldCard = SPI_HALF_SPEED;
const int newCard = SPI_QUARTER_SPEED;

// ---------------------------------------------------------------------------------
// set cardType to 'oldCard' if using an old SD card (more than a few years old) or
// to 'newCard' if using a newly-purchase Class-4 card.
int cardType = oldCard;
// ---------------------------------------------------------------------------------

int wasPlaying = 0;
int inSwitch = 7;
int finished = 0;
int start = 0;
int pauseOn = 0;
unsigned long timeDiff = 0;
unsigned long timePress = 0;

void setup() {
  Serial.begin(9600);
  Serial.print("\nInitializing SD card...");
  pinMode(chipSelect, OUTPUT); 
  if (!SD.begin(chipSelect,cardType)) {
    Serial.println("failed!");
    return;
  }
  Serial.println("done.");

  tmrpcm.speakerPin = 13;

  pinMode(inSwitch,INPUT_PULLUP);
  digitalWrite(inSwitch,HIGH);

  root = SD.open("/");
}

void loop(void) {
  if(!tmrpcm.isPlaying() && wasPlaying == 1) { 
    tmrpcm.stopPlayback();
    playNext();  
  }

  if (millis() - timeDiff > 50) { // check switch every 100ms 
    timeDiff = millis(); // get current millisecond count

    if(digitalRead(inSwitch) == LOW) {

      if(start==0) {
        start=1; 
        playNext();
        delay(200);

      } 
      else {

        timePress = millis();
        while(digitalRead(inSwitch)==LOW) {
          delay(50);
        }
        if (millis() - timePress < 1000 && start == 1) {
          tmrpcm.pause();
          if (pauseOn == 0) {
            pauseOn = 1;
          } 
          else {
            pauseOn = 0;
          }
        }
        if (millis() - timePress > 1000 && start == 1) {
          if (pauseOn == 1) {
            pauseOn = 0; 
            tmrpcm.pause();
          }
          tmrpcm.stopPlayback();
          timePress = millis();
          if (finished == 0) {
            playNext(); 
          } 
          else {
            finished = 0;
            Serial.println("Restarting."); 
            root.rewindDirectory();
            playNext(); 
          }
        }
      }
    }
  }
}

void playNext() {
  entry = root.openNextFile();
  if (entry) {
    entry.close();
    tmrpcm.play(entry.name()); 
    wasPlaying = 1;
  } 
  else {
    if (wasPlaying == 1) {
      Serial.println("Completed playback."); 
      wasPlaying = 0;
      finished = 1;
      start = 0;
      root.rewindDirectory();
    }     
  }
}

So this is only a player code i need to implement an code that will read PWM throttle signal from RC receiver and regarding to RPM its should change wav file with sound equal to RPM, but this is not important now.
In this code it play a wave and when it ends its jumping to next . I want to modify that code so it will play in loop wav i chose?

This code here should work for you. Don't forget to change the file name to your particular engine noise file, and modify the SD.begin() to your CS pin number for your SD card module.

It just plays the .WAV file in a continuous loop. That's it.

#include <SD.h>
#include <TMRpcm.h> 
TMRpcm tmrpcm;

int wasPlaying = 0;
int finished = 0;
int start = 0;

void setup() {
  Serial.begin(9600);
  Serial.print("\nInitializing SD card...");
  pinMode(10, OUTPUT); 
  if (!SD.begin(10)) {                                   // set this to your card type and cs pin
    Serial.println("failed!");
    return;
  }
  Serial.println("done.");
  tmrpcm.speakerPin = 9;
}

void loop() {
  start = 1;
  playNext();
  if(tmrpcm.isPlaying())
    while(tmrpcm.isPlaying());
  tmrpcm.stopPlayback();
}

void playNext() {
  if(wasPlaying == 1) {
    Serial.println("Completed playback.");
    wasPlaying = 0;
    finished = 1;
    start = 0;
  }
  tmrpcm.play("your file name goes here.WAV");           // put your engine file name here
  wasPlaying = 1;  
}

Judd

Hi everybody,

I got the same issue with an BareConductive Touch Board. When "pin" is touched it plays a song then stops. I would like to get the pin which is touched to play in loop. Do you think it is possible ? Thanks a lot

*******************************************************************************/

// compiler error handling
#include "Compiler_Errors.h"

// touch includes
#include <MPR121.h>
#include <Wire.h>
#define MPR121_ADDR 0x5C
#define MPR121_INT 4

// mp3 includes
#include <SPI.h>
#include <SdFat.h>
#include <FreeStack.h> 
#include <SFEMP3Shield.h>

// mp3 variables
SFEMP3Shield MP3player;
byte result;
int lastPlayed = 0;

// mp3 behaviour defines
#define REPLAY_MODE TRUE  // By default, touching an electrode repeatedly will 
                          // play the track again from the start each time.
                          //
                          // If you set this to FALSE, repeatedly touching an 
                          // electrode will stop the track if it is already 
                          // playing, or play it from the start if it is not.

// touch behaviour definitions
#define firstPin 0
#define lastPin 11

// sd card instantiation
SdFat sd;

void setup(){  
  Serial.begin(57600);
  
  pinMode(LED_BUILTIN, OUTPUT);
   
  //while (!Serial) ; {} //uncomment when using the serial monitor 
  Serial.println("Bare Conductive Touch MP3 player");

  if(!sd.begin(SD_SEL, SPI_HALF_SPEED)) sd.initErrorHalt();

  if(!MPR121.begin(MPR121_ADDR)) Serial.println("error setting up MPR121");
  MPR121.setInterruptPin(MPR121_INT);

  MPR121.setTouchThreshold(40);
  MPR121.setReleaseThreshold(20);

  result = MP3player.begin();
  MP3player.setVolume(10,10);
 
  if(result != 0) {
    Serial.print("Error code: ");
    Serial.print(result);
    Serial.println(" when trying to start MP3 player");
   }
   
}

void loop(){
  readTouchInputs();
}


void readTouchInputs(){
  if(MPR121.touchStatusChanged()){
    
    MPR121.updateTouchData();

    // only make an action if we have one or fewer pins touched
    // ignore multiple touches
    
    if(MPR121.getNumTouches()<=1){
      for (int i=0; i < 12; i++){  // Check which electrodes were pressed
        if(MPR121.isNewTouch(i)){
        
            //pin i was just touched
            Serial.print("pin ");
            Serial.print(i);
            Serial.println(" was just touched");
            digitalWrite(LED_BUILTIN, HIGH);
            
            if(i<=lastPin && i>=firstPin){
              if(MP3player.isPlaying()){
                if(lastPlayed==i && !REPLAY_MODE){
                  // if we're already playing the requested track, stop it
                  // (but only if we're not in REPLAY_MODE)
                  MP3player.stopTrack();
                  Serial.print("stopping track ");
                  Serial.println(i-firstPin);
                } else {
                  // if we're already playing a different track (or we're in
                  // REPLAY_MODE), stop and play the newly requested one
                  MP3player.stopTrack();
                  MP3player.playTrack(i-firstPin);
                  Serial.print("playing track ");
                  Serial.println(i-firstPin);
                  
                  // don't forget to update lastPlayed - without it we don't
                  // have a history
                  lastPlayed = i;
                }
              } else {
                // if we're playing nothing, play the requested track 
                // and update lastplayed
                MP3player.playTrack(i-firstPin);
                Serial.print("playing track ");
                Serial.println(i-firstPin);
                lastPlayed = i;
              }
            }     
        }else{
          if(MPR121.isNewRelease(i)){
            Serial.print("pin ");
            Serial.print(i);
            Serial.println(" is no longer being touched");
            digitalWrite(LED_BUILTIN, LOW);
         } 
        }
      }
    }
  }
}