Home > Other >Special Application Circuits > How to use the MP3 player from CATALEX

How to use the MP3 player from CATALEX

Source: InternetPublisher:狂妄火龙果 Keywords: Sensor player MP3 Updated: 2024/08/06

How to use the MP3 player from CATALEX

Only one library is needed (#include 《SoftwareSerial.h》) and there are some simple functions:

How to use HC-SR04 within the specified range

Inside, it's easy to use.

How to use them both Your version 1: simple mp3 if simple

If you don't have the library yet, please install it first (SoftwareSerial.h).

In this version of the code, if something gets close to 50, the mp3 audio is triggered.

NOTE: The complete .ino code is in the project attachment below.

How to use version 2 Similar to Disturbance mp3

This version does not affect the detection distance, only the difference between scans. If the error triggers suddenly, if (gap > 20) {. We need to declare the new variable outside the loop at the same time.

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){//needed for stability,
auxDistance=Distance;
gap=0;
//only do this for the first time after playing a song to avoid first loop glitch
firstTime++;
delay(1000);
}
if(gap>20){ //if the distance changed by 20cm
sendCommand(CMD_PLAY_WITHFOLDER, 0X0201);//play the first song of the second folder
firstTime=0;//avoid errors!! We don't 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 of if() for the next loop
}

How to use them version 3: two distance sensors

Using two distance sensors, you can actually infer the direction of movement, so I made a program that can tell "hello" or "goodbye" based on the person's movement.

First, we'll digitize two more controls to control the second HC-SR04:

New variables!

In settings() we add new settings.

We can now get parameters from functions, change parameters.

In our loop():

void loop()
{
Distance=measureDistance(trigPin,echoPin);//Measure distance 1 and store
Distance2=measureDistance(trigPin2,echoPin2);//Measure distance 2 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){//Necessary stuff for stability
auxDistance=Distance;
auxDistance2=Distance2;
gap=0;
gap2=0;
//Only do this the first time after playing a song to avoid first loop glitch
firstTime++;
delay(2000);
}
if(gap>20 and gap2<20 ){ //If distance changed by 20cm 
sendCommand(CMD_PLAY_WITHFOLDER, 0X0201);//Play the first song of the second folder
firstTime=0;//Avoid bugs! ! We don't like errors
Serial.println("RIGHT MOVEMENT DETECTED");
delay(2000);
}
if(gap2>20 and gap<20){ //If the distance change is 20cm 
sendCommand(CMD_PLAY_WITHFOLDER, 0X0202);//Play the second song in the second folder
firstTime=0;//Avoid errors ! ! We don't 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.
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 of if() for the next loop
auxDistance2=Distance2;//store the value of if() for the next loop
}
Disturbance2=0;
Left=0;
Right=0;
delay(1000);//wait to avoid error
}
delay(300);
auxDistance=Distance;
auxDistance2=Distance2;//store the value of if() for the next loop
}

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号