#include "SoftwareSerial.h" #include "DFRobotDFPlayerMini.h" #include // FOR DF Mini Player MP3 Module //initialize SoftwareSerial by using the pins 10 and 11 SoftwareSerial mySoftwareSerial(10, 11); // RX, TX DFRobotDFPlayerMini myDFPlayer; char command; int pause = 0; // FOR ServoMotor const int servoPin = 3; // Pin connected to the servo Servo myServo; // Servo object void setup(){ // FOR ServoMotor myServo.attach(servoPin); // FOR DF Mini Player MP3 Module //Serial communication with the module mySoftwareSerial.begin(9600); //Initialize the Arduino serial Serial.begin(115200); //Checking that the module is responding and that the SD card is found Serial.println(); Serial.println(F("DFRobot DFPlayer Mini")); Serial.println(F("Initializing DFPlayer module ... Wait!")); if (!myDFPlayer.begin(mySoftwareSerial)){ Serial.println(F("Not initialized:")); Serial.println(F("1. Check the DFPlayer Mini connections")); Serial.println(F("2. Insert an SD card")); while (true); } Serial.println(); Serial.println(F("DFPlayer Mini module initialized!")); //initial setup myDFPlayer.setTimeOut(500); //Timeout serial 500ms myDFPlayer.volume(30); //Max volume 30 myDFPlayer.EQ(0); //Normal equalization menu_opcoes(); } void loop(){ // put your main code here, to run repeatedly: // FOR ServoMotor //Choose a dance style danceServo(2); // FOR DF Mini Player MP3 Module //Waits for data entry via serial while (Serial.available() > 0){ command = Serial.read(); if ((command >= '1') && (command <= '9')){ Serial.print("Music reproduction"); Serial.println(command); command = command - 48; myDFPlayer.play(command); menu_opcoes(); } //Reproduction //Stop if (command == 's'){ myDFPlayer.stop(); Serial.println("Music Stopped!"); menu_opcoes(); } //Pause/Continue the music if (command == 'p'){ pause = !pause; if (pause == 0){ Serial.println("Continue..."); myDFPlayer.start(); } if (pause == 1){ Serial.println("Music Paused!"); myDFPlayer.pause(); } menu_opcoes(); } //Increases volume if (command == '+'){ myDFPlayer.volumeUp(); Serial.print("Current volume:"); Serial.println(myDFPlayer.readVolume()); menu_opcoes(); } if (command == '<'){ myDFPlayer.previous(); Serial.println("Previous:"); Serial.print("Current track:"); Serial.println(myDFPlayer.readCurrentFileNumber()-1); menu_opcoes(); } if (command == '>'){ myDFPlayer.next(); Serial.println("next:"); Serial.print("Current track:"); Serial.println(myDFPlayer.readCurrentFileNumber()+1); menu_opcoes(); } //Decreases volume if (command == '-'){ myDFPlayer.volumeDown(); Serial.print("Current Volume:"); Serial.println(myDFPlayer.readVolume()); menu_opcoes(); } } } void menu_opcoes(){ Serial.println(); Serial.println(F("==================================================================================================================================")); Serial.println(F("Commands:")); Serial.println(F(" [1-3] To select the MP3 file")); Serial.println(F(" [s] stopping reproduction")); Serial.println(F(" [p] pause/continue music")); Serial.println(F(" [+ or -] increases or decreases the volume")); Serial.println(F(" [< or >] forwards or backwards the track")); Serial.println(); Serial.println(F("=================================================================================================================================")); } void danceServo(int pattern) { switch (pattern) { case 1: // Example dance pattern 1 // Move the servo to position 0 degrees myServo.write(0); delay(500); // Pause for half a second // Move the servo to position 180 degrees myServo.write(180); delay(500); // Pause for half a second break; case 2: // Example dance pattern 2 // Move the servo to position 90 degrees myServo.write(90); delay(1000); // Pause for half a second // Move the servo to position 0 degrees myServo.write(0); delay(1000); // Pause for half a second // Move the servo to position 180 degrees myServo.write(180); delay(1000); // Pause for half a second break; default: // Default dance pattern // Move the servo to position 0 degrees myServo.write(0); delay(200); // Pause for two-tenths of a second // Move the servo to position 180 degrees myServo.write(180); delay(200); // Pause for two-tenths of a second break; } }