Created
October 11, 2018 20:43
-
-
Save blhack/828185bf8a6771730e5a081bb79fa82a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <SoftwareSerial.h> | |
#define ARDUINO_RX 5//should connect to TX of the Serial MP3 Player module | |
#define ARDUINO_TX 6//connect to RX of the module | |
SoftwareSerial mySerial(ARDUINO_RX, ARDUINO_TX); | |
static int8_t Send_buf[8] = {0} ; | |
const int buttonPin = 2; | |
int buttonState = 0; | |
int buttonStateOld = 0; | |
#define CMD_SEL_DEV 0X09 | |
#define DEV_TF 0X02 | |
#define CMD_PLAY_W_VOL 0X22 | |
#define CMD_PLAY 0X0D | |
#define CMD_PAUSE 0X0E | |
#define CMD_PREVIOUS 0X02 | |
#define CMD_NEXT 0X01 | |
int16_t songs[] = {0X1E02, 0X1E03, 0X1E04, 0X1E06}; | |
void sendCommand(int8_t command, int16_t dat) { | |
delay(20); | |
Send_buf[0] = 0x7e; //starting byte | |
Send_buf[1] = 0xff; //version | |
Send_buf[2] = 0x06; //the number of bytes of the command without starting byte and ending byte | |
Send_buf[3] = command; // | |
Send_buf[4] = 0x00;//0x00 = no feedback, 0x01 = feedback | |
Send_buf[5] = (int8_t)(dat >> 8);//datah | |
Send_buf[6] = (int8_t)(dat); //datal | |
Send_buf[7] = 0xef; //ending byte | |
for(uint8_t i=0; i<8; i++)// | |
{ | |
mySerial.write(Send_buf[i]) ; | |
} | |
} | |
void playSong(int songId) { | |
Serial.print("Playing: "); | |
Serial.println(songId); | |
uint16_t song = songs[songId]; | |
sendCommand(CMD_PLAY_W_VOL, song); | |
} | |
void stopPlaying() { | |
sendCommand(CMD_PAUSE, 0X0E); | |
} | |
void setup() { | |
mySerial.begin(9600); | |
pinMode(buttonPin, INPUT_PULLUP); | |
Serial.begin(9600); | |
delay(500);//Wait chip initialization is complete | |
sendCommand(CMD_SEL_DEV, DEV_TF);//select the TF card | |
delay(200);//wait for 200ms | |
sendCommand(CMD_PLAY_W_VOL, 0X1E01);//play the first song with volume 30 class | |
delay(500); | |
} | |
void loop() { | |
for (int i=0; i<10; i++) { | |
playSong(i); | |
delay(5000); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment