G2A Many GEOs
SHARE
TWEET

Seinfeld Door Sensor

a guest Apr 14th, 2016 1,649 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. // Author c_alix on reddit
  3.  
  4. #include <SPI.h>
  5. #include <Adafruit_VS1053.h>
  6. #include <SD.h>
  7.  
  8. // These are the pins used for the breakout example
  9. #define BREAKOUT_RESET  9      // VS1053 reset pin (output)
  10. #define BREAKOUT_CS     10     // VS1053 chip select pin (output)
  11. #define BREAKOUT_DCS    8      // VS1053 Data/command select pin (output)
  12. // These are the pins used for the music maker shield
  13. #define SHIELD_RESET  -1      // VS1053 reset pin (unused!)
  14. #define SHIELD_CS     7      // VS1053 chip select pin (output)
  15. #define SHIELD_DCS    6      // VS1053 Data/command select pin (output)
  16.  
  17. // These are common pins between breakout and shield
  18. #define CARDCS 4     // Card chip select pin
  19. // DREQ should be an Int pin, see http://arduino.cc/en/Reference/attachInterrupt
  20. #define DREQ 3       // VS1053 Data request, ideally an Interrupt pin
  21.  
  22. Adafruit_VS1053_FilePlayer musicPlayer = Adafruit_VS1053_FilePlayer(SHIELD_RESET, SHIELD_CS, SHIELD_DCS, DREQ, CARDCS);
  23.  
  24.  
  25.  
  26. int n_files;
  27. int led = 9;
  28.  
  29.  
  30. void setup()
  31. {  
  32.   Serial.begin(9600);
  33.   Serial.println("Ready, Start!");
  34.  
  35.   pinMode(led, OUTPUT);
  36.   pinMode(2,INPUT);
  37.  
  38.   // initialise the music player
  39.   if (! musicPlayer.begin()) { // initialise the music player
  40.     Serial.println(F("Couldn't find VS1053, do you have the right pins defined?"));
  41.     while (1);
  42.   }
  43.   Serial.println(F("VS1053 chip found..."));
  44.  
  45.  
  46.   musicPlayer.sineTest(0x44, 500);    // Make a tone to indicate VS1053 is working
  47.  
  48.   if (!SD.begin(CARDCS)) {
  49.     Serial.println(F("SD failed, or not present"));
  50.     while (1);  // don't do anything more
  51.   }
  52.  
  53.   Serial.println("SD Card Loaded...");
  54.   Serial.println();
  55.  
  56.   musicPlayer.setVolume(1,1);
  57.  
  58.   n_files = countMP3File(SD.open("/"));
  59.  
  60.   Serial.print("Number of MP3 files: ");
  61.   Serial.println(n_files);
  62.   Serial.println();
  63. }
  64.  
  65. int countMP3File(File dir)
  66. {
  67.   int counter = 0;
  68.  
  69.   Serial.println("Listing files:");
  70.  
  71.   while(true)
  72.   {
  73.     File entry = dir.openNextFile();
  74.     if (! entry)
  75.     {
  76.       dir.rewindDirectory();
  77.       break;
  78.     }
  79.  
  80.     Serial.println(entry.name());
  81.  
  82.     counter++;
  83.  
  84.     entry.close();
  85.   }
  86.  
  87.   dir.close();
  88.  
  89.   Serial.println();
  90.   return counter;
  91. }
  92.  
  93.  
  94. void loop()
  95. {
  96.   int i, rand_song;
  97.  
  98.   Serial.print("Motion Sensor Val: ");
  99.   Serial.println(digitalRead(2));
  100.  
  101.   if (digitalRead(2) == 1)
  102.   {
  103.     File folder = SD.open("/");
  104.     File random_file;
  105.    
  106.     rand_song = random(0, n_files)+1;
  107.  
  108.     folder.rewindDirectory();  
  109.  
  110.     random_file = selectFileN(rand_song, folder);
  111.  
  112.     folder.close();
  113.    
  114.     Serial.print("Playing: ");
  115.     Serial.println(random_file.name());
  116.    
  117.     // smoke machine
  118.     //digitalWrite(led, LOW);
  119.    
  120.     if(!musicPlayer.playFullFile(random_file.name()))
  121.     {
  122.       Serial.println("Could not open mp3 file");
  123.     }
  124.     random_file.close();
  125.    
  126.     // stop smoke machine
  127.     //digitalWrite(led, HIGH);    
  128.    
  129.     Serial.println("Waiting 6 sec...");
  130.     delay(6000);
  131.   }
  132.  
  133.  
  134.   Serial.println();
  135.   delay(100);
  136. }
  137.  
  138. File selectFileN(int number, File dir)
  139. {
  140.   int counter = 0;
  141.   File return_entry;
  142.  
  143.   while(true)
  144.   {
  145.     File entry = dir.openNextFile();
  146.     if (! entry)
  147.     {
  148.       //Serial.println("Last file reached");
  149.       dir.rewindDirectory();
  150.       break;
  151.     }
  152.  
  153.     //Serial.println(entry.name());
  154.  
  155.     counter++;
  156.  
  157.     if(counter==number)
  158.     {
  159.       return_entry = entry;
  160.       dir.rewindDirectory();
  161.       break;
  162.     }
  163.  
  164.     entry.close();
  165.   }
  166.   return return_entry;
  167. }
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