Using vs1053 Music Maker to create a detecting sensor

Hi,
I'm quite new at programming and during the last month I've been carrying a project which intends to create a device that helps blind people.

To do this I want an ultrasonic sensor (HR-SR04) to detect an object and when it does to play an information through the headset.

To do this I have programmed this:

// include SPI, MP3 and SD libraries
#include <SPI.h>
#include <Adafruit_VS1053.h>
#include <SD.h>

// These are the pins used for the music maker shield
#define SHIELD_RESET  -1      // VS1053 reset pin (unused!)
#define SHIELD_CS     7      // VS1053 chip select pin (output)
#define SHIELD_DCS    6      // VS1053 Data/command select pin (output)

// These are common pins between breakout and shield
#define CARDCS 4     // Card chip select pin
// DREQ should be an Int pin, see http://arduino.cc/en/Reference/attachInterrupt
#define DREQ 3       // VS1053 Data request, ideally an Interrupt pin

int trig = 5; // trig is the one that sends the wave
int echo = 2; // echo is the one that registers the time
int duration;
int distance;

Adafruit_VS1053_FilePlayer musicPlayer = 
Adafruit_VS1053_FilePlayer(SHIELD_RESET, SHIELD_CS, SHIELD_DCS, DREQ, CARDCS);

void setup() {
  Serial.begin(9600);
  Serial.println("Start test");
  //pinMode(trig, OUTPUT); // trig is an output has it sends a signal
  //pinMode(echo, INPUT); // echo is an input has it recives information
  //digitalWrite(trig, LOW);

  if (! musicPlayer.begin()) { // initialise the music player
     Serial.println(F("Couldn't find VS1053, do you have the right pins defined?"));
     while (1);
  }
  Serial.println(F("VS1053 found"));
  
   if (!SD.begin(CARDCS)) {
    Serial.println(F("SD failed, or not present"));
    while (1);  // don't do anything more
  }
  
  // Set volume for left, right channels. lower numbers == louder volume!
  musicPlayer.setVolume(20,20);

  // If DREQ is on an interrupt pin (on uno, #2 or #3) we can do background
  // audio playing
  musicPlayer.useInterrupt(VS1053_FILEPLAYER_PIN_INT);  // DREQ int
  
}

void loop() {
  digitalWrite(trig, HIGH);
  delayMicroseconds(10);
  digitalWrite(trig, LOW);
  // This code above sends a the "wave"

  duration = pulseIn(echo, HIGH);
  distance = duration * 0.034/2;
  // This one determines the distance based on a simple calculus of how many cm travels the sound time each microsecond
  
  Serial.print("distance: ");
  Serial.println(distance);

  delay(distance*5);

  if(distance >= 100){
    Serial.println(F("Playing track 002"));
    musicPlayer.playFullFile("/track001.mp3");
  }

}

However, the distance variable keeps giving me 0. Both programms the music one and the ultrasonic one are tested and work separate. But when I get them togheter it doesn't work. I would like to know why this is happening and how to solve it.
Thanks :grinning:

@redhotdaddy
I've seen in your post Adafruit Music Maker Shield with Ping))) ultrasonic sensor for halloween show. You had the same problem, were you able to solve it?

Two problems! First, you do not set a time-out, so nothing happens for one second and then zero is returned.

Second problem, pulseln() does not return an int.

Can't really undestand it. Why do I need to set a time-out?

And in the second problem, setting duration as an unsigned long should be enough?

Thanks, by the way

To keep your program responsive as possible. Just stopping for 1 second each time you execute the pulseln() makes for a vary slow program.

duration = pulseIn(echo, HIGH, 1000000);
distance = duration * 0.034/2;

Is it good if I change my code to the above?

Are you unable to test it?

I've test it and it continues to give me 0...

So, reduce the time-out value to a reasonable value. What are you using for a reflector during your tests?

