【创客玩音乐】arduino极简电子琴

2020-03-061.4万
AI 快速预览详细 收起
本项目介绍如何使用Arduino Mega2560和DF MP3模块,通过ADTouch库实现一个简单的电子琴。硬件清单包括Arduino Mega2560、DF MP3模块和一些电线。通过下载钢琴音阶mp3文件到MP3模块,并连接Arduino,可以轻松实现音乐创作。这个项目适合STEM教育场景,适合儿童科学实验和编程启蒙。
本次实验的原理主要是利用Arduino的模拟触控ADTouch库,将接收到的模拟信号输出触发相应的mp3模块的mp3文件,从而使音响发出不同的声音。力求用最少的元件打造一个“电子琴”。



硬件清单:


【创客玩音乐】arduino极简电子琴图1
1.arduino mega2560


【创客玩音乐】arduino极简电子琴图2
2.DF MP3模块
https://wiki.dfrobot.com.cn/index.php?title=(SKU:DFR0534)Voice_Module

3.一些电线
====================
把下载好的钢琴音阶mp3文件下载到mp3模块里,文件名以0开头。

【创客玩音乐】arduino极简电子琴图3
连线图如下A0-A7是触控线,用来接收模拟信号,可以理解为代表了8个最基础的简谱音符
【创客玩音乐】arduino极简电子琴图4
代码如下;
[mw_shl_code=applescript,false]/*This sketch was made by gada888
-------2020-03-06---------
-------Luoyang.China------
*/

//#include <SoftwareSerial.h>
//SoftwareSerial Serial1(10, 11);// RX, TX
#include <ADCTouch.h>

int ref0, ref1, ref2, ref3, ref4, ref5, ref6, ref7;
int threshold ;

unsigned char order[4] = {0xAA,0x06,0x00,0xB0};

void setup()
{
  int th = 550;

Serial.begin(9600);
  //Serial1.begin(9600);
  volume(0x1E);//音量设置0x00-0x1E
   ref0 = ADCTouch.read(A0, 500);     
   ref1 = ADCTouch.read(A1, 500);   
   ref2 = ADCTouch.read(A2, 500);     
   ref3 = ADCTouch.read(A3, 500);
   ref4 = ADCTouch.read(A4, 500);     
   ref5 = ADCTouch.read(A5, 500);
   ref6 = ADCTouch.read(A6, 500);
   ref7 = ADCTouch.read(A7, 500);

}

void loop()
{

int total1 = ADCTouch.read(A0,20);  
int total2 = ADCTouch.read(A1,20);
int total3 = ADCTouch.read(A2,20);  
int total4 = ADCTouch.read(A3,20);  
int total5 = ADCTouch.read(A4,20);  
int total6 = ADCTouch.read(A5,20);
int total7 = ADCTouch.read(A6,20);
int total8 = ADCTouch.read(A7,20);   

    total1 -= ref0;
    total2 -= ref1;
    total3 -= ref2;
    total4 -= ref3;
    total5 -= ref4;
    total6 -= ref5;
    total7 -= ref6;
    total8 -= ref7;
  
  if (total1 > 100 && total1  > threshold ) {
      play(0x01);//指定播放:0x01-文件0001
      delay(700);
Serial.println("o1");
  }

  if (total2 > 100 && total2 > threshold ) {
      play(0x02);//指定播放:0x01-文件0001
     delay(700);
  Serial.println("o2");
  }

  if (total3 > 100 && total3 > threshold ) {
      play(0x03);//指定播放:0x01-文件0001
      delay(700);
  Serial.println("o3");
  }

   if (total4 > 100 && total4 > threshold ) {
      play(0x04);//指定播放:0x01-文件0001
      delay(700);
  Serial.println("o4");
  }

  if (total5 > 100 && total5 > threshold ) {
      play(0x05);//指定播放:0x01-文件0001
      delay(700);
  Serial.println("o5");
  }

  if (total6 > 100 && total6 > threshold ) {
      play(0x06);//指定播放:0x01-文件0001
      delay(700);
  Serial.println("o6");
  }

  if (total7 > 100 && total7 > threshold ) {
      play(0x07);//指定播放:0x01-文件0001
      delay(700);
  Serial.println("o7");
  }

  if (total8 > 100 && total8 > threshold ) {
      play(0x08);//指定播放:0x01-文件0001
      delay(700);
  Serial.println("o8");
  }
// do nothing
delay(1);
}

void play(unsigned char Track)
{
unsigned char play[6] = {0xAA,0x07,0x02,0x00,Track,Track+0xB3};
Serial.write(play,6);
}

void volume( unsigned char vol)
{
unsigned char volume[5] = {0xAA,0x13,0x01,vol,vol+0xBE};
Serial.write(volume,5);
}[/mw_shl_code]

【创客玩音乐】arduino极简电子琴图5







TouchPiano_master.rar

249.88 KB, 下载次数: 2853

创作许可协议

本项目采用 None(不开放任何权利,保留所有权利) 进行许可。

评论(3)
星卡卡里
星卡卡里
这个可以实现混音功能吗
gada888
gada888
作者
回复星卡卡里
没有尝试过,应该有这个可能
dofanng
dofanng
用Arduino UNO板是不是也可以呀?
gada888
gada888
作者
回复dofanng
也可以,就是只能用6个模拟口。
帅猫
帅猫
下次电路图美观一点
- 没有更多了 -