亲,“电路城”已合并升级到更全、更大、更强的「新与非网」。点击查看「新与非网」

本网页已闲置超过3分钟,按键盘任意键或点击空白处,即可回到网页

基于距离传感器的 MP3 播放器

发布时间:2021-11-30
分享到:

基于距离传感器的 MP3 播放器

发布时间:2021-11-30
分享到:

如何使用 CATALEX 的串行 Mp3 播放器

只需要一个库 ( #include <SoftwareSerial.h>),并且一些简单的函数:

如何使用 HC-SR04 超声波范围
不需要库,反正很容易使用。我刚刚把我之前的 hc-sr04 帖子放在这个函数中:

如何同时使用它们 版本 1:简单的 mp3 距离触发器
如果您还没有该库,请先安装它(<SoftwareSerial.h>)。

在这个版本的代码中,如果有东西接近 50 厘米,那么它会触发 mp3 音频。我用它用一个非常响亮的“惊喜混蛋”剪辑来吓唬我的室友。

注意:完整.ino 代码在下面的项目附件中。

如何同时使用版本 2 和 Disturbance mp3 距离触发器
此版本不关心距离,只检测读数之间的差异。如果错误突然触发,if(gap>20){....... 我们需要在循环外声明新变量。我在代码的开头这样做了。

int firstTime=0;//we need to declare firstTime outside the loop  
long Distance,auxDistance,gap=0; 
void loop()  
{  
Distance=measureDistance(trigPin,echoPin);//measure distance and store 
gap=abs(Distance-auxDistance);// calculate the difference between now and last reading 
if(firstTime==0){//necesary for stability things 
auxDistance=Distance; 
gap=0; 
//does it only the first time after play a song to avoid first loop malfuntcion 
firstTime++;  
delay(1000); 

if(gap>20){ //if distance variation is 20cm 
sendCommand(CMD_PLAY_WITHFOLDER, 0X0201);//play the first song of the second folder  
firstTime=0;//avoid errors!!we dont like errors 
delay(2000);  
}  
Serial.print("New Distace:");//debugggggg 
Serial.print(Distance); 
Serial.print("  Old Distance: "); 
Serial.print(auxDistance); 
Serial.println(gap); 
delay(300);  
auxDistance=Distance;//store the value for the if() in the next loop 

如何同时使用它们 版本 3:两个距离传感器
使用两个距离传感器,您实际上可以猜测运动的方向,因此我做了一个程序, 根据人的运动方向显示“ hello ”或“ bye”。

  • 首先,我们定义另外两个 DIGITAL 引脚来控制第二个 HC-SR04:

  • 新变量!

  • 在setup()我们添加新的引脚声明。

  • 我们更改measureDistance()函数,现在函数从参数中读取引脚。

  • 在我们的 loop():

void loop()  
{  
Distance=measureDistance(trigPin,echoPin);//measure distance1 and store 
Distance2=measureDistance(trigPin2,echoPin2);//measure distance2 and store 
gap=abs(Distance-auxDistance);// calculate the difference between now and last reading 
gap2=abs(Distance2-auxDistance2);// calculate the difference between now and last reading 
if(firstTime==0){//necesary for stability things 
auxDistance=Distance; 
auxDistance2=Distance2; 
gap=0; 
gap2=0; 
//does it only the first time after play a song to avoid first loop malfuntcion 
firstTime++;  
delay(2000); 

if(gap>20 and gap2<20 ){ //if distance variation is 20cm 
sendCommand(CMD_PLAY_WITHFOLDER, 0X0201);//play the first song of the second folder  
firstTime=0;//avoid errors!!we dont like errors 
Serial.println("RIGHT MOVEMENT DETECTED"); 
delay(2000);  
}  
if(gap2>20 and gap<20){ //if distance variation is 20cm 
sendCommand(CMD_PLAY_WITHFOLDER, 0X0202);//play the second song of the second folder  
firstTime=0;//avoid errors!!we dont like errors 
Serial.println("LEFT MOVEMENT DETECTED"); 
delay(2000);  
}  
Serial.println("\\\\\\\\\\\\\\\\\\\\\\");//debugggggg 
Serial.print(" New Distace:");//debugggggg 
Serial.print(Distance); 
Serial.print("   Old Distance: "); 
Serial.print(auxDistance); 
Serial.print("   GAP "); 
Serial.println(gap); 
Serial.print("New Distace2:");//debugggggg 
Serial.print(Distance); 
Serial.print("  Old Distance2: "); 
Serial.print(auxDistance); 
Serial.print("  GAP2 "); 
Serial.println(gap); 
Serial.println("\\\\\\\\\\\\\\\\\\\\\\");//debugggggg 
delay(300);  
auxDistance=Distance;//store the value for the if() in the next loop 
auxDistance2=Distance2;//store the value for the if() in the next loop 

Disturbance2=0;
left=0;
right=0;
delay(1000);//wait to avoid errors 

delay(300); 
auxDistance=Distance;//store the value for the if() in the next loop
auxDistance2=Distance2;//store the value for the if() in the next loop

如果您对此项目有任何想法、意见或问题,请在下方留言。

原文链接丨以上内容来源网络,如涉及侵权可联系删除。

加入微信技术交流群

技术交流,职业进阶

关注与非网服务号

获取电子工程师福利

加入电路城 QQ 交流群

与技术大牛交朋友

讨论