I've set the time out to 10^6 which equals 1 sec.
I'm using all kind of reflectors, I've tried, a box, my hand, a closet, etc...

I've accomplished to make it sound, don't really know why, but the code below works for me.

#include <NewPing.h>

#define TRIGGER_PIN A0
#define ECHO_PIN 9
#define MAX_DISTANCE 200

NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);

int distance;
int q = 1;

// include SPI, MP3 and SD libraries
#include <SPI.h>
#include <Adafruit_VS1053.h>
#include <SD.h>

// These are the pins used for the music maker shield
#define SHIELD_RESET  -1      // VS1053 reset pin (unused!)
#define SHIELD_CS     7      // VS1053 chip select pin (output)
#define SHIELD_DCS    6      // VS1053 Data/command select pin (output)

// These are common pins between breakout and shield
#define CARDCS 4     // Card chip select pin
// DREQ should be an Int pin, see http://arduino.cc/en/Reference/attachInterrupt
#define DREQ 3       // VS1053 Data request, ideally an Interrupt pin

Adafruit_VS1053_FilePlayer musicPlayer = 
  // create breakout-example object!
  //Adafruit_VS1053_FilePlayer(BREAKOUT_RESET, BREAKOUT_CS, BREAKOUT_DCS, DREQ, CARDCS);
  // create shield-example object!
  Adafruit_VS1053_FilePlayer(SHIELD_RESET, SHIELD_CS, SHIELD_DCS, DREQ, CARDCS);
  
void setup() {
  Serial.begin(9600);
  Serial.println("prova relacio sensor cascs activa");

  pinMode(ECHO_PIN,OUTPUT);
  //delayMicroseconds(10);
  digitalWrite(ECHO_PIN,LOW);
  pinMode(ECHO_PIN,INPUT);
  
  if (! musicPlayer.begin()) { // initialise the music player
     Serial.println(F("Couldn't find VS1053, do you have the right pins defined?"));
     while (1);
  }
  Serial.println(F("VS1053 found"));
  
   if (!SD.begin(CARDCS)) {
    Serial.println(F("SD failed, or not present"));
    while (1);  // don't do anything more
  }
  
  // Set volume for left, right channels. lower numbers == louder volume!
  musicPlayer.setVolume(20,20);

  // Timer interrupts are not suggested, better to use DREQ interrupt!
  //musicPlayer.useInterrupt(VS1053_FILEPLAYER_TIMER0_INT); // timer int

  // If DREQ is on an interrupt pin (on uno, #2 or #3) we can do background
  // audio playing

  /*pinMode(ECHO_PIN,OUTPUT);
  //delayMicroseconds(10);
  digitalWrite(ECHO_PIN,LOW);
  pinMode(ECHO_PIN,INPUT);*/

  //musicPlayer.useInterrupt(VS1053_FILEPLAYER_PIN_INT)
  musicPlayer.useInterrupt(VS1053_FILEPLAYER_TIMER0_INT);  // DREQ int
}

void loop() {
  
   Serial.println("Abans");
   //musicPlayer.playFullFile("/track001.mp3"); //Aicò està en comenta perquè sinó direcatamnet no passa d'0aqui
   Serial.println("Després");
  
  //unsigned int uS = sonar.ping();
  pinMode(ECHO_PIN,OUTPUT);
  delayMicroseconds(10);
  digitalWrite(ECHO_PIN,LOW);
  pinMode(ECHO_PIN,INPUT);
  
  unsigned int uS = sonar.ping();

  distance = uS / US_ROUNDTRIP_CM;
  Serial.print("Ping: ");
  Serial.print(distance);
  Serial.println("cm");
  
     bool primercop = true;
      if(distance >= 100){
      Serial.println(F("Playing track 001"));
      if(primercop){
        musicPlayer.playFullFile("/track001.mp3");
        primercop = false;
      }
      }
    /*while(!music.stopped()){
      }*/
    
  int x = distance * 5;
  delay(x);
  

}

Thanks Paul

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