daily pastebin goal
55%
SHARE
TWEET

Arduino WAV/Seeed SD Card Shield

weaknetlabs Jul 30th, 2014 2,665 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  
  3. Simple Sound Player - coded by Douglas Berdeaux
  4. WeakNetLabs@Gmail.com
  5.  
  6. The WAV files need to be 32kHz (Maximum) and 8 bit. (You can convert mp3s and such using the custom settings module in iTunes or Audacity (both free)).
  7. Although the SD card is formatted as FAT, the FAT filesystem on the SD Card is accessed like xpath, or UNIX paths. For instance, I had a folder called "sounds" and in "sounds" I had the file "payphone.wav" which I called from the play() method as,
  8. tmrpcm.play("sounds/payphone.wav");
  9.  
  10. Also, A specific naming convention must be used according to the Arduino documentation, as old FAT filesystems did (rememebr the old Windows 95/98 file name tilde-truncation-mangling, like FILE00~1.EXT?): http://arduino.cc/en/Reference/SDCardNotes
  11. The TMRh20 library is used to play the WAV files, https://github.com/TMRh20/TMRpcm
  12. The Seeed SD Card Reader was used, (version 3.0): http://www.seeedstudio.com/wiki/SD_Card_Shield_V3.0
  13.  
  14. Caveats: I had to programmatically lower the volume, because of slight distortion. Also, I had popping and cracking sounds during playback, to which I had to uncomment the line,
  15. #define buffSize 128
  16. in the file, pcmConfig.h which helped dramatically with sound quality.  
  17.  
  18. */
  19.  
  20. #include <SD.h> // preprocessor directives (header files)
  21. #define SDPIN 10
  22. #include <TMRpcm.h>
  23.  
  24. File myFile; // create objects here from library classes
  25. TMRpcm tmrpcm;
  26.  
  27. char mychar; // for reading input from Serial console
  28.  
  29. void setup(){
  30.   Serial.begin(9600);
  31.   tmrpcm.speakerPin = 9; //11 on Mega, 9 on Uno, Nano, etc
  32.   tmrpcm.setVolume(4);
  33.   if (!SD.begin(SDPIN)) {
  34.     Serial.println("initialization failed!");
  35.     return;
  36.   }
  37.   Serial.println("Type 'a' to play a song.");
  38. }
  39.  
  40. void loop(){
  41.     if(Serial.available()){
  42.       mychar = Serial.read();
  43.       if(mychar == 'a'){ //send the letter a over the serial monitor to start playback
  44.       Serial.println("playing wav");
  45.         tmrpcm.play("sounds/payphone.wav");
  46.       }
  47.     }
  48. }
  49.  
  50. void playSound(char *file){
  51.  tmrpcm.play(file);
  52.  return;
  53. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top