Advertisement
Guest User

Untitled

a guest
May 26th, 2016
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.55 KB | None | 0 0
  1. /*
  2. * WaveShieldPlaySelection sketch
  3. *
  4. * play a selected WAV file
  5. *
  6. * Position of variable resistor slider when button pressed selects file to play
  7. *
  8. */
  9.  
  10. #include <FatReader.h>
  11. #include <SdReader.h>
  12.  
  13. #include "WaveHC.h"
  14. #include "WaveUtil.h"
  15.  
  16. SdReader card; // This object holds the information for the card
  17. FatVolume vol; // This holds the information for the partition on the card
  18. FatReader root; // This holds the information for the volumes root directory
  19. FatReader file; // This object represents the WAV file
  20. WaveHC wave; // Only wave (audio) object - only one file played at a time
  21.  
  22.  
  23. const int buttonPin = 15;
  24. const int potPin = 0; // analog input pin 0
  25.  
  26. char * wavFiles[] = {
  27. "1.wav","2.WAV","3.WAV","4.WAV","5.WAV","6.WAV","7.WAV","8.WAV","9.WAV"};
  28.  
  29. void setup()
  30. {
  31. Serial.begin(9600);
  32. pinMode(buttonPin, INPUT);
  33. digitalWrite(buttonPin, HIGH); // turn on pull-up resistor
  34.  
  35. if (!card.init())
  36. {
  37. // Something went wrong, sdErrorCheck prints an error number
  38. putstring_nl("Card init. failed!");
  39. sdErrorCheck();
  40. while(1); // then 'halt' - do nothing!
  41. }
  42.  
  43.  
  44. // enable optimized read - some cards may time out
  45. card.partialBlockRead(true);
  46.  
  47. // find a FAT partition!
  48. uint8_t part;
  49. for (part = 0; part < 5; part++) // we have up to 5 slots to look in
  50. {
  51. if (vol.init(card, part))
  52. break; // found one so break out of this for loop
  53. }
  54. if (part == 5) // valid parts are 0 to 4, more not valid
  55. {
  56. putstring_nl("No valid FAT partition!");
  57. sdErrorCheck(); // Something went wrong, print the error
  58. while(1); // then 'halt' - do nothing!
  59. }
  60.  
  61. // tell the user about what we found
  62. putstring("Using partition ");
  63. Serial.print(part, DEC);
  64. putstring(", type is FAT");
  65. Serial.println(vol.fatType(),DEC); // FAT16 or FAT32?
  66.  
  67. // Try to open the root directory
  68. if (!root.openRoot(vol))
  69. {
  70. putstring_nl("Can't open root dir!"); // Something went wrong,
  71. while(1); // then 'halt' - do nothing!
  72. }
  73.  
  74. // if here then all the file prep succeeded.
  75. putstring_nl("Ready!");
  76. }
  77.  
  78. void loop()
  79. {
  80.  
  81. int sensorValue = analogRead(A1);
  82. if(sensorValue < 200);
  83. {
  84. Serial.println("PLAY");
  85. playcomplete("2.wav");
  86. }
  87. }
  88.  
  89. void playcomplete(char *name)
  90. {
  91. int sensorValue = analogRead(A1);
  92. // call our helper to find and play this name
  93. playfile(name);
  94. while (wave.isplaying);
  95. {
  96. if(sensorValue > 200) // check for high signal on pin 14 (analog 0)
  97. {
  98. Serial.println("STOP");
  99. wave.stop(); // stop and return if no person detected
  100. delay(500);
  101. }
  102. }
  103. }
  104.  
  105. void playfile(char *name) {
  106. // see if the wave object is currently doing something
  107. if (wave.isplaying) {
  108. // already playing something, so stop it!
  109. wave.stop(); // stop it
  110. }
  111. // look in the root directory and open the file
  112. if (!file.open(root, name)) {
  113. putstring("Couldn't open file ");
  114. Serial.print(name);
  115. return;
  116. }
  117. // read the file and turn it into a wave object
  118. if (!wave.create(file)) {
  119. putstring_nl("Not a valid WAV");
  120. return;
  121. }
  122. // start playback
  123. wave.play();
  124. }
  125.  
  126. void sdErrorCheck(void)
  127. {
  128. if (!card.errorCode()) return;
  129. putstring("\n\rSD I/O error: "); Serial.print(card.errorCode(), HEX);
  130. putstring(", ");
  131. Serial.println(card.errorData(), HEX);
  132. while(1)
  133. ; // stay here if there is an error
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement