7947| 5
|
[项目] 【创客玩音乐】arduino极简电子琴 |
本次实验的原理主要是利用Arduino的模拟触控ADTouch库,将接收到的模拟信号输出触发相应的mp3模块的mp3文件,从而使音响发出不同的声音。力求用最少的元件打造一个“电子琴”。 硬件清单: 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个最基础的简谱音符 代码如下; [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] |
249.88 KB, 下载次数: 1075
© 2013-2024 Comsenz Inc. Powered by Discuz! X3.4 Licensed