Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Applehead_witch
- // Demo program for Sparkfun's LilyPad MP3 Player
- // Mike Grusin, SparkFun Electronics
- // This sketch works with Amanda Clark's Apple Head Witch tutorial.
- // If your shadow passes over an innocent-looking apple-head doll,
- // the LilyPad MP3 Player will play a scary sound!
- // HOW IT WORKS:
- // The sketch monitors a light sensor connected to TRIG1.
- // When the sketch first runs, it will sample a baseline light level
- // and compute a threshold value (baseline * 0.9). After that, if the
- // light level falls below this threshold, a sound file will play.
- // Also, TRIG2 will be set to HIGH while the file is playing, and
- // LOW otherwise (for optional scary LED eyes or other features).
- // SOFTWARE INSTALLATION:
- // If you haven't yet, you should install the LilyPad MP3 Player
- // libraries available here: https://github.com/sparkfun/LilyPad_MP3_Player
- // HARDWARE CONNECTIONS:
- // Connect the "S" pin of the LilyPad Light Sensor to TRIG1.
- // Also connect 3.3V to the Light Sensor's "+" pin, and
- // GND to the Light Sensor's "-" pin.
- // (Optional) Connect TRIG2 to LEDs through a resistor for 3.3V supply.
- // (330 Ohms is a common value).
- // Connect one or two speakers to the left and/or right speaker terminals.
- // Put a sound file on a microSD card and place it in the player.
- // Change the below filename to match the one you put on the SD card:
- char filename[] = "cackle.mp3";
- // Connect a 3.7V Lipo battery to the battery connector.
- // Connect a 5V FTDI Basic Breakout.
- // Remember to turn on the player before programming it!
- // RUNNING THE SKETCH
- // When you first run the sketch, the light sensor will be sampled for
- // the baseline light level. So have the project in it's installed position,
- // and avoid casting shadows on it, before turning it on.
- // Once it is on, when you cast a shadow over the project, it should play
- // the audio file through the speaker.
- // If it doesn't activate properly (too often or not often enough),
- // you can adjust the sensitivity value below.
- // The sensitivity can be from 0.0 to 1.0. The closer it is to 1.0, the more
- // sensitive the sketch will be. If you make it 1.0, it will probably activate
- // continuously.
- // If your project it too sensitive, make the sensitivity smaller.
- // If your project is not sensitive enough, make the sensitivity larger.
- const float sensitivity = 0.9;
- // If the sketch does not work properly, connect your 5V FTDI and open a
- // Serial Monitor at 9600 baud to receive debugging information.
- // HAVE FUN!
- // Your friends at SparkFun
- // We'll need a few libraries to access all this hardware!
- #include <SPI.h> // To talk to the SD card and MP3 chip
- #include <SdFat.h> // SD card file system
- #include <SFEMP3Shield.h> // MP3 decoder chip
- // Constants for the trigger pins:
- const int TRIG1 = A0;
- const int TRIG2 = A4;
- // Save the light sensor baseline reading:
- int threshold;
- // And a few output pins we'll be using:
- const int ROT_LEDR = 10; // Red LED in rotary encoder (optional)
- const int EN_GPIO1 = A2; // Amp enable + MIDI/MP3 mode select
- const int SD_CS = 9; // Chip Select for SD card
- // Create library objects:
- SFEMP3Shield MP3player;
- SdFat sd;
- // Set debugging = true to send status messages to the serial port:
- boolean debugging = true;
- void setup()
- {
- byte result;
- // Use TRIG1 as an anlog input for our light sensor:
- pinMode(TRIG1,INPUT);
- // Use TRIG2 as a digital output that will be HIGH
- // while we're playing an audio file and LOW otherwise:
- pinMode(TRIG2,OUTPUT);
- digitalWrite(TRIG2,LOW);
- // If serial port debugging is inconvenient, you can connect
- // a LED to the red channel of the rotary encoder to blink
- // startup error codes:
- pinMode(ROT_LEDR,OUTPUT);
- digitalWrite(ROT_LEDR,HIGH); // HIGH = off
- // The board uses a single I/O pin to select the
- // mode the MP3 chip will start up in (MP3 or MIDI),
- // and to enable/disable the amplifier chip:
- pinMode(EN_GPIO1,OUTPUT);
- digitalWrite(EN_GPIO1,LOW); // MP3 mode / amp off
- // If debugging is true, initialize the serial port:
- // (The 'F' stores constant strings in flash memory to save RAM)
- if (debugging)
- {
- Serial.begin(9600);
- Serial.println(F("Lilypad MP3 Player trigger sketch"));
- }
- // Initialize the SD card; SS = pin 9, half speed at first
- if (debugging) Serial.print(F("initialize SD card... "));
- result = sd.begin(SD_CS, SPI_HALF_SPEED); // 1 for success
- if (result != 1) // Problem initializing the SD card
- {
- if (debugging) Serial.print(F("error, halting"));
- errorBlink(1); // Halt forever, blink LED if present.
- }
- else
- if (debugging) Serial.println(F("success!"));
- // Start up the MP3 library
- if (debugging) Serial.print(F("initialize MP3 chip... "));
- result = MP3player.begin(); // 0 or 6 for success
- // Check the result, see the MP3 library readme for error codes.
- if ((result != 0) && (result != 6)) // Problem starting up
- {
- if (debugging)
- {
- Serial.print(F("error code "));
- Serial.print(result);
- Serial.print(F(", halting."));
- }
- errorBlink(result); // Halt forever, blink red LED if present.
- }
- else
- if (debugging) Serial.println(F("success!"));
- // Set the VS1053 volume. 0 is loudest, 255 is lowest (off):
- MP3player.setVolume(10,10);
- // Get baseline readings from the light sensor:
- threshold = (analogRead(TRIG1));
- if (debugging)
- {
- Serial.print(F("measured threshold value: "));
- Serial.println(threshold);
- }
- threshold = threshold * sensitivity;
- if (debugging)
- {
- Serial.print(F("modified threshold value: "));
- Serial.println(threshold);
- }
- // Turn on the amplifier chip:
- digitalWrite(EN_GPIO1,HIGH);
- delay(2);
- }
- void loop()
- {
- byte result;
- byte sensorvalue;
- // Get the current light level (sensorvalue):
- sensorvalue = analogRead(TRIG1);
- if (debugging)
- {
- Serial.print(F("sensor value: "));
- Serial.print(sensorvalue);
- Serial.print(F(" threshold: "));
- Serial.println(threshold);
- }
- // Check to see whether we're below the threshold
- if (sensorvalue < threshold)
- {
- if(debugging)
- {
- Serial.println(F("got a trigger!"));
- }
- // If we're currently playing a file, let it finish (don't start over)
- if (MP3player.isPlaying())
- {
- if(debugging)
- {
- Serial.println(F("...but we're already playing"));
- }
- }
- else
- {
- // Play the file!
- result = MP3player.playMP3(filename);
- // Print out error information for debugging
- if(debugging)
- {
- if(result != 0)
- {
- Serial.print(F("error "));
- Serial.print(result);
- Serial.print(F(" when trying to play track "));
- }
- else
- {
- Serial.print(F("playing "));
- }
- Serial.println(filename);
- }
- }
- }
- // For fun, we'll set TRIG2 HIGH while we're playing a file,
- // and LOW when the player is silent.
- if (MP3player.isPlaying())
- digitalWrite(TRIG2,HIGH);
- else
- digitalWrite(TRIG2,LOW);
- }
- void errorBlink(int blinks)
- {
- // The following function will blink the red LED in the rotary
- // encoder (optional) a given number of times and repeat forever.
- // This is so you can see any startup error codes without having
- // to use the serial monitor window.
- int x;
- while(true) // Loop forever
- {
- for (x=0; x < blinks; x++) // Blink the given number of times
- {
- digitalWrite(ROT_LEDR,LOW); // Turn LED ON
- delay(250);
- digitalWrite(ROT_LEDR,HIGH); // Turn LED OFF
- delay(250);
- }
- delay(1500); // Longer pause between blink-groups
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement