Advertisement
kowalyshyn_o

MP3_Cackle

Feb 22nd, 2018
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.78 KB | None | 0 0
  1. // Applehead_witch
  2. // Demo program for Sparkfun's LilyPad MP3 Player
  3. // Mike Grusin, SparkFun Electronics
  4.  
  5. // This sketch works with Amanda Clark's Apple Head Witch tutorial.
  6. // If your shadow passes over an innocent-looking apple-head doll,
  7. // the LilyPad MP3 Player will play a scary sound!
  8.  
  9. // HOW IT WORKS:
  10.  
  11. // The sketch monitors a light sensor connected to TRIG1.
  12. // When the sketch first runs, it will sample a baseline light level
  13. // and compute a threshold value (baseline * 0.9). After that, if the
  14. // light level falls below this threshold, a sound file will play.
  15. // Also, TRIG2 will be set to HIGH while the file is playing, and
  16. // LOW otherwise (for optional scary LED eyes or other features).
  17.  
  18. // SOFTWARE INSTALLATION:
  19.  
  20. // If you haven't yet, you should install the LilyPad MP3 Player
  21. // libraries available here: https://github.com/sparkfun/LilyPad_MP3_Player
  22.  
  23. // HARDWARE CONNECTIONS:
  24.  
  25. // Connect the "S" pin of the LilyPad Light Sensor to TRIG1.
  26. // Also connect 3.3V to the Light Sensor's "+" pin, and
  27. // GND to the Light Sensor's "-" pin.
  28. // (Optional) Connect TRIG2 to LEDs through a resistor for 3.3V supply.
  29. // (330 Ohms is a common value).
  30. // Connect one or two speakers to the left and/or right speaker terminals.
  31. // Put a sound file on a microSD card and place it in the player.
  32. // Change the below filename to match the one you put on the SD card:
  33.  
  34. char filename[] = "cackle.mp3";
  35.  
  36. // Connect a 3.7V Lipo battery to the battery connector.
  37. // Connect a 5V FTDI Basic Breakout.
  38. // Remember to turn on the player before programming it!
  39.  
  40. // RUNNING THE SKETCH
  41.  
  42. // When you first run the sketch, the light sensor will be sampled for
  43. // the baseline light level. So have the project in it's installed position,
  44. // and avoid casting shadows on it, before turning it on.
  45.  
  46. // Once it is on, when you cast a shadow over the project, it should play
  47. // the audio file through the speaker.
  48.  
  49. // If it doesn't activate properly (too often or not often enough),
  50. // you can adjust the sensitivity value below.
  51. // The sensitivity can be from 0.0 to 1.0. The closer it is to 1.0, the more
  52. // sensitive the sketch will be. If you make it 1.0, it will probably activate
  53. // continuously.
  54. // If your project it too sensitive, make the sensitivity smaller.
  55. // If your project is not sensitive enough, make the sensitivity larger.
  56.  
  57. const float sensitivity = 0.9;
  58.  
  59. // If the sketch does not work properly, connect your 5V FTDI and open a
  60. // Serial Monitor at 9600 baud to receive debugging information.
  61.  
  62. // HAVE FUN!
  63. // Your friends at SparkFun
  64.  
  65.  
  66. // We'll need a few libraries to access all this hardware!
  67.  
  68. #include <SPI.h>            // To talk to the SD card and MP3 chip
  69. #include <SdFat.h>          // SD card file system
  70. #include <SFEMP3Shield.h>   // MP3 decoder chip
  71.  
  72. // Constants for the trigger pins:
  73.  
  74. const int TRIG1 = A0;
  75. const int TRIG2 = A4;
  76.  
  77. // Save the light sensor baseline reading:
  78.  
  79. int threshold;
  80.  
  81. // And a few output pins we'll be using:
  82.  
  83. const int ROT_LEDR = 10; // Red LED in rotary encoder (optional)
  84. const int EN_GPIO1 = A2; // Amp enable + MIDI/MP3 mode select
  85. const int SD_CS = 9;     // Chip Select for SD card
  86.  
  87. // Create library objects:
  88.  
  89. SFEMP3Shield MP3player;
  90. SdFat sd;
  91.  
  92. // Set debugging = true to send status messages to the serial port:
  93.  
  94. boolean debugging = true;
  95.  
  96.  
  97. void setup()
  98. {
  99.   byte result;
  100.  
  101.   // Use TRIG1 as an anlog input for our light sensor:
  102.  
  103.   pinMode(TRIG1,INPUT);
  104.  
  105.   // Use TRIG2 as a digital output that will be HIGH
  106.   // while we're playing an audio file and LOW otherwise:
  107.  
  108.   pinMode(TRIG2,OUTPUT);
  109.   digitalWrite(TRIG2,LOW);
  110.  
  111.   // If serial port debugging is inconvenient, you can connect
  112.   // a LED to the red channel of the rotary encoder to blink
  113.   // startup error codes:
  114.  
  115.   pinMode(ROT_LEDR,OUTPUT);
  116.   digitalWrite(ROT_LEDR,HIGH);  // HIGH = off
  117.  
  118.   // The board uses a single I/O pin to select the
  119.   // mode the MP3 chip will start up in (MP3 or MIDI),
  120.   // and to enable/disable the amplifier chip:
  121.  
  122.   pinMode(EN_GPIO1,OUTPUT);
  123.   digitalWrite(EN_GPIO1,LOW);  // MP3 mode / amp off
  124.  
  125.   // If debugging is true, initialize the serial port:
  126.   // (The 'F' stores constant strings in flash memory to save RAM)
  127.  
  128.   if (debugging)
  129.   {
  130.     Serial.begin(9600);
  131.     Serial.println(F("Lilypad MP3 Player trigger sketch"));
  132.   }
  133.  
  134.   // Initialize the SD card; SS = pin 9, half speed at first
  135.  
  136.   if (debugging) Serial.print(F("initialize SD card... "));
  137.  
  138.   result = sd.begin(SD_CS, SPI_HALF_SPEED); // 1 for success
  139.  
  140.   if (result != 1) // Problem initializing the SD card
  141.   {
  142.     if (debugging) Serial.print(F("error, halting"));
  143.     errorBlink(1); // Halt forever, blink LED if present.
  144.   }
  145.   else
  146.     if (debugging) Serial.println(F("success!"));
  147.  
  148.   // Start up the MP3 library
  149.  
  150.   if (debugging) Serial.print(F("initialize MP3 chip... "));
  151.  
  152.   result = MP3player.begin(); // 0 or 6 for success
  153.  
  154.   // Check the result, see the MP3 library readme for error codes.
  155.  
  156.   if ((result != 0) && (result != 6)) // Problem starting up
  157.   {
  158.     if (debugging)
  159.     {
  160.       Serial.print(F("error code "));
  161.       Serial.print(result);
  162.       Serial.print(F(", halting."));
  163.     }
  164.     errorBlink(result); // Halt forever, blink red LED if present.
  165.   }
  166.   else
  167.     if (debugging) Serial.println(F("success!"));
  168.  
  169.   // Set the VS1053 volume. 0 is loudest, 255 is lowest (off):
  170.  
  171.   MP3player.setVolume(10,10);
  172.  
  173.   // Get baseline readings from the light sensor:
  174.  
  175.   threshold = (analogRead(TRIG1));
  176.   if (debugging)
  177.   {
  178.     Serial.print(F("measured threshold value: "));
  179.     Serial.println(threshold);
  180.   }
  181.  
  182.   threshold = threshold * sensitivity;
  183.   if (debugging)
  184.   {
  185.     Serial.print(F("modified threshold value: "));
  186.     Serial.println(threshold);
  187.   }
  188.  
  189.   // Turn on the amplifier chip:
  190.  
  191.   digitalWrite(EN_GPIO1,HIGH);
  192.   delay(2);
  193. }
  194.  
  195.  
  196. void loop()
  197. {
  198.   byte result;
  199.   byte sensorvalue;
  200.  
  201.   // Get the current light level (sensorvalue):
  202.  
  203.   sensorvalue = analogRead(TRIG1);
  204.  
  205.   if (debugging)
  206.   {
  207.     Serial.print(F("sensor value: "));
  208.     Serial.print(sensorvalue);
  209.     Serial.print(F("  threshold: "));
  210.     Serial.println(threshold);
  211.   }
  212.  
  213.   // Check to see whether we're below the threshold
  214.  
  215.   if (sensorvalue < threshold)
  216.   {
  217.     if(debugging)
  218.     {
  219.       Serial.println(F("got a trigger!"));
  220.     }  
  221.  
  222.     // If we're currently playing a file, let it finish (don't start over)
  223.  
  224.     if (MP3player.isPlaying())
  225.     {
  226.       if(debugging)
  227.       {
  228.         Serial.println(F("...but we're already playing"));
  229.       }
  230.     }
  231.     else
  232.     {
  233.       // Play the file!
  234.  
  235.       result = MP3player.playMP3(filename);
  236.  
  237.       // Print out error information for debugging
  238.  
  239.       if(debugging)
  240.       {
  241.         if(result != 0)
  242.         {
  243.           Serial.print(F("error "));
  244.           Serial.print(result);
  245.           Serial.print(F(" when trying to play track "));
  246.         }
  247.         else
  248.         {
  249.           Serial.print(F("playing "));
  250.         }
  251.         Serial.println(filename);
  252.       }
  253.     }
  254.   }
  255.  
  256.   // For fun, we'll set TRIG2 HIGH while we're playing a file,
  257.   // and LOW when the player is silent.
  258.  
  259.   if (MP3player.isPlaying())
  260.     digitalWrite(TRIG2,HIGH);
  261.   else
  262.     digitalWrite(TRIG2,LOW);
  263. }
  264.  
  265.  
  266. void errorBlink(int blinks)
  267. {
  268.   // The following function will blink the red LED in the rotary
  269.   // encoder (optional) a given number of times and repeat forever.
  270.   // This is so you can see any startup error codes without having
  271.   // to use the serial monitor window.
  272.  
  273.   int x;
  274.  
  275.   while(true) // Loop forever
  276.   {
  277.     for (x=0; x < blinks; x++) // Blink the given number of times
  278.     {
  279.       digitalWrite(ROT_LEDR,LOW); // Turn LED ON
  280.       delay(250);
  281.       digitalWrite(ROT_LEDR,HIGH); // Turn LED OFF
  282.       delay(250);
  283.     }
  284.     delay(1500); // Longer pause between blink-groups
  285.   }
  286. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement