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.
the files in the SD card are numbered 1-5.wav (as apposed to 0001 etc, but i've tried using 0001.wav to similar results).
any advice would be greatly appreciated
// DEFINES
// Provides debugging information over serial connection
#define DEBUG
// INCLUDES
#include <pcmConfig.h>
#include <pcmRF.h>
#include <TMRpcm.h>
#include <SPI.h>
#include <SD.h>
#define SD_ChipSelectPin 10
// CONSTANTS
// Define the number of possible "inputs" - i.e. the number of switches, buttons etc. that the player can press
const byte numInputs = 4;
// What pins are those buttons connected to? (other wire should go to ground)
const byte inputPins[numInputs] = {2, 3, 4, 5};
// Define the number of steps in the sequence that the player must follow
const byte numSteps = 3;
// The correct sequence of inputs required to solve the puzzle.
const byte steps[numSteps] = {0, 3, 0}; // i.e. press button #2 once, then button #3 twice, then button #0, then button #1.
// This pin will be driven LOW to release a lock when puzzle is solved
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;
// Switches can "bounce" when they open/close, generating a flurry of false readings
// To prevent this, we'll add a short delay between each time an input value
// is read.
// 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;
char fileName[5];
int fileToPlay ;
// Setup function runs once when first starting (or resetting) the board
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);
}
// Initialise the LED pins that show progress through the sequence
// for(int i=0; i< numSteps; i++){
// pinMode(ledPins[i], OUTPUT);
// }
// Set the lock pin as output and secure the lock
pinMode(lockPin, OUTPUT);
digitalWrite(lockPin, HIGH);
#ifdef DEBUG
Serial.println(F("Lock secured"));
#endif
fileToPlay=0;
sprintf(fileName, "%d.wav", fileToPlay);
// Debug
#ifdef DEBUG
Serial.println("Setup Complete");
#endif
}
// The main program loop runs continuously
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 the input is currently being pressed (and wasn't before)
// Note that since the input pins are configured as INPUT_PULLUP,
// they read as LOW when pressed and HIGH when not.
if(currentInputState == LOW && lastInputState[i] == HIGH) {
fileToPlay=inputPins[i++];
tmrpcm.play("fileToPlay");
// 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){
}
}