How to Make A Timing Music Player Based On Arduino UNO

How to Make A Timing Music Player Based On Arduino UNO

Introduction:   A timing music player allows us to relax with a piece of music on a designated time or remind us it is time to do something. Today we are going to make a timing music player with Arduino UNO.  

Tools & Materials:  

1 X Freaduino UNO Rev2.2 MB_EFUNO

1 X Mp3 Module

2 X Octopus Real-time Clock

1 X Micro SD Card

1 X Speaker  

Hardware Connection:  

Let's take a look at our final work of hardware connection first.  

  Plug your components together according to the table below.


Mp3 Module

Freaduino UNO Rev2.2 MB_EFUNO

VCC

V

GND

G

TX

D2

RX

D3

Finnally, connect Mp3 Module with your speaker. Now all connection are completed! MP3 Module It is inspired by JQ6500-24SS MP3 chipset of Jiaqiang Electronics. It supports SD cards for FAT16, FAT32 file systems and SPI Flash updates via PC. With some very simple serial commands to control music playback, it owns the characteristics of easy operation and stable performance. Additionally, with an external headphone (PH) and an external speaker (SP), the MP3 Module provides more interfaces and utilities. MP3 Module Commands:  

OCTOPUS Real-time Clock   This is a clock module which can achieve counting for year, month, day, hour by using the DS1307 clock chip.This chip has lots of merits. For example, low power supply, with 56 bytes of non-volatile RAM clock calendar and BCD code real-time clock chip, transmit address and data via a two-wire bidirectional serial bus, provide time information in detail as well as other information, automatically adjust the number of days in each month, etc.. And more surprisingly, the chip has also equipped with the function of leap year compensation. It has a built-in power-sense circuit with the function of brownout detection and battery switching.  

Programming   First of all, we have to upload 2 package files: "DS1307RTC" and "Time".  

5  

Next click "Examples-DS1307RTC-SetTime", choose "SetTime", and then upload your code. Set your computer system time to be time showed within Octopus Real-time Clock.  

6  

[cceN_cpp theme="dawn"] #include <Wire.h> #include <Time.h> #include <DS1307RTC.h> #include <TimeLib.h> const char *monthName[12] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; tmElements_t tm; void setup() { bool parse=false; bool config=false; // get the date and time the compiler was run if (getDate(__DATE__) && getTime(__TIME__)) { parse = true; // and configure the RTC with this info if (RTC.write(tm)) { config = true; } } Serial.begin(9600); while (!Serial) ; // wait for Arduino Serial Monitor delay(200); if (parse && config) { Serial.print("DS1307 configured Time="); Serial.print(__TIME__); Serial.print(", Date="); Serial.println(__DATE__); } else if (parse) { Serial.println("DS1307 Communication Error :-{"); Serial.println("Please check your circuitry"); } else { Serial.print("Could not parse info from the compiler, Time=\""); Serial.print(__TIME__); Serial.print("\", Date=\""); Serial.print(__DATE__); Serial.println("\""); } } void loop() { } bool getTime(const char *str) { int Hour, Min, Sec; if (sscanf(str, "%d:%d:%d", &Hour, &Min, &Sec) != 3) return false; tm.Hour = Hour; tm.Minute = Min; tm.Second = Sec; return true; } bool getDate(const char *str) { char Month[12]; int Day, Year; uint8_t monthIndex; if (sscanf(str, "%s %d %d", Month, &Day, &Year) != 3) return false; for (monthIndex = 0; monthIndex < 12; monthIndex++) { if (strcmp(Month, monthName[monthIndex]) == 0) break; } if (monthIndex >= 12) return false; tm.Day = Day; tm.Month = monthIndex + 1; tm.Year = CalendarYrToTm(Year); return true; } [/cceN_cpp]

In the following, download our code for music playing.

[cceN_cpp theme="dawn"] #include <Wire.h> #include <Time.h> #include <DS1307RTC.h> #include <TimeLib.h> #include <SoftwareSerial.h> SoftwareSerial mySerial(2, 3); unsigned char play[6] = {0x7E, 0x04, 0x03, 0x00, 0x01, 0xEF}; //play unsigned char SC_SD[5] = {0x7E, 0x03, 0x09, 0x01, 0xEF};//set music effects unsigned char play_mode[5] = {0x7E, 0x03, 0x11, 0x00, 0xEF};//set play mode unsigned char pause[4] = {0x7E, 0x02, 0x0E, 0xEF}; //stop unsigned char next[4] = {0x7E, 0x02, 0x01, 0xEF}; //next one unsigned int chutime[6] = {15, 30, 0, 26, 12, 2017}; unsigned int time1[3] = {9, 0, 5}; //start at 9:00 and end at 9:05 unsigned int time2[3] = {13, 30, 40}; //start at 13:30 and end at 13:40 unsigned int time3[3] = {12, 0, 10}; //start at 12:00 and end at 12:10 unsigned int time4[3] = {16, 0, 15}; //start at 16:00 and end at 16:15 unsigned int time5[3] = {18, 30, 45}; //start at 18:30 and end at 18:45 unsigned char TimeHour, TimeMinute, TimeSecond; unsigned char MP3status = 0; void setup() { Serial.begin(9600); while (!Serial) ; // wait for serial delay(200); Serial.println("DS1307RTC Read Test"); Serial.println("-------------------"); mySerial.begin(9600); delay(1000); mySerial.write(SC_SD, 5); delay(2000); mySerial.write(play_mode, 5); delay(500); mySerial.write(play, 6); delay(2000); mySerial.write(pause, 4); //stop play delay(500); } void xianshi() { tmElements_t tm; if (RTC.read(tm)) { Serial.print("Time = "); TimeHour = tm.Hour; Serial.print(TimeHour); Serial.write(':'); TimeMinute = tm.Minute; //minute (); Serial.print(TimeMinute); Serial.write(':'); TimeSecond = tm.Second; Serial.print(TimeSecond); Serial.print(" "); Serial.print(tm.Day); Serial.print(" Day "); Serial.print(tm.Month); Serial.print(" Month "); Serial.print(tmYearToCalendar(tm.Year)); Serial.println(" Year "); } else { if (RTC.chipPresent()) { Serial.println("The DS1307 is stopped. Please run the SetTime"); Serial.println("example to initialize the time and begin running."); Serial.println(); } else { Serial.println("DS1307 read error! Please check the MP3statusrcuitry."); Serial.println(); } delay(9000); } } void loop() { xianshi(); if (TimeHour == time1[0] && TimeMinute >= time1[1] && TimeMinute < time1[2] ) //9:00 { if (MP3status == 0) { mySerial.write(next, 4); delay(5); mySerial.write(play, 6); //start play delay(10); Serial.println("The first start"); MP3status = 1; } else Serial.println("The first start"); } if (TimeHour == time1[0] && TimeMinute == time1[2] ) { if (MP3status == 1) { mySerial.write(pause, 4); //stop play delay(10); Serial.println("The first one is over"); MP3status = 0; } else Serial.println("The first one is over"); } if (TimeHour == time2[0] && TimeMinute >= time2[1] && TimeMinute < time2[2]) //13:30 { if (MP3status == 0) { mySerial.write(next, 4); delay(5); mySerial.write(play, 6); //start play delay(10); Serial.println("The second start"); MP3status = 1; } else Serial.println("The second start"); } if (TimeHour == time2[0] && TimeMinute == time2[2] ) { if (MP3status == 1) { mySerial.write(pause, 4); //stop play delay(10); Serial.println("The second end"); MP3status = 0; } else Serial.println("The second end"); } if (TimeHour == time3[0] && TimeMinute >= time3[1] && TimeMinute < time3[2]) //16:00 { if (MP3status == 0) { mySerial.write(next, 4); delay(5); mySerial.write(play, 6); //start play delay(10); Serial.println("The third start"); MP3status = 1; } else Serial.println("The third start"); } if (TimeHour == time3[0] && TimeMinute == time3[2] ) { if (MP3status == 1) { mySerial.write(pause, 4); //stop play delay(10); Serial.println("The third end"); MP3status = 0; } else Serial.println("The third end"); } if (TimeHour == time4[0] && TimeMinute >= time4[1] && TimeMinute < time4[2]) //16:00 { if (MP3status == 0) { mySerial.write(next, 4); delay(5); mySerial.write(play, 6); //start play delay(10); Serial.println("The fourth start"); MP3status = 1; } else Serial.println("The fourth start"); } if (TimeHour == time4[0] && TimeMinute == time4[2] ) { if (MP3status == 1) { mySerial.write(pause, 4); //stop play delay(10); Serial.println("The fourth end"); MP3status = 0; } else Serial.println("The fourth end"); } if (TimeHour == time5[0] && TimeMinute >= time5[1] && TimeMinute < time5[2]) //16:00 { if (MP3status == 0) { mySerial.write(next, 4); delay(5); mySerial.write(play, 6); //start play delay(10); Serial.println("The fifth start"); MP3status = 1; } else Serial.println("The fifth start"); } if (TimeHour == time5[0] && TimeMinute == time5[2] ) { if (MP3status == 1) { mySerial.write(pause, 4); //stop play delay(10); Serial.println("The fifth end"); MP3status = 0; } else Serial.println("The fifth end"); } delay(200); } void print2digits(int number) { if (number >= 0 && number < 10) { Serial.write('0'); } Serial.print(number); } [/cceN_cpp]

Code Explain:  

Mp3 Module commands have to be written into code in the format of array. Send these commands through serial port so as to achieve the control of Mp3 Module. unsigned char play[6] = {0x7E, 0x04, 0x03, 0x00, 0x01, 0xEF}; //play bofang unsigned char SC_SD[5] = {0x7E, 0x03, 0x09, 0x01, 0xEF};//set music effects unsigned char play_mode[5] = {0x7E, 0x03, 0x11, 0x00, 0xEF};//set play mode unsigned char pause[4] = {0x7E, 0x02, 0x0E, 0xEF}; //stop unsigned char next[4] = {0x7E, 0x02, 0x01, 0xEF}; //next one   Set Pins for Software Serial Port SoftwareSerial mySerial(2, 3);   Set the start and end time of play. In the array, there are 3 values relatively for: the hour pin in the beginning, the minute pin and the end minute pin in the play. unsigned int time1[3] = {9, 0, 5}; //start at 9:00 and end at 9:05. Judge if time is between 9:00 and 9:05. If it is, then play music. if (TimeHour == time1[0] && TimeMinute >= time1[1] && TimeMinute < time1[2] ) //9:00 { if (MP3status == 0) { mySerial.write(next, 4); delay(5); mySerial.write(play, 6); //start play delay(10); Serial.println("The first start"); MP3status = 1; } else Serial.println("The first start"); } Judge if time is 9:05. If it is, stop playing music. if (TimeHour == time1[0] && TimeMinute == time1[2] ) { if (MP3status == 1) { mySerial.write(pause, 4); //stop play delay(10); Serial.println("The first one is over"); MP3status = 0; } else Serial.println("The first one is over"); }  

Success   Now you have already successfully made your own timing music player. Let's have a try and see what will happen!