/* TBD380 and TBD381 MP3 player from www.MDfly.com Digital Pins: 0. RX <- MP3 Player pin 16 (TX) 1. TX -> MP3 Player pin 15 (RX) 2. MP3 Player pin 13 (Busy) 3. PIR Signal Pin 4. 5. 6. BLINK (Turn off power to Servos) 7. 8. 9. NOD 10. TILT 11. TURN 12. 13. LED pin TDB380 / TBDB381 / MP3 Player Commands: 0x00 : Random play files in folder 01 (root directory) 0x01-0xC7 : Play the selected MP3 file 0xC8-0xE7 : Set Volume, Value = Command Word - 200 0xE8 : Volume + 0xE9 : Volume - 0xEB : Suspend Current Playing 0xEC : Resume Playing 0xEF : Stop to play 0xF1 : Change to folder 1 (Root Directory) 0xF2 : Change to folder 2 0xF3 : Change to folder 3 0xF4 : Change to folder 4 0xF5 : Change to folder 5 0xF6 : Change to folder 6 0xF7 : Change to folder 7 0xF8 : Change to folder 8 0xF9 : Change to folder 9 0xFA : Change to folder 10 0xFB : Change to folder 11 0xFC : Change to folder 12 0xFD : Change to folder 13 0xFE : Change to folder 14 0xFF : Change to folder 15 http://playground.arduino.cc/Code/PIRsense */ // ******************************************************* // // Constants / Global Variables // // ******************************************************* int inByte = 0; // incoming serial byte int busyPin = 2; // Tell if the MP3 is occupied/playing int calibrationTime = 30; // The time we give the sensor to calibrate (10-60 secs according to the datasheet) total time in seconds is 2*calibrationTime int pirPin = 3; // The PIR sensor's output long unsigned int lowIn; // The time when the sensor outputs a low impulse long unsigned int pause = 5000; // The amount of milliseconds the sensor has to be low // before we assume all motion has stopped boolean lockLow = true; boolean takeLowTime; int ledPin = 13; // Blinks once per second for first minute while PIR calibrates // ******************************************************* // // Prototypes // // ******************************************************* void playAll(); // MP3 player functions void randomPlay(); void volumeUp(); void volumeDown(); void playPause(); void hold(); void resume(); void stopMp3(); int checkBusy(); void calibrateTime(); // PIR Functions // ******************************************************* // // Setup // // ******************************************************* void setup() { pinMode(busyPin, INPUT); // Digital Pin 2 pinMode(pirPin, INPUT); // Digital Pin 3 digitalWrite(pirPin, LOW); // Turb off internal pull up resistor digitalWrite(busyPin, HIGH); // Turn on internal pull up resistor // Give the sensor some time to calibrate **** UNCOMMENT THIS WHEN PIR IS ADDED **** calibrateTime(); // 60 seconds calibration time LED pin 13 will flash while calibrating delay(50); Serial.begin(4800); // start serial port at 4800 bps: } // ******************************************************* // // MAIN // // ******************************************************* void loop() { Serial.write(0xDB); // Set Volume 0xC8-0xE7, Max Volume = 0xE7, 31 levels // playAll(); // PIR SENSOR START ********************** if((digitalRead(pirPin) == HIGH)) // The PIR saw something & MP3 player isn't playing { digitalWrite(ledPin, HIGH); // The led visualizes the sensors output pin state if(lockLow) // Starts True { lockLow = false; // Makes sure we wait for a transition to LOW before any further output is made: delay(50); } takeLowTime = true; // No default stopMp3(); // Stop the MP3 player playTrack(1); // Play one Random Track for(int i = 0; i < 4; i++) // Wait 7 seconds For MP3 to start. { delay(1000); } while(!checkBusy()) // Wait for MP3 player to finsih current track { if((digitalRead(pirPin) == HIGH)) // Visualize sensor busyPin or pirPin { digitalWrite(ledPin, HIGH); } else { digitalWrite(ledPin, LOW); } delay(50); } } if(digitalRead(pirPin) == LOW) { digitalWrite(ledPin, LOW); //the led visualizes the sensors output pin state if(takeLowTime) // If PIR was just High { lowIn = millis(); // Save the time of the transition from High to LOW takeLowTime = false; // Make sure this is only done at the start of a LOW phase } //if the sensor is low for more than the given pause, //we assume that no more motion is going to happen if(!lockLow && millis() - lowIn > pause) { //makes sure this block of code is only executed again after //a new motion sequence has been detected lockLow = true; delay(50); } } // PIR SENSOR END ********************** } // ******************************************************* // // Functions // // ******************************************************* // ******************************************************* // Calibrate the PIR // ******************************************************* void calibrateTime() { for(int i = 0; i < calibrationTime; i++) { digitalWrite(ledPin, HIGH); delay(500); // wait for a second digitalWrite(ledPin, LOW); delay(500); // wait for a second } digitalWrite(ledPin, HIGH); } // ******************************************************* // Play the first 13 audio files. // ******************************************************* void playAll() { // stopMp3(); //Serial.write(0x04); // Play track4 for(int i = 0; i < 13; i++) { if(i == 0) { stopMp3(); Serial.write(0x01); for(int j = 0; j < 40; j++) { delay(250); // 10 seconds } } else if(i == 1) { stopMp3(); Serial.write(0x02); for(int j = 0; j < 40; j++) { delay(250); // 10 seconds } } else if(i == 2) { stopMp3(); Serial.write(0x03); for(int j = 0; j < 40; j++) { delay(250); // 10 seconds } } else if(i == 3) { stopMp3(); Serial.write(0x04); for(int j = 0; j < 40; j++) { delay(250); // 10 seconds } } else if(i == 4) { stopMp3(); Serial.write(0x05); for(int j = 0; j < 40; j++) { delay(250); // 10 seconds } } else if(i == 5) { stopMp3(); Serial.write(0x06); for(int j = 0; j < 40; j++) { delay(250); // 10 seconds } } else if(i == 6) { stopMp3(); Serial.write(0x07); for(int j = 0; j < 40; j++) { delay(250); // 10 seconds } } else if(i == 7) { stopMp3(); Serial.write(0x08); for(int j = 0; j < 40; j++) { delay(250); // 10 seconds } } else if(i == 8) { stopMp3(); Serial.write(0x09); for(int j = 0; j < 40; j++) { delay(250); // 10 seconds } } else if(i == 9) { stopMp3(); Serial.write(0x0A); for(int j = 0; j < 40; j++) { delay(250); // 10 seconds } } else if(i == 10) { stopMp3(); Serial.write(0x0B); for(int j = 0; j < 40; j++) { delay(250); // 10 seconds } } else if(i == 11) { stopMp3(); Serial.write(0x0C); for(int j = 0; j < 40; j++) { delay(250); // 10 seconds } } else //(i == 12) { stopMp3(); Serial.write(0x0D); for(int j = 0; j < 40; j++) { delay(250); // 10 seconds } } } } // ******************************************************* // Play a specific track // ******************************************************* void playTrack(uint8_t track) { if(!checkBusy()) { stopMp3(); } if (track > 0 && track < 200) { Serial.write(track); } } // ******************************************************* // Tell the MP3 player to randomly play tracks // ******************************************************* void randomPlay() { Serial.write(0x00); delay(10); } // ******************************************************* // Turn the volume up one level // ******************************************************* void volumeUp() { Serial.write(0xE8); delay(10); } // ******************************************************* // Turn the volume down one level // ******************************************************* void volumeDown() { Serial.write(0xE9); delay(10); } // ******************************************************* // Play or Pause // ******************************************************* void playPause() { Serial.write(0xEA); delay(10); } // ******************************************************* // Suspend Current Playing // ******************************************************* void hold() { Serial.write(0xEB); delay(10); } // ******************************************************* // Resume Playing // ******************************************************* void resume() { Serial.write(0xEC); delay(10); } // ******************************************************* // STOP to play // ******************************************************* void stopMp3() { Serial.write(0xEF); delay(10); } // ******************************************************* // Check if the MP3 player can take a command // ******************************************************* int checkBusy() { int busy = digitalRead(busyPin); // output low when busy return busy; }