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
}
- How to Make a Pet Robot
- How to build an autonomous robot using the DonkeyCar platform
- Satellite TV Set Top Box Remote Controller
- Low voltage electronic self-defense baton
- Automatic recovery electronic fuse with soft start function
- Electronic fly killer
- Ultrasonic dog chaser made by NE555
- Production of electric water bottle energy saver
- Bookcase moth-proofing circuit made with 7556 dual-time base circuit
- How to use ultrasonic sensors to make a simple nucleic acid sampling machine
- Differential temperature sensor
- Humidity detector circuit
- Digital Strain Gauge Measurement Circuit
- AD7710 internal functional block diagram and pin function diagram
- XTR105 internal circuit function diagram
- Color sensor amplification circuit
- LCD boost circuit for MP3 player
- Inductive anti-theft alarm circuit 2 using RD627 sensor
- Sensor lighting LED driver circuit
- MP3 circuit diagram_torque solution 1