2022-7-27 19:20:08 [显示全部楼层]
2798浏览
查看: 2798|回复: 0

触控-拟人化的发音胡萝卜

[复制链接]
本帖最后由 gada888 于 2022-7-28 16:40 编辑

突然心血来潮.想出一个入门级的Arduino 触控教程.给新手一点创新思路. 思来想去想到了拟人化一个常用的食物.这次胡萝卜被选中了.因为要按上眼睛,胡萝卜皮硬,比较容易被粘上.用到的一个DF的MP3模块和一个触控ADCTouch库来完成任务.
触控-拟人化的发音胡萝卜图1

下面是实物接线图
触控-拟人化的发音胡萝卜图2
注意:电源输入是4.2V.直接接入到UNO的5V接线口.原因是电压提示后电流也变大.从而影响了MP3喇叭的音质.只有4.2V或以下才能听不到噪音.但供电电压再小.音量会越来越小.
硬件清单
品名 数量 链接
arduino UNO 1 https://www.dfrobot.com.cn/goods-521.html
MP3模块 1 https://www.dfrobot.com.cn/goods-1752.html
电源 1 https://www.dfrobot.com.cn/goods-3395.html
杜邦线 1
鳄鱼嘴夹子 3 https://www.dfrobot.com.cn/goods-429.html
胡萝卜 1
水果刀11
纽扣 2

ADCTouch库是用arduino的模拟脚位来设计触控功能的,它用了AVR芯片的内部功能类测量外部物体的电容值.代码中的ref0 = ADCTouch.read(A0, 500);  这个阈值500可以调整的.实测这个值是在通电后并在人手碰到胡萝卜后,MP3模块就会发出声音.

代码中的int ref0, ref1, ref2, ref3, ref4, ref5;这个设定是指定变量,用来给A0-A6六个模拟脚位赋值.实际我只用了A0一个模拟口.当然如果需要.可以连更多的线.用来发不同的声音文件.

代码中的SoftwareSerial Serial1(10, 11);.指为MP3模块的串口线设定为软串口.并把D10给TX,D1给RX.

MP3文件是通过下面的软件生成的
触控-拟人化的发音胡萝卜图3
而生成的MP3文件会拷贝到DF MP3模块里面.方式是通过连接USB线到MP3模块.这个MP3模块有个内置存储,不过存不了大的MP3文件.MP3文件名的开头必须是000+数字

触控-拟人化的发音胡萝卜图4
下面是烧录成功的代码
触控-拟人化的发音胡萝卜图5
  1. #include <SoftwareSerial.h>
  2. #include <ADCTouch.h>
  3. SoftwareSerial Serial1(10, 11);
  4. unsigned char order[4] = {0xAA,0x06,0x00,0xB0};
  5. int ref0, ref1, ref2, ref3, ref4, ref5;
  6. int th ;
  7. void setup()
  8. {
  9.   int th = 550;
  10.   //Serial.begin(9600);
  11.   Serial1.begin(9600);
  12.   volume(0x1E);//音量设置0x00-0x1E
  13.   Serial1.begin(9600);
  14.   
  15.    ref0 = ADCTouch.read(A0, 500);     
  16.    ref1 = ADCTouch.read(A1, 500);   
  17.    ref2 = ADCTouch.read(A2, 500);     
  18.    ref3 = ADCTouch.read(A3, 500);
  19.    ref4 = ADCTouch.read(A4, 500);     
  20.    ref5 = ADCTouch.read(A5, 500);
  21. }
  22. void loop()
  23. {
  24. int total1 = ADCTouch.read(A0,20);  
  25. int total2 = ADCTouch.read(A1,20);
  26. int total3 = ADCTouch.read(A2,20);  
  27. int total4 = ADCTouch.read(A3,20);  
  28. int total5 = ADCTouch.read(A4,20);  
  29. int total6 = ADCTouch.read(A5,20);   
  30.     total1 -= ref0;
  31.     total2 -= ref1;
  32.     total3 -= ref2;
  33.     total4 -= ref3;
  34.     total5 -= ref4;
  35.     total6 -= ref5;
  36.   
  37.   if (total1 > 100 && total1  > th ) {
  38.   play(0x01);
  39. // Serial.println("o1");
  40.   }
  41.   if (total2 > 100 && total2 > th ) {
  42.   play(0x02);
  43.   //Serial.println("o2");
  44.   }
  45.   if (total3 > 100 && total3 > th ) {
  46.   play(0x03);
  47.   //Serial.println("o3");
  48.   }
  49.    if (total4 > 100 && total4 > th ) {
  50.   play(0x04);
  51.   //Serial.println("o4");
  52.   }
  53.   if (total5 > 100 && total5 > th ) {
  54.   play(0x05);
  55.   //Serial.println("o5");
  56.   }
  57.   if (total6 > 100 && total6 > th ) {
  58.   play(0x06);
  59.   //Serial.println("o6");
  60.   }
  61. // do nothing
  62. delay(1);
  63. }
  64. void play(unsigned char Track)
  65. {
  66. unsigned char play[6] = {0xAA,0x07,0x02,0x00,Track,Track+0xB3};//0xB3=0xAA+0x07+0x02+0x00,即最后一位为校验和
  67. Serial1.write(play,6);
  68. }
  69. void volume( unsigned char vol)
  70. {
  71. unsigned char volume[5] = {0xAA,0x13,0x01,vol,vol+0xBE};//0xBE=0xAA+0x13+0x01,即最后一位为校验和
  72. Serial1.write(volume,5);
  73. }
复制代码


再接下来是接线图
触控-拟人化的发音胡萝卜图6






您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

为本项目制作心愿单
购买心愿单
心愿单 编辑
[[wsData.name]]

硬件清单

  • [[d.name]]
btnicon
我也要做!
点击进入购买页面
上海智位机器人股份有限公司 沪ICP备09038501号-4

© 2013-2024 Comsenz Inc. Powered by Discuz! X3.4 Licensed

mail