My previous tutorial about it, only one library needed (#include <SoftwareSerial.h>
), and some easy function already explained in the previous tutorial:
void sendCommand(int8_t command, int16_t dat)
{
if (command==CMD_PLAY_WITHFOLDER or command==CMD_PLAY_WITHVOLUME){Serial.print("PLAYING SONG, SLIGHTLY MOVE YOUR HEAD FOR GREAT EFFECT");}
delay(20);
Send_buf[0] = 0x7e; //starting byte
Send_buf[1] = 0xff; //version
Send_buf[2] = 0x06; //the number of bytes of the command without starting byte and ending byte
Send_buf[3] = command; //
Send_buf[4] = 0x00;//0x00 = no feedback, 0x01 = feedback
Send_buf[5] = (int8_t)(dat >> 8);//datah
Send_buf[6] = (int8_t)(dat); //datal
Send_buf[7] = 0xef; //ending byte
for(uint8_t i=0; i<8; i++)//
{
mySerial.write(Send_buf[i]) ;//send bit to serial mp3
}
Serial.println();
}
My previous tutorial about it, no library needed, easy to use anyway. I just took my previous hc-sr04 post and put it in this function:
long measureDistance(){
long duration, distance;
digitalWrite(trigPin, LOW); //PULSE ___|---|___
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
Serial.println("distance:");
Serial.println(distance);
return distance; //returns distance in cm
}
First install the library if you don't have it yet (<SoftwareSerial.h>).
In this version of code, if something comes closer than 50cm then it triggers the mp3 audio. I used it to scare my roommates with a very loud "surprise motherfucker" clip.
Note: Full .ino
code is in the Project Attachments below.
void loop()
{
if(measureDistance()<50){
sendCommand(CMD_PLAY_WITHFOLDER, 0X0203);//play the third song of the second folder
delay(1000);//wait to avoid errors
}
delay(300);
}
How to use both Version 2 and Disturbance mp3 distance triggerThis version doesn't care about the distance, only detects differences between readings. If triggers suddenly by error put a higher value in if(gap>20){.......
we need to declare new variables outside the loop. I did it at the beginning of the code.
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
}
How to use them both Version 3: Two Distance SensorsWith two distance sensors you can actually guess the direction of movement so I did a program which says "hello
" or "bye
" depending of the person´s direction of movement.
- First we define another two DIGITAL pins for controlling the second HC-SR04:
#define trigPin 13//for the FIRST distance module
#define echoPin 12
#define trigPin2 10//for the SECOND distance module
#define echoPin2 9
- New variables! Yeah!
long Distance,auxDistance,gap=0;
long Distance2,auxDistance2,gap2=0; //new variables
- In the
setup()
we add our new pins declaration.
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(trigPin2, OUTPUT);
pinMode(echoPin2, INPUT);
- We change the
measureDistance()
function, now the function reads the pins from the arguments.
long measureDistance(int trigger,int echo){
long duration, distance;
digitalWrite(trigger, LOW); //PULSE ___|---|___
delayMicroseconds(2);
digitalWrite(trigger, HIGH);
delayMicroseconds(10);
digitalWrite(trigger, LOW);
duration = pulseIn(echo, HIGH);
distance = (duration/2) / 29.1;
Serial.println("distance:");
Serial.println(distance);
return distance;
}
- In our
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
}
Happy hacking, fellas!
Comments