Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //import libraries downloaded from Sparkfun at https://github.com/sparkfun/LilyPad_MP3_Player
- // 3.3V, 8Mhz, Pro-Mini
- #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
- //DEFINE ports on MP3 Board
- #define T1 A0 // Trig1 analog port
- #define T2 A4 // Trig2 analog port
- #define T3 A5 // Trig3 analog port
- #define MP3_ENABLE A2 // Amp enable + MIDI/MP3 mode select
- #define SD_SEL 9 // Chip Select for SD card
- #define MIN_VOL 255 // Define volume settings for MP3 Board
- #define MAX_VOL 0
- #define REFERENCE_VOLTAGE 3.3 // Set source battery voltage for MP3 board
- #define SAMPLE_WINDOW 250 // Sampling time for sensors (ms)
- #define LIGHT_THOLD 2.0 // Threshold for light sensor
- #define SOUND_THOLD 1.2 // Threshold for sound sensor
- //Create variables
- int T1_sample; // Store sensor value from Trig1
- int T2_sample; // Store sensor value from Trig2
- int T3_output; // Store value to write to Trig3
- byte result; // Store results from calls to MP3 methods, 0-255 values only
- char filename1[]="track001.mp3"; //filename to play, must be 8.3 format only!!
- // Create objects
- SFEMP3Shield MP3player;
- SdFat sd;
- void setup() {
- // Start serial monitor
- Serial.begin(9600);
- // Set port input and output
- pinMode(T1,INPUT); // Sensor #1
- pinMode(T2,INPUT); // Sensor #2
- pinMode(T3,OUTPUT); // LED
- // Initialize SD Card and display result
- result = sd.begin(SD_SEL,SPI_HALF_SPEED);
- Serial.println("SD Card Initialized (1 for success) = "+String(result));
- // Initialize MP3 board and LED in this order
- pinMode(MP3_ENABLE,OUTPUT); //Enable MP3 mode
- digitalWrite(MP3_ENABLE,LOW); //Turn off amplifier
- result = MP3player.begin(); //Start board
- digitalWrite(MP3_ENABLE,HIGH); //Turn on amplifier
- delay(2);
- Serial.println("MP3 Enable (0 or 6 for success) = "+String(result));
- } // end setup
- void loop() {
- int sensor1_Max = 0.0; // Define maximum signal as 0 to start
- int sensor1_Min = 1024.0; // Define minimum signal as 1024 to start
- int sensor2_Max = 0.0; // Define maximum signal as 0 to start
- int sensor2_Min = 1024.0; // Define minimum signal as 1024 to start
- // Sample sensor1 and sensor 2
- unsigned long start= millis(); // Set start of sample window
- while (millis() - start < SAMPLE_WINDOW) {
- T1_sample = analogRead(T1);
- T2_sample = analogRead(T2);
- if (T1_sample > sensor1_Max)
- {
- sensor1_Max = T1_sample; // save just the max levels
- }
- else if (T1_sample < sensor1_Min)
- {
- sensor1_Min = T1_sample; // save just the min levels
- }
- if (T2_sample > sensor2_Max)
- {
- sensor2_Max = T2_sample; // save just the max levels
- }
- else if (T2_sample < sensor2_Min)
- {
- sensor2_Min = T2_sample; // save just the min levels
- }
- } // end while
- // Print Max volts detected from sensor 1
- float sensor1_max_volt = (sensor1_Max * REFERENCE_VOLTAGE)/1024;
- Serial.println("Sensor1 Volts Reading = "+String(sensor1_max_volt));
- // Print Peak voltage detected from sensor 2
- float sensor2_peak_volt = (sensor2_Max - sensor2_Min) * REFERENCE_VOLTAGE/1024;
- Serial.println("Sensor2 Peak Volts Reading = "+String(sensor2_peak_volt));
- //Example: Bright light or sound? Play filename
- if (sensor2_max_volt >= LIGHT_THOLD || sensor2_peak_volt >= SOUND_THOLD) {
- if (MP3player.isPlaying() == false) {
- MP3player.setVolume(MAX_VOL,MAX_VOL);
- result = MP3player.playMP3(filename1);
- Serial.println(F("Sensor Triggered"));
- //Flash LED
- analogWrite(T3,255);
- delay(500);
- analogWrite(T3,0);
- } //end if
- }
- delay(250);
- } // end loop
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement