【创客玩音乐】arduino极简电子琴
本次实验的原理主要是利用arduino的模拟触控ADTouch库,将接收到的模拟信号输出触发相应的mp3模块的mp3文件,从而使音响发出不同的声音。力求用最少的元件打造一个“电子琴”。https://v.youku.com/v_show/id_XNDU3NTUyOTMyNA==.html?spm=a2hzp.8244740.0.0
硬件清单:
1.arduino mega2560
2.DF MP3模块
https://wiki.dfrobot.com.cn/index.php?title=(SKU:DFR0534)Voice_Module
3.一些电线
====================
把下载好的钢琴音阶mp3文件下载到mp3模块里,文件名以0开头。
连线图如下A0-A7是触控线,用来接收模拟信号,可以理解为代表了8个最基础的简谱音符
代码如下;
/*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 = {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 = {0xAA,0x07,0x02,0x00,Track,Track+0xB3};
Serial.write(play,6);
}
void volume( unsigned char vol)
{
unsigned char volume = {0xAA,0x13,0x01,vol,vol+0xBE};
Serial.write(volume,5);
}
下次电路图美观一点 用Arduino UNO板是不是也可以呀? dofanng 发表于 2020-5-29 10:57
用Arduino UNO板是不是也可以呀?
也可以,就是只能用6个模拟口。 这个可以实现混音功能吗
星卡卡里 发表于 2022-10-16 13:57
这个可以实现混音功能吗
没有尝试过,应该有这个可能
页:
[1]