Wav trigger with ultrasonic sensors

Hi there,
I have 3 ultrasonic sensors set up on an arduino uno that are working as expected. In each of my if loops, I would like to play a track.

if (OrangeSensor < 39) {
    Serial.print("orange - small ");
    Serial.print(OrangeSensor);
    Serial.println();
    //wTrig.trackPlayPoly(01);               // Start Track 16 poly
    //delay(10000);
    //digitalWrite(play, LOW);  
  } 

I am currently struggling to connect the WAV trigger correctly - I am using different bits of code found from different forums

  • the libraries have been included correctly
  • the wav board is connect to a 12v power supply
  • it's grounded
  • TX is in pin 9

Questions

  • there are currently 3 tracks on the SD card names 0001.wav, 0002.wav and 0003.wav. I am unsure where to define these
  • with the code I have I would expect that on startup I can at least hear the 1st track
    using; wTrig.trackPlayPoly(01); I cannot
  • do I need to code in that the TX pin is connected to digital 9?

Many thanks in advance

//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 


wavTrigger wTrig;             // Our WAV Trigger object

//ORANGE 
#define trigPin1 7
#define echoPin1 6

//PINK
#define trigPin2 11
#define echoPin2 12

//BLUE
#define trigPin3 8
#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);
  
  // 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); 

  Serial.print("we've started");
  wTrig.trackPlayPoly(01); 
  delay(100);
  //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;


  //Serial.print("Orange - " + String(OrangeSensor));
  /*Serial.print("Orange Sensor");
  Serial.print(" - ");
  Serial.println(OrangeSensor);

  Serial.print("Pink Sensor");
  Serial.print(" - ");
  Serial.println(PinkSensor);

  Serial.println("===========");*/




//orange loop
if (OrangeSensor < 39) {
    Serial.print("orange - small ");
    Serial.print(OrangeSensor);
    Serial.println();
    //wTrig.trackPlayPoly(01);               // Start Track 16 poly
    //delay(10000);
    //digitalWrite(play, LOW);  
  } else if ((OrangeSensor > 40) && (OrangeSensor < 99)) {
    Serial.print("orange - medium ");
    Serial.print(OrangeSensor);
    Serial.println();
    //digitalWrite(play, LOW);       
  } else if ((OrangeSensor > 100) && (OrangeSensor < 150)) {
    Serial.print("orange - large");
    Serial.print(OrangeSensor);
    Serial.println();
    //digitalWrite(play, LOW);        // dont play recording 
  }
// pink loop
if (PinkSensor < 39) {
    Serial.print("pink - small ");
    Serial.print(PinkSensor);
    Serial.println();
    //digitalWrite(play, LOW);  
  } else if ((PinkSensor > 40) && (PinkSensor < 99)) {
    Serial.print("Pink - medium ");
    Serial.print(PinkSensor);
    Serial.println();
    //digitalWrite(play, LOW);       
  } else if ((PinkSensor > 100) && (PinkSensor < 150)) {
    Serial.print("Pink - large");
    Serial.print(PinkSensor);
    Serial.println();
    //digitalWrite(play, LOW);        // dont play recording 
  }

  //blue loop
if (BlueSensor < 39) {
    Serial.print("blue - small ");
    Serial.print(BlueSensor);
    Serial.println();
    //digitalWrite(play, LOW);  
  } else if ((BlueSensor > 40) && (BlueSensor < 99)) {
    Serial.print("Pink - medium ");
    Serial.print(BlueSensor);
    Serial.println();
    //digitalWrite(play, LOW);       
  } else if ((BlueSensor > 100) && (BlueSensor < 150)) {
    Serial.print("Blue - large");
    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;
}

I don't know how the code you posted gets to be repeated so often, but I wish it would stop.

@anon56112670 I found it repeated a few times in beginner projects with the ultrasonic sensors which is why it's possibly so common.

Do you know if it will impact the usability of the wav trigger? I don't see why it would but perhaps that's why you flagged it?

I flagged it because it is lazy for several reasons.
I aim to stamp it out.