Go Down

Topic: play an audio file with tmrpcm using element access (Read 580 times) previous topic - next topic

matrixparks

Hi,
I'm trying to make a button input puzzle that plays a different sound whenever you press a button. I'm trying to tweak code by Playful Technology (he used a DF mini player and I'm just using an SD reader module).

it'll usually only play a short buzz, but when I set it so that it plays the same audio clip regardless of which button is pressed it works fine so I don't think it's a problem with the wiring or audio files.

I'm pretty sure the problem is that I'm not specifying that the audio file is a .wav but I don't know how to include a file extension with [i+1].

sorry in advance if there's a very simple solution and I was just too dumb to realise it


Code: [Select]


#define DEBUG


// INCLUDES

#include <pcmConfig.h>
#include <pcmRF.h>
#include <TMRpcm.h>
#include <SPI.h>
#include <SD.h>
#define SD_ChipSelectPin 7


// CONSTANTS
const byte numInputs = 4;
const byte inputPins[numInputs] = {3, 4, 5, 6};
const byte numSteps = 3;
const byte steps[numSteps] = {0, 3, 0}; // i.e. press button #2 once, then button #3 twice, then button #0, then button #1.
const byte lockPin = A0;

// GLOBALS

TMRpcm tmrpcm;

// Assume the default state of each switch is HIGH.
bool lastInputState[] = {HIGH, HIGH, HIGH, HIGH};
// What step of the sequence is the player currently on?
int currentStep = 0;


// The last time the input switch was toggled
unsigned long lastDebounceTime = 0;
// The amount of time (in ms) to wait before reading again
unsigned long debounceDelay = 1000;




void setup() {
  #ifdef DEBUG
    // Initialise serial communications channel with the PC
    Serial.begin(9600);
    delay(500);
  #endif
 

tmrpcm.speakerPin=9;
if(!SD.begin(SD_ChipSelectPin)){
  Serial.println("SD FAILED");
  return;
}
tmrpcm.setVolume(5);


  // Initialise the input pins that have switches attached
  for(int i=0; i< numInputs; i++){
    pinMode(inputPins[i], INPUT_PULLUP);
  }
 


  // Set the lock pin as output and secure the lock
  pinMode(lockPin, OUTPUT);
  digitalWrite(lockPin, HIGH);

 #ifdef DEBUG
    Serial.println(F("Lock secured"));
  #endif
 
  // Debug
  #ifdef DEBUG
    Serial.println("Setup Complete");
  #endif
}





void loop() {

  // Check that we've waited at least "debounceDelay" since last input
  if ( (millis() - lastDebounceTime) > debounceDelay) {
 
    // Loop through all the inputs
    for(int i=0; i<numInputs; i++){
      int currentInputState = digitalRead(inputPins[i]);

      // If the input has changed, reset the debounce timer
      if(currentInputState != lastInputState[i]) {
        lastDebounceTime = millis();   
      }
       

      if(currentInputState == LOW && lastInputState[i] == HIGH) {
 
tmrpcm.play(i+1);


     
        // Was this the correct input for this step of the sequence?
        if(steps[currentStep] == i) {
          currentStep++;
         
          #ifdef DEBUG
            Serial.print(F("Correct input! Onto step #"));
            Serial.println(currentStep);
          #endif
        }
        // Incorrect input!
        else {
          currentStep = 0;
          Serial.println(F("Incorrect input! Back to the beginning!"));
        }
      }
     
      // Update the stored value for this input
      lastInputState[i] = currentInputState;
    }
  }


 
  // Check whether the puzzle has been solved
  if(currentStep == numSteps){
    onSolve();
  }

}

// Takes action when the puzzle becomes solved
void onSolve(){

  #ifdef DEBUG
    // Print a message
    Serial.println(F("Puzzle Solved!"));
  #endif

  // Release the lock
  digitalWrite(lockPin, LOW);

  // Loop forever
  while(true){

  }
}

Go Up