-
1Step 1
Breadboarding the setup
-
2Step 2
Uploading the program - this is the final version that plays all sounds then restarts
Library files are from here https://github.com/TMRh20/TMRpcm/wiki
Note WAV files are 8bit and should be less than 20K/sec samples - I used Audacity Portable
This is modified code originally from APC mag in Australia - the article goes into great detail
Thanks Darren Yates http://apcmag.com/arduino-project-5-digital-audio-...
// ---------------------------------------------------------------------------------
// 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 = 10;
// ---------------------------------------------------------------------------------
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;
int trigPin = 4;
int echoPin = 5;
void setup() {
pinMode(trigPin, OUTPUT); //for HC-SR04
pinMode(echoPin, INPUT); //for HC-SR04
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 = 9; //Pin9 for audio output
pinMode(inSwitch,INPUT_PULLUP); //Audio Test switch
// digitalWrite(inSwitch,HIGH);
root = SD.open("/");
}
void loop(void) {
long duration, distance; //
digitalWrite(trigPin, LOW); // HC-SR04 setup
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(20);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH); //Get ping time
Serial.println(duration);
boolean btnState = digitalRead(inSwitch); //Check Audio Test switch
if(btnState == LOW){ //manual trigger on pin 7 - bring low
playNext();
delay(100);
}
if ((duration < 8000) && (duration > 3000)) { // Trigger set for 40cm - 120cm
playNext();
delay(5000); //Delay between each sound file
}
delay(1000); //Check ping sensor every second
}
void playNext() { //play next track function
entry = root.openNextFile();
Serial.println(entry);
if (entry) {
entry.close();
tmrpcm.play(entry.name());
wasPlaying = 1;
} else {
if (wasPlaying == 1) {
wasPlaying = 0;
root.rewindDirectory();
entry = root.openNextFile();
Serial.println(entry);
entry.close();
tmrpcm.play(entry.name());
}
}
}
Discussions
Become a Hackaday.io Member
Create an account to leave a comment. Already have an account? Log In.