Go Down

Topic: Controlling Adafruit Music Maker Shield with HC-SR04 sensors. (Read 2276 times) previous topic - next topic

danielsz

Hi everyone, I actually put up a posting a few months ago about the same thing, but I had to switch my attention. I now have a week (and a bit) to do what I'm about to describe.

I study Product Design at Degree level, and am developing a "motion controlled" musical instrument.

What I need is the shield I mentioned to play a certain file when I trigger a distance on the Sensor. So I will have multiple recorded files of musical notes on keyboard, put onto a microSD that slots into the musicmaker shield. I then need to connect a HCSR-04  sensor to this setup (the shield mounted into my Uno) and make the board output specified tracks depending on the distance I touch.

That's the basic idea, I actually want 5 sensors doing this, in tandem if required, but for prototype purposes I will only worry about getting one sensor working fully. I don't have any prior experience in Arduino, and only a basic understanding of the code workings (maybe not even that). I'm fully aware that I may have bitten off more than I can chew as a result.

I know that I need to use IF statements to control the reading and interpretation of the distance numbers. if (currDist > 10) then play a track that I've specified. This is about as far as my knowledge takes me. I have code that someone created to make a motion controlled MP3 player, but he/ she used a different shield to me (the one they used I could not find anywhere). It seems to have most of the information and basic code lines that I will need, but I don't know if the syntax they have used carries over to MY shield.

Sorry if this all sounds convoluted, I'm more than happy to break it down again, but basically;

Arduino UNO will be attached to Adafruit Music Maker MP3 shield, which will then be attached to one HC-SR04 ultrasonic sensor. I need to be able to wave my hand over the sensor and play a specified track by doing so. It's for my final project of my Degree, and NEEDS to work, and I thought it'd be a lot easier than it apparently is. As a result any significant amount of help given WILL be credited in my major project design report, as this is so important to me.

Thanks in advance, the website for the code I have (for the motion controlled mp3 players) is linked below, as is the shield itself.

https://www.itead.cc/blog/motion-control-music-player


https://learn.adafruit.com/adafruit-music-maker-shield-vs1053-mp3-wav-wave-ogg-vorbis-player/play-music

Many thanks to anyone that can help, I intend to ask around the electrical departments at University to see if anyone can help me there, but I'm not holding out much help. Thanks.

danielsz

I got it!! Spent the morning writing the code with someone at University, and got there after a couple of hours! Here is the code for anyone who wants to do something similar in future!!

Code: [Select]
// include SPI, MP3, Servo and SD libraries
#include <SPI.h>             // We will use the hardware SPI pins: CLK (13), MISO (12), MOSI (11)
#include "Adafruit_VS1053.h"
#include <SD.h>
int track1, track2, track3;
const int pingPin = 8;
const int echoPin = 9;
unsigned int duration, inches;


// 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 =
 
  Adafruit_VS1053_FilePlayer(SHIELD_RESET, SHIELD_CS, SHIELD_DCS, DREQ, CARDCS);

  void setup() {
 
  track1=0;
  track2=0;
  track3=0;
  pinMode(8,OUTPUT);
  pinMode(9,INPUT);
  Serial.begin(9600);
  Serial.println("File From Distance Test");

  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"));
 
  SD.begin(CARDCS);    // initialise the SD card
 
  // Set volume for left, right channels. lower numbers == louder volume!
  musicPlayer.setVolume(5,5);
  }

void ping()  {
  digitalWrite(pingPin, LOW);        // Ensure pin is low
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);       // Start ranging
  delayMicroseconds(5);              //   with 5 microsecond burst
  digitalWrite(pingPin, LOW);        // End ranging
 
  duration = pulseIn(echoPin, HIGH); // Read echo pulse
  inches = duration / 74 / 2;        // Convert to inches
  Serial.println(inches);            // Display result
  if ((inches>3) && (inches<7)) { Serial.println("1"); delay(1000); }
  if ((inches>8) && (inches<12))  { Serial.printIn("2"); delay(1000); }
  if ((inches>13) && (inches<17))  { Serial.printIn("3"); delay(1000( ;}
 
}
void loop() {
  delay(50);
  ping();
   if ((inches>3) && (inches<7)) {
    if (track1==0){
    musicPlayer.playFullFile("track001.mp3");
    track1=1; track2=0; track3=0;}
   }
 
   
   if ((inches>8) && (inches<12)) {
   if (track2==0){
   musicPlayer.playFullFile("track002.mp3");
    track2=1; track1=0; track3=0;
    } //track2=0
   } //inches

   if ((inches>13) && (inches<17)) {
    if (track3==0){

   musicPlayer.playFullFile("track003.mp3");}
   track3=1; track2=0; track1=0;}

}




   


Essentially uses if statements, combined with distance readouts, but with an extra couple of lines so that each sound cannot be triggered UNTIL the next sound is triggered, using a second if statement. This stopped an annoying repetition of playbacks, so that one note can only be played again if another file has been triggered.

Got 3 different mp3 files currently linked in this code, and I'm going to add 4 more tomorrow over the ideal distance, and hopefully adapt the code to house a second sensor. Not sure how this'll go just yet, but the hard part is done now!

stevewhite

Could you please post the code that includes the additional audio tracks? I am doing a project that will involve about 10 different tracks at varying distances. Thanks so much!

stevewhite

I am having challenges getting this sketch to work. I have modified Danielsz's sketch (thanks SO MUCH!) to include more audio tracks triggered by the HR-SC04 sensor and It works fine. Then I added a relay into the sketch to be turned on at the same time as an audio track. The error message in the Arduino IDE is "expected '}' at the end of input. How can I get this to work? Please let me know if I have done this incorrectly and what I can do to resolve the issue. Thanks!


