Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Code based on example and library for DFPlayer
- // https://wiki.dfrobot.com/DFPlayer_Mini_SKU_DFR0299#Connection_Diagram
- /****************************************************
- Example created 2016-12-07
- By [Angelo qiao](Angelo.qiao@dfrobot.com)
- GNU Lesser General Public License.
- See <http://www.gnu.org/licenses/> for details.
- All above must be included in any redistribution
- ****************************************************/
- #include "Arduino.h"
- #include "SoftwareSerial.h"
- #include "DFRobotDFPlayerMini.h"
- #include <AltSoftSerial.h>
- // High / low threshold of analog read value that triggers music fragments to play.
- unsigned int ldr_threshold = 300;
- int Opdroogtijd = 5*60*1000;
- // Define pins of LDRs.
- #define LDR_ONE A0
- #define LDR_TWO A1
- #define LDR_THREE A2
- #define LDR_FOUR A4
- #define LDR_FIVE A5
- unsigned long time_at_playing_sound_one = 0;
- unsigned long time_at_playing_sound_two = 0;
- unsigned long time_at_playing_sound_three = 0;
- unsigned long time_at_playing_sound_four = 0;
- unsigned long time_at_playing_sound_five = 0;
- AltSoftSerial altSerial;
- SoftwareSerial AltSoftSerial (21, 20); // Setting pins 0 and 1 (RX, TX) for serial communication.
- DFRobotDFPlayerMini myDFPlayer;
- void printDetail(uint8_t type, int value);
- void setup()
- {
- pinMode(LDR_ONE,INPUT); // One pin per LDR
- pinMode(LDR_TWO,INPUT);
- pinMode(LDR_THREE,INPUT);
- pinMode(LDR_FOUR,INPUT);
- pinMode(LDR_FIVE,INPUT);
- pinMode(13, OUTPUT);
- altSerial.begin(9600);
- Serial.begin(115200);
- /* Serial.println();
- Serial.println(F("DFRobot DFPlayer Mini Demo"));
- Serial.println(F("Initializing DFPlayer ... (May take 3~5 seconds)"));
- */
- myDFPlayer.begin(altSerial);
- // if (!myDFPlayer.begin(altSerial)) { //Use softwareSerial to communicate with mp3.
- // // digitalWrite(13, HIGH);
- // Serial.println(F("Unable to begin:"));
- // Serial.println(F("1.Please recheck the connection!"));
- // Serial.println(F("2.Please insert the SD card!"));
- // while(true);
- // }
- Serial.println(F("DFPlayer Mini online."));
- myDFPlayer.setTimeOut(500); //Set serial communictaion time out 500ms
- // Set volume
- myDFPlayer.volume(29); //Set volume value (0~30).
- // Set EQ
- myDFPlayer.EQ(DFPLAYER_EQ_NORMAL);
- //Set SD as default
- myDFPlayer.outputDevice(DFPLAYER_DEVICE_SD);
- myDFPlayer.playFolder(1, 1); //play specific mp3 in SD:/15/004.mp3; Folder Name(1~99); File Name(1~255)
- /*// Read imformation
- Serial.println(myDFPlayer.readState()); //read mp3 state
- Serial.println(myDFPlayer.readVolume()); //read current volume
- Serial.println(myDFPlayer.readEQ()); //read EQ setting
- Serial.println(myDFPlayer.readFileCounts()); //read all file counts in SD card
- Serial.println(myDFPlayer.readCurrentFileNumber()); //read current play file number
- Serial.println(myDFPlayer.readFileCountsInFolder(3)); //read fill counts in folder SD:/03
- */}
- void loop()
- {
- // static unsigned long timer = millis();
- // READ SENSOR VALUES OF LDRS
- /*
- int sensorValueA0 = analogRead(LDR_ONE);
- Serial.println(sensorValueA0);
- int sensorValueA1 = analogRead(LDR_TWO);
- Serial.println(sensorValueA1);
- int sensorValueA2 = analogRead(LDR_THREE);
- Serial.println(sensorValueA2);
- int sensorValueA3 = analogRead(LDR_FOUR);
- Serial.println(sensorValueA3);
- int sensorValueA4 = analogRead(LDR_FIVE);
- Serial.println(sensorValueA4);
- */
- int TEST = millis()-time_at_playing_sound_one;
- Serial.println(TEST);
- if (analogRead(LDR_ONE) < ldr_threshold && ((millis()-time_at_playing_sound_one ) > Opdroogtijd) || time_at_playing_sound_one == 0)) {
- // Serial.println(F("No touchy pin 14"));
- myDFPlayer.play(1);
- time_at_playing_sound_one = millis();
- }
- if (analogRead(LDR_TWO) < ldr_threshold && ((millis()-time_at_playing_sound_two ) > Opdroogtijd) || time_at_playing_sound_two == 0)){
- // Serial.println(F("No touchy pin 15"));
- myDFPlayer.play(2);
- }
- if (analogRead(LDR_THREE) < ldr_threshold && ((millis()-time_at_playing_sound_three ) > Opdroogtijd) || time_at_playing_sound_three == 0))
- // Serial.println(F("No touchy pin 16"));
- myDFPlayer.play(3);
- }
- if (analogRead(LDR_FOUR) < ldr_threshold && ((millis()-time_at_playing_sound_four ) > Opdroogtijd) || time_at_playing_sound_four == 0))
- // Serial.println(F("No touchy pin 16"));
- myDFPlayer.play(4);
- }
- if (analogRead(LDR_FIVE) < ldr_threshold && ((millis()-time_at_playing_sound_five ) > Opdroogtijd) || time_at_playing_sound_five == 0))
- // Serial.println(F("No touchy pin 16"));
- myDFPlayer.play(5);
- }
- }
- void printDetail(uint8_t type, int value){
- switch (type) {
- case TimeOut:
- Serial.println(F("Time Out!"));
- break;
- case WrongStack:
- Serial.println(F("Stack Wrong!"));
- break;
- case DFPlayerCardInserted:
- Serial.println(F("Card Inserted!"));
- break;
- case DFPlayerCardRemoved:
- Serial.println(F("Card Removed!"));
- break;
- case DFPlayerCardOnline:
- Serial.println(F("Card Online!"));
- break;
- case DFPlayerPlayFinished:
- Serial.print(F("Number:"));
- Serial.print(value);
- Serial.println(F(" Play Finished!"));
- break;
- case DFPlayerError:
- Serial.print(F("DFPlayerError:"));
- switch (value) {
- case Busy:
- Serial.println(F("Card not found"));
- break;
- case Sleeping:
- Serial.println(F("Sleeping"));
- break;
- case SerialWrongStack:
- Serial.println(F("Get Wrong Stack"));
- break;
- case CheckSumNotMatch:
- Serial.println(F("Check Sum Not Match"));
- break;
- case FileIndexOut:
- Serial.println(F("File Index Out of Bound"));
- break;
- case FileMismatch:
- Serial.println(F("Cannot Find File"));
- break;
- case Advertise:
- Serial.println(F("In Advertise"));
- break;
- default:
- break;
- }
- break;
- default:
- break;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement