Problem with jumpy readings from HC-SR04 proximity sensor to trigger mp3s

Hello,

I’m using the New Ping library with a HC-SR04 ultrasonic sensor, to control an mp3 from a Bare Conductive touch board which runs on the Leonardo (although it uses pretty much the same libraries as the ones that Bill Porter made for the Sparkfun mp3 shield)... Phew!

I am trying to trigger the track (which is one second long) when an object moves in front of the sensor, and have it play only once (and not repeat over and over) until the object moves away from the sensor’s range.

The sketch setup is based on Bare Conductive’s Touch_MP3 sketch - I know it’s an awkward way to do things but I’m prototyping until an mp3 shield arrives, however I doubt it will make a difference to my problem.

My issue is that while the sensor gives me good readings at short ranges, at wider ranges (starting around 40cm from the sensor and getting worse as the distance increases) the Serial Monitor starts giving random high readings, sporadically adding a high value in between the accurate ones.

These readings mean that the mp3 triggers as it should, then stops and starts when the readings become jumpy.

I’m using a large piece of cardboard to test the sensor at wider ranges for stability but to no avail.

Eg:

66cm
65cm
112cm
66cm
67cm
67cm
67cm
67cm
66cm
66cm
118cm
66cm
65cm
65cm
66cm
125cm
65cm
65cm

etc…

Here is my code:

#include <NewPing.h>

boolean trackhasplayed;

int led = 13;

#define TRIGGER_PIN  4
#define ECHO_PIN     3
#define MAX_DISTANCE 200


NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);


// mp3 includes
#include <SPI.h>
#include <SdFat.h>
#include <SdFatUtil.h>
#include <SFEMP3Shield.h>

// mp3 variables
SFEMP3Shield MP3player;
byte result;

//sd card
SdFat sd;


void setup() {
  Serial.begin(115200);

  pinMode(led, OUTPUT);

  if (!sd.begin(SD_SEL, SPI_HALF_SPEED)) sd.initErrorHalt();

  result = MP3player.begin();
  MP3player.setVolume(5, 5);
  
}

void loop() {

  delay(50);
  int uS = sonar.ping();
 // Serial.print("Ping: ");
  Serial.print(uS / US_ROUNDTRIP_CM);
  Serial.println("cm");



//if statements

 if ((uS / US_ROUNDTRIP_CM < 80) && (trackhasplayed == false)) { //if cm3 is less than 80
 digitalWrite (led, HIGH);
 trackhasplayed = true;
MP3player.playTrack(1);

while (trackhasplayed == true){
  break;
  }
 }

if ((uS / US_ROUNDTRIP_CM > 80) && (trackhasplayed == true)) { 
  
trackhasplayed = false;
digitalWrite (led, LOW);
MP3player.stopTrack();
 }
 
}

I do not get the funny readings when using the NewPing example sketch - things appear much smoother. I have a feeling that the break in my while loop is causing these readings however I might be wrong. If I remove the break then the Serial Monitor gets blocked and I can’t exit the while loop when the object moves away from the sensor.

If anyone could shed some light on why these readings are being so jumpy (but seemingly following a pattern) then it would be amazing. Also, if there’s any better way of doing this then any help would be much appreciated.

Thanks so much in advance :slight_smile: