This is my first post, so please bear with me.
I'm using an Uno and trying to get it to play one of ten wav files contained in one of four folders based upon the number of presses of a momentary button. I researched and found what I thought was the right combination of code, but things just aren't working.
Files are in folders on a wav player board, connected to the Uno. The audio output is connected to a small amp. Small momentary button connected to the Uno.
Any help? I'm happy to add any additional info needed.
Thanks!
#include <SD.h> // need to include the SD library
#define SD_ChipSelectPin 10 //connect pin 10 of arduino to cs pin of sd card
#include <TMRpcm.h> //Arduino library for asynchronous playback of PCM/WAV files
#include <SPI.h> // need to include the SPI library
long randNumber;
byte switchPin = 2; // switch is connected to pin 2
byte buttonPresses = 0; // how many times the button has been pressed
byte lastPressCount = 0; // to keep track of last press count
TMRpcm tmrpcm;
String fold;
void setup()
{
// pinMode(pp,INPUT_PULLUP);
// pinMode(next,INPUT_PULLUP);
// pinMode(prev,INPUT_PULLUP);
pinMode(switchPin, INPUT); // Set the switch pin as input
digitalWrite(switchPin, HIGH); // set pullup resistor
tmrpcm.speakerPin = 9; //5,6,11 or 46 on Mega, 9 on Uno, Nano, etc
Serial.begin(9600);
if (!SD.begin(SD_ChipSelectPin)) // returns 1 if the card is present
{
Serial.println("SD fail");
return;
}
tmrpcm.setVolume(7);
tmrpcm.play("startup.wav"); //the sound file "startup" will play each time the arduino powers up, or is reset
}
void loop(){
if (digitalRead(switchPin) == LOW) // check if button was pressed
{
buttonPresses++; // increment buttonPresses count
delay(250); // debounce switch
}
if (buttonPresses == 5) buttonPresses = 0; // rollover every fifth press
if (lastPressCount != buttonPresses) // only do output if the count has changed
{
Serial.print ("Button press count = "); // out to serial
Serial.println(buttonPresses, DEC);
}
if (buttonPresses == 1)
{
fold = "Short";
}
if (buttonPresses == 2)
{
fold = "Medium";
}
if (buttonPresses == 3)
{
fold = "Long";
}
if (buttonPresses == 4)
{
fold = "Wet";
}
randNumber = random(1, 10);
String myString1 = "/";
String myString2 = ".WAV";
String myFile = myString1 + fold + myString1 + randNumber + myString2;
Serial.println(myFile);
tmrpcm.play(myFile.c_str());
lastPressCount = 0; // track last press count
}
