Trouble with while loop + WAV trigger wTrig.isTrackPlaying(1)

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;
}


Looking at one of your while loops

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;
}

The value of OrangeSensor never changes once the while loop is entered so how will the while loop ever end ?

Hi @UKHeliBob thanks for your help. I don't quite understand what you mean - as I move my hand back and forth in front of the sensor the value changes. I've attached a screenshot of my serial monitor showing that as i move back and forth it tells me the value is changing.

image

Why use a while with a break, when a simple if would suffice?

Silly me for not noticing the unconditional break in the while loop

If you are going to do that then, as asked above, why use a while loop ?

@anon56112670 many thanks for your reply.
@UKHeliBob

I used the while loop with the hope that I could include a clause that would mean that the track was started and would play continuously until the condition no longer returned true. At the moment using either an if loop or a while loop the track start again and again extremely quickly as the code loops.

I started out with an if loop and had the same problem.

The primary issue is that I cannot seem to create a working condition that will allow the track to play as long as someone is standing in the same spot. The values are continiously read and the trigger to play the song is re-triggered.

I have used a delay that is the same as the track length. That does prevent the song from starting again and again. As it's a delay however, it just .. delays moving through the code. So the song doesn't stop if you move out of range.

A while loop with an unconditional break is exactly equivalent to an if (without an else clause)

A while loop without an update of the controlling expression within it can never end

Ok great, thanks for the info, now that you pointed it out it feel quite obvious :woman_facepalming:

In this instance is the controlling expression; (OrangeSensor > 11) && (OrangeSensor < 20) ?

If that is the case it does update. I want it to keep reading the distance, I just want it to ignore the request to start playing the track again if two things are true - 1. the person is standing in the 'small' zone (between 11-20 in this case) and 2. the track is not playing.

I tried to write the following but it doesn't work;

while((OrangeSensor > 11) && (OrangeSensor < 20) && (wTrig.isTrackPlaying(2)) ) 

It only updates because of the break.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.