基于Arduino的按键控制音乐播放器实验(MP3音乐播放器模块)

1、概述

MP3音乐播放器之前我们已经实现了通过红外遥控来实现对MP3播放器的控制,今天就做一个按键控制的MP3播放器。通过按键控制MP3的播放、暂停、上一曲、下一曲等功能。

对于模块不了解的,可以先看一下我们的模块教程[传送门],了解掌握模块使用方法后,再尝试本小实验。因为综合实验一般涉及多个模块,我们仅对全部使用我司产品的客户做技术支持,若只是部分使用我司产品的,我们只对我司产品部分是否有故障进行排查。

2、所需材料

1、UNO主控板:官方板 或者 国产板
2、辅助配件:面包板杜邦线
3、传感器和模块:MP3播放模块(可配喇叭和电阻、TF卡)、按键开关

3、指令表

4、接线方式

MP3模块 Arduino
VCC 5V
RX–1K电阻 D11
TX–1K电阻 D10
GND GND

MP3模块 小喇叭
SPL_1 红线
SPL_2 黑线

单个喇叭属于单声道,喇叭导线没有正负极之分。


按键开关 Arduino
按键1 D2–GND
按键2 D3–GND
按键3 D4–GND

5、例子程序

///              MP3 PLAYER PROJECT
/// http://educ8s.tv/arduino-mp3-player/
//////////////////////////////////////////


#include "SoftwareSerial.h"
SoftwareSerial mySerial(10, 11);
# define Start_Byte 0x7E
# define Version_Byte 0xFF
# define Command_Length 0x06
# define End_Byte 0xEF
# define Acknowledge 0x00 //Returns info with command 0x41 [0x01: info, 0x00: no info]

# define ACTIVATED LOW

int buttonNext = 2;
int buttonPause = 3;
int buttonPrevious = 4;
boolean isPlaying = false;



void setup () {

pinMode(buttonPause, INPUT);
digitalWrite(buttonPause,HIGH);
pinMode(buttonNext, INPUT);
digitalWrite(buttonNext,HIGH);
pinMode(buttonPrevious, INPUT);
digitalWrite(buttonPrevious,HIGH);

mySerial.begin (9600);
delay(1000);
playFirst();
isPlaying = true;


}



void loop () {

 if (digitalRead(buttonPause) == ACTIVATED)
  {
    if(isPlaying)
    {
      pause();
      isPlaying = false;
    }else
    {
      isPlaying = true;
      play();
    }
  }


 if (digitalRead(buttonNext) == ACTIVATED)
  {
    if(isPlaying)
    {
      playNext();
    }
  }

   if (digitalRead(buttonPrevious) == ACTIVATED)
  {
    if(isPlaying)
    {
      playPrevious();
    }
  }
}

void playFirst()
{
  execute_CMD(0x3F, 0, 0);
  delay(500);
  setVolume(20);
  delay(500);
  execute_CMD(0x11,0,1);
  delay(500);
}

void pause()
{
  execute_CMD(0x0E,0,0);
  delay(500);
}

void play()
{
  execute_CMD(0x0D,0,1);
  delay(500);
}

void playNext()
{
  execute_CMD(0x01,0,1);
  delay(500);
}

void playPrevious()
{
  execute_CMD(0x02,0,1);
  delay(500);
}

void setVolume(int volume)
{
  execute_CMD(0x06, 0, volume); // Set the volume (0x00~0x30)
  delay(2000);
}

void execute_CMD(byte CMD, byte Par1, byte Par2)
// Excecute the command and parameters
{
// Calculate the checksum (2 bytes)
word checksum = -(Version_Byte + Command_Length + CMD + Acknowledge + Par1 + Par2);
// Build the command line
byte Command_line[10] = { Start_Byte, Version_Byte, Command_Length, CMD, Acknowledge,
Par1, Par2, highByte(checksum), lowByte(checksum), End_Byte};
//Send the command line to the module
for (byte k=0; k<10; k++)
{
mySerial.write( Command_line[k]);
}
}

6、实验效果

7、相关下载

此程序使用官方softwareSerial库文件,为官方编译器(IDE)自带库文件。无需添加或下载。

6
分享到:

评论2

请先

  1. 666
    chituzongzi2019-03-12 15:12:34
  2. 请问:用0X4C查询TF卡播放曲目后,如何获得返回值?
    lisxi0012020-10-23 19:42:59
本站资料配套硬件销售店铺:天猫店ultirobot.tmall.com 、三冠淘宝店zhongbest.taobao.com
没有账号? 忘记密码?