Code: [Select]
/*

// include SPI, MP3, Servo and SD libraries
#include <SPI.h>             // We will use the hardware SPI pins: CLK (13), MISO (12), MOSI (11)
#include "Adafruit_VS1053.h"
#include <SD.h>
int track1, track2, track3, track4, track5, track6, track7, track8, track9, track10;
const int pingPin = 8;
const int echoPin = 9;
unsigned int duration, inches;
int relay = 7;       // relay switch on pin 7



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

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

void setup() {

 
  track1=0;
  track2=0;
  track3=0;
  track4=0;
  track5=0;
  track6=0;
  track7=0;
  track8=0;
  track9=0;
  track10=0;
  pinMode(8,OUTPUT);
  pinMode(9,INPUT);
  Serial.begin(9600);
  Serial.println("File From Distance Test");
    pinMode(relay, OUTPUT);

  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"));

  SD.begin(CARDCS);    // initialise the SD card

  // Set volume for left, right channels. lower numbers == louder volume!
  musicPlayer.setVolume(1,1);
}

void ping()  {
  digitalWrite(pingPin, LOW);        // Ensure pin is low
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);       // Start ranging
  delayMicroseconds(5);              //   with 5 microsecond burst
  digitalWrite(pingPin, LOW);        // End ranging

  duration = pulseIn(echoPin, HIGH); // Read echo pulse
  inches = duration / 74 / 2;        // Convert to inches
  Serial.println(inches);            // Display result
  if ((inches>3) && (inches<5)) {
    Serial.println("1");
    delay(100);
  }
  if ((inches>5) && (inches<7))  {
    Serial.println("2");
    delay(100);
  }
  if ((inches>7) && (inches<9))  {
    Serial.println("3");
    delay(100);
  }
  if ((inches>9) && (inches<11)) {
    Serial.println("4");
    delay(100);
  }
  if ((inches>11) && (inches<13)) {
    Serial.println("5");
    delay(100);
  }
  if ((inches>13) && (inches<15)) {
    Serial.println("6");
    delay(100);
  }
  if ((inches>15) && (inches<17)) {
    Serial.println("7");
    delay(100);
  }
  if ((inches>17) && (inches<19)) {
    Serial.println("8");
    delay(100);
  }
  if ((inches>19) && (inches<21)) {
    Serial.println("9");
    delay(100);
  }
  if ((inches>21) && (inches<23)) {
    Serial.println("10");
    delay(100) ;}
 

}
void loop() {
  delay(50);
  ping();
  if ((inches>3) && (inches<5)) {
    if (track1==0){
      musicPlayer.playFullFile("track001.mp3");
      track1=1;
      track2=0;
      track3=0;
      track4=0;
      track5=0;
      track6=0;
      track7=0;
      track8=0;
      track9=0;
      track10=0;
   
    digitalWrite(relay, HIGH);   // turn the relay on (HIGH is the voltage level)
    delay(10000);               // wait for 10 seconds
    digitalWrite(relay, LOW);    // turn the relay off by making the voltage LOW


  }

  if ((inches>5) && (inches<7)) {
    if (track2==0){
      musicPlayer.playFullFile("track002.mp3");
      track1=0;
      track2=1;
      track3=0;
      track4=0;
      track5=0;
      track6=0;
      track7=0;
      track8=0;
      track9=0;
      track10=0;
   
    digitalWrite(relay, HIGH);   // turn the LED on (HIGH is the voltage level)
    delay(10000);               // wait for a second
    digitalWrite(relay, LOW);    // turn the LED off by making the voltage LOW


  }

  if ((inches>7) && (inches<9)) {
    if (track3==0){
      musicPlayer.playFullFile("track003.mp3");
     
    track1=0;
    track2=0;
    track3=1;
    track4=0;
    track5=0;
    track6=0;
    track7=0;
    track8=0;
    track9=0;
    track10=0;
 
    digitalWrite(relay, HIGH);   // turn the relay on (HIGH is the voltage level)
    delay(10000);               // wait for 10 seconds
    digitalWrite(relay, LOW);    // turn the relay off by making the voltage LOW


 
  }


  if ((inches>9) && (inches<11)) {
    if (track4==0){
      musicPlayer.playFullFile("track004.mp3");
      track1=0;
      track2=0;
      track3=0;
      track4=1;
      track5=0;
      track6=0;
      track7=0;
      track8=0;
      track9=0;
      track10=0;
   
    digitalWrite(relay, HIGH);   // turn the relay on (HIGH is the voltage level)
    delay(10000);               // wait for 10 seconds
    digitalWrite(relay, LOW);    // turn the relay off by making the voltage LOW

   
  }

  if ((inches>11) && (inches<13)) {
    if (track5==0){
      musicPlayer.playFullFile("track005.mp3");
      track1=0;
      track2=0;
      track3=0;
      track4=0;
      track5=1;
      track6=0;
      track7=0;
      track8=0;
      track9=0;
      track10=0;
   
    digitalWrite(relay, HIGH);   // turn the relay on (HIGH is the voltage level)
    delay(10000);               // wait for 10 seconds
    digitalWrite(relay, LOW);    // turn the relay off by making the voltage LOW

   
  }

  if ((inches>13) && (inches<15)) {
    if (track1==6){

      musicPlayer.playFullFile("track006.mp3");
      track1=0;
      track2=0;
      track3=0;
      track4=0;
      track5=0;
      track6=1;
      track7=0;
      track8=0;
      track9=0;
      track10=0;
    digitalWrite(relay, HIGH);   // turn the relay on (HIGH is the voltage level)
    delay(10000);               // wait for 10 seconds
    digitalWrite(relay, LOW);    // turn the relay off by making the voltage LOW

  }

  if ((inches>15) && (inches<17)) {
    if (track7==0){

      musicPlayer.playFullFile("track007.mp3");
      track1=0;
      track2=0;
      track3=0;
      track4=0;
      track5=0;
      track6=0;
      track7=1;
      track8=0;
      track9=0;
      track10=0;
   
    digitalWrite(relay, HIGH);   // turn the relay on (HIGH is the voltage level)
    delay(10000);               // wait for 10 seconds
    digitalWrite(relay, LOW);    // turn the relay off by making the voltage LOW

  }

  if ((inches>17) && (inches<19)) {
    if (track8==0){

      musicPlayer.playFullFile("track008.mp3");
      track1=0;
      track2=0;
      track3=0;
      track4=0;
      track5=0;
      track6=0;
      track7=0;
      track8=1;
      track9=0;
      track10=0;
 

     digitalWrite(relay, HIGH);   // turn the relay on (HIGH is the voltage level)
    delay(10000);               // wait for 10 seconds
    digitalWrite(relay, LOW);    // turn the relay off by making the voltage LOW


  }
  if ((inches>19) && (inches<21)) {
    if (track9==0){
      musicPlayer.playFullFile("track009.mp3");
      track1=0;
      track2=0;
      track3=0;
      track4=0;
      track5=0;
      track6=0;
      track7=0;
      track8=0;
      track9=1;
      track10=0;
   
    digitalWrite(relay, HIGH);   // turn the relay on (HIGH is the voltage level)
    delay(10000);               // wait for 10 seconds
    digitalWrite(relay, LOW);    // turn the relay off by making the voltage LOW


  }


  if ((inches>21) && (inches<23)) {
    if (track10==0){

      musicPlayer.playFullFile("track010.mp3");}
   
    track1=0;
    track2=0;
    track3=0;
    track4=0;
    track5=0;
    track6=0;
    track7=0;
    track8=0;
    track9=0;
    track10=1;
 
    digitalWrite(relay, HIGH);   // turn the relay on (HIGH is the voltage level)
    delay(10000);               // wait for 10 seconds
    digitalWrite(relay, LOW);    // turn the relay off by making the voltage LOW
    }





CrossRoads

Put a } at the end of the code you listed to close out the loop() section. Might be others missing also.
You can put click your cursor next to each } and then scroll back to see the mating {, make sure they are paired up as intended.
CTRL-T will also alert you to mis-matched ( ) and { } quantities.
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

CrossRoads

For example this section has two each of {  and }.
Many others are missing the } after the 3rd line. I don't know if that one should be there, or if it should be after the 2nd digitalWrite.
Code: [Select]

  if ((inches>21) && (inches<23)) {
    if (track10==0){

      musicPlayer.playFullFile("track010.mp3");}
   
    track1=0;
    track2=0;
    track3=0;
    track4=0;
    track5=0;
    track6=0;
    track7=0;
    track8=0;
    track9=0;
    track10=1;
 
    digitalWrite(relay, HIGH);   // turn the relay on (HIGH is the voltage level)
    delay(10000);               // wait for 10 seconds
    digitalWrite(relay, LOW);    // turn the relay off by making the voltage LOW
    }
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

stevewhite

I combed through the sketch and found the culprit '}' and added the missing ones as well.
Things are working now! Thanks for the reply.

CrossRoads

Not introducing those, and fixing them, gets better with practice.
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

Kikino12

Hi Guys,

Thanks so much for bringing this to the forum. I'm trying to do a similar thing except I want the the tracks to play as normal except that when someone stands in front of the  HR-SC04, the volume of the track will lower until they get out of the way. The idea is for a device that can tell when someone is outside the bathroom door so as to inform you to keep your shower singing down by lowering your music.

I'm also a total newbie to Arduino and i'm actually kind of lost as to where to start. I haven't ever done anything like this before. If anyone had any pointers I'd be all ears. Thanks guys :)

Ciara

Go Up