I am using a ultrasonic sensor to detect where someone is standing. Depending on how close they are standing I want to trigger a different track. I have divided the space in front of the sensor up into 3 sections (small, medium and large). The code I have successfully stops a track playing when the person moves from one section to another.
However, I cannot get the while loop to work. I want to make it work so that if a person stands in the section it plays a track, and when they leave the section it stops playing. At the moment the track continuously starts the first few seconds. I tried to create a while loop and use wTrig.isTrackPlaying(1)
so that when the first track is playing it exits the loop and doesn't restart the trigger to play. It isn't working. It also doesn't even print Serial.print('leave track to play jim!' I have tried using break and return to exit the loop but to no avail.
I would be grateful for any help on how to get the track to be played until the person exits the designated area.
The code below is the code I am working on. I have only tried to add the if statement to the first section.
//orange loop
while (OrangeSensor < 10) {
if (wTrig.isTrackPlaying(1)){
Serial.print('leave track to play jim!');
//break;
return 0;
}
Serial.print("orange - small- 0007 ");
Serial.print(OrangeSensor);
Serial.println();
//play correct track
wTrig.trackPlayPoly(1); // start track
delay(1000);// add in delay to give a chance to move to another section
//pause all other tracks
wTrig.trackPause(2);
wTrig.trackPause(3);
break;
}
while((OrangeSensor > 11) && (OrangeSensor < 20)) {
Serial.print("orange - medium - 0004 ");
Serial.print(OrangeSensor);
Serial.println();
//play correct
wTrig.trackPlayPoly(2);
delay(1000);// add in delay to give a chance to move to another segment
//pause all other tracks
wTrig.trackPause(1);
wTrig.trackPause(3);
break;
}
while((OrangeSensor > 21) && (OrangeSensor < 30)) {
Serial.print("orange - large - 0001 ");
Serial.print(OrangeSensor);
Serial.println();
wTrig.trackPlayPoly(3);
//delay(1000);// add in delay to give a chance to move to another segment
//pause all other tracks
wTrig.trackPause(1);
wTrig.trackPause(2);
break;
}
The rest of the code is pasted below. I have commented out large section of it so I can try to isolate the issue with the repeated triggering of the start. In the final project I have 3 sensors.
//https://www.theengineeringprojects.com/2015/02/interfacing-multiple-ultrasonic-sensor-arduino.html
//sucessfully added second sensor - one sensor controls lights, the other sound (and light)
//attempting to add WAV trigger
// to add libraries from github; navigate to folder and use git clone on master branch
#include <AltSoftSerial.h> // Arduino build environment requires this
#include <wavTrigger.h>
#include <Metro.h> // no idea
//uno wav trigger
//GND <------> GND
//Pin9 <------> RX
wavTrigger wTrig; // Our WAV Trigger object
char gWTrigVersion[VERSION_STRING_LEN];
//ORANGE
#define trigPin1 7
#define echoPin1 6
//PINK
#define trigPin2 10
#define echoPin2 12
//BLUE
#define trigPin3 11
#define echoPin3 5
long duration, distance, OrangeSensor,PinkSensor, BlueSensor;
void setup()
{
Serial.begin (9600);
// If the Arduino is powering the WAV Trigger, we should wait for the WAV
// Trigger to finish reset before trying to send commands.
//delay(1000);
//WAV Trigger startup at 57600
wTrig.start();
delay(10);
if (wTrig.getVersion(gWTrigVersion, VERSION_STRING_LEN)) {
Serial.print("HELLO FROM WAV");
Serial.print(wTrig.getNumTracks());
}
else {
Serial.print("Something's wrong");
}
// Send a stop-all command and reset the sample-rate offset, in case we have
// reset while the WAV Trigger was already playing.
wTrig.stopAllTracks();
wTrig.samplerateOffset(0);
//enable reporting
wTrig.setReporting(true);
delay(100);
Serial.print("we've started");
//delay(1000);
wTrig.trackLoad(1);
delay(10);
//wTrig.trackPlaySolo(1);
//delay(10000);
//Serial.print(BlueSensor);
//set orange sensor
pinMode(trigPin1, OUTPUT);
pinMode(echoPin1, INPUT);
//set pink sensor
pinMode(trigPin2, OUTPUT);
pinMode(echoPin2, INPUT);
//set blue sensor
pinMode(trigPin3, OUTPUT);
pinMode(echoPin3, INPUT);
}
void loop() {
//orange sensor
SonarSensor(trigPin1, echoPin1);
OrangeSensor = distance;
//pink sensor
SonarSensor(trigPin2, echoPin2);
PinkSensor = distance;
//blue sensor
SonarSensor(trigPin3, echoPin3);
BlueSensor = distance;
//orange loop
while (OrangeSensor < 10) {
if (wTrig.isTrackPlaying(1)){
Serial.print('leave track to play jim!');
//break;
return 0;
}
Serial.print("orange - small- 0007 ");
Serial.print(OrangeSensor);
Serial.println();
//play correct track
wTrig.trackPlayPoly(1); // start track
delay(1000);// add in delay to give a chance to move to another section
//pause all other tracks
wTrig.trackPause(2);
wTrig.trackPause(3);
break;
}
while((OrangeSensor > 11) && (OrangeSensor < 20)) {
Serial.print("orange - medium - 0004 ");
Serial.print(OrangeSensor);
Serial.println();
//play correct
wTrig.trackPlayPoly(2);
delay(1000);// add in delay to give a chance to move to another segment
//pause all other tracks
wTrig.trackPause(1);
wTrig.trackPause(3);
break;
}
while((OrangeSensor > 21) && (OrangeSensor < 30)) {
Serial.print("orange - large - 0001 ");
Serial.print(OrangeSensor);
Serial.println();
wTrig.trackPlayPoly(3);
//delay(1000);// add in delay to give a chance to move to another segment
//pause all other tracks
wTrig.trackPause(1);
wTrig.trackPause(2);
break;
}
// pink loop
if (PinkSensor < 39) {
//Serial.print("pink - small - 0008 ");
//Serial.print(PinkSensor);
// Serial.println();
//digitalWrite(play, LOW);
} else if ((PinkSensor > 40) && (PinkSensor < 99)) {
/*Serial.print("Pink - medium - 0005 ");
Serial.print(PinkSensor);
Serial.println();
//digitalWrite(play, LOW); */
} else if ((PinkSensor > 100) && (PinkSensor < 150)) {
/*Serial.print("Pink - large - 0002 ");
Serial.print(PinkSensor);
Serial.println();*/
//digitalWrite(play, LOW); // dont play recording
}
//blue loop
if (BlueSensor < 39) {
/*Serial.print("blue - small - 0009 ");
Serial.print(BlueSensor);
Serial.println();
//digitalWrite(play, LOW); */
} else if ((BlueSensor > 40) && (BlueSensor < 99)) {
/*Serial.print("Pink - medium - 0006 ");
Serial.print(BlueSensor);
Serial.println();
//digitalWrite(play, LOW); */
} else if ((BlueSensor > 100) && (BlueSensor < 150)) {
/*Serial.print("Blue - large - 0003 ");
Serial.print(BlueSensor);
Serial.println();*/
//digitalWrite(play, LOW); // dont play recording
}
//no idea why this doesn't work
else if ((OrangeSensor >151) && (PinkSensor >151) ) {
//placed here to pevent LEDs constantly flickering
Serial.print('no one is within the paremeters ');
Serial.println();
//digitalWrite(play, LOW); // player off
}
}
void SonarSensor(int trigPin,int echoPin)
{
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
}