Music maker + Reflective IR sensor

Post here about your Arduino projects, get help - for Adafruit customers!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
TheKnitKnight
 
Posts: 4
Joined: Fri Jun 15, 2018 11:01 am

Music maker + Reflective IR sensor

Post by TheKnitKnight »

I just purchased a Music Maker shield and got it soldered to the Arduino board and it works like a dream using the simple_player! The sound is fantastic. On the advice of a friend, I also purchased some Reflective IR sensors with the hopes of making a specific mp3 play when the sensor is triggered.

I know this is a very noob question, but I can't seem to figure out how to make the two things work together, I can get the sensor to work on it's own and the player to work on its own. I've done some researching but can't seem to find a specific instance using these two items together.

Any suggestions or advice for a very, very new user would be much appreciated.

Thanks so much!

User avatar
johnpark
 
Posts: 985
Joined: Wed Mar 25, 2009 2:15 pm

Re: Music maker + Reflective IR sensor

Post by johnpark »

Ooh, cool idea, this will be a fun project! It may be helpful for you to look through this tutorial I did using a Music Make FeatherWing and a magnetic Hall effect sensor: https://learn.adafruit.com/mystery-box- ... o/overview

It is doing a similar type of thing, where a sensor of some kind is triggering .mp3 playback. You should be able to simply swap in your IR sensor for the Hall effect sensor and Music Maker/Arduino code may vary a bit from my Feather/FeatherWing code, but otherwise I think you'll be good to go.

User avatar
TheKnitKnight
 
Posts: 4
Joined: Fri Jun 15, 2018 11:01 am

Re: Music maker + Reflective IR sensor

Post by TheKnitKnight »

Hi Dave! Thanks so much for the help. I've managed to get some things working and others not so much. My problem still seems to be getting the shield to read the response from the sensor.

As you can see from the sketch, I want the ultrasonic sensor to measure distance. If it reads over 6, don't do anything, if it reads under 6, play the mp3 and then start looking for sensor readings again.

Included is a shot of the serial monitor. What I see is happening (in my very limited scope) is that it's reading the sensor value but that value is not being transmitted to the MP3 shield to play the music. I've switched to an Ultrasonic sensor HC-SRO4 to get a more accurate reading.

Please excuse what I'm sure is a horribly mangled code. This is my first time doing anything like this and it's been quite a challenge.

also including the serial readout
Screen Shot 2018-06-16 at 8.39.01 AM.png
Screen Shot 2018-06-16 at 8.39.01 AM.png (38.36 KiB) Viewed 315 times

Code: Select all






//Music test
//by Flynn
//based on feather_player by Limor Fried and haunted radio by John Park
//
//
//MIT License

#include <SPI.h>
#include <SD.h>
#include <Adafruit_VS1053.h>
#include <SR04.h>


// These are the pins used for the USS
#define TRIG_PIN 10          //sets trigger pin on USS
#define ECHO_PIN 9         //sets echo pin on USS)
    SR04 sr04 = SR04(ECHO_PIN,TRIG_PIN);
long a;

// 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 shield-example object!
  Adafruit_VS1053_FilePlayer(SHIELD_RESET, SHIELD_CS, SHIELD_DCS, DREQ, CARDCS);



const int USSPIN = 10;   // USS pin (since we aren't using I2C)
const int USSOUT = 9;
int ussState = 1;       // variable for reading the hall sensor status

//int royalsOn = 1;  // variable to store state of sound being played
//int royalsOn = 0; // variable to store state of station being selected

/////////////////////////// Setup /////////////////////////////////////////////

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

  pinMode(USSPIN, OUTPUT); //so we can read the USS sensor


  Serial.println("\n\nTheSuperBest");
  
  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"));
 
  //musicPlayer.sineTest(0x44, 500);    // Make a tone to indicate VS1053 is working
  
  if (!SD.begin(CARDCS)) {
    Serial.println(F("SD failed, or not present"));
    while (1);  // don't do anything more
  }
  Serial.println("SD OK!");
  
  
  // Set volume for left, right channels. lower numbers == louder volume!
  musicPlayer.setVolume(40,40);
  
// 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
  musicPlayer.useInterrupt(VS1053_FILEPLAYER_PIN_INT);  // DREQ int
  
  // Play one file, don't return until complete
  //Serial.println(F("Playing track 001"));
  //musicPlayer.playFullFile("track001.mp3");
  // Play another file in the background, REQUIRES interrupts!
  //Serial.println(F("Playing track 002"));
  //musicPlayer.startPlayingFile("track002.mp3");
}


/////////////////////////  Loop  //////////////////////////////////////////////



void loop(){
 

  ussState = digitalRead(USSPIN); //check reading of usspin

  
   a=sr04.Distance();
   Serial.print(a);
   Serial.println("cm");
   delay(1000);


 //if(royalsOn == 1) {
    //Serial.println(F("Playing track001a.mp3.\n"));
   // musicPlayer.startPlayingFile("track001.mp3");
   // royalsOn = 0; //flip the flag so it doesn't start playing over again
  //}

   if (ussState>6){ //if over 6 dont do anything
    Serial.println("No reading.\n");
    delay(200);
  }


  else if(ussState<6){ //if lower than 6 play sound
    Serial.println("\t lower than 18!");
    //if(royalsOn==1){
      Serial.println("\t Playing file track001b.mp3\n");
      musicPlayer.setVolume(30,30);
      musicPlayer.stopPlaying();
      musicPlayer.playFullFile("track001.mp3");
      //royalsOn=1;
    }  
    delay(200);
  


 
  if(musicPlayer.stopped()) {
    Serial.println("Player has stopped.\n");    
   
    //royalsOn=1; //reset counter so station can be restarted when sensor is
    //over the magnet again, comment this line out if you don't want the station file to loop or play multiple times
  }
}

User avatar
TheKnitKnight
 
Posts: 4
Joined: Fri Jun 15, 2018 11:01 am

Re: Music maker + Reflective IR sensor

Post by TheKnitKnight »

Actually, looks like I was able to make it work after all! Thanks for the help and inspiration!

User avatar
Tekkaz72
 
Posts: 3
Joined: Thu Mar 01, 2018 7:39 pm

Re: Music maker + Reflective IR sensor

Post by Tekkaz72 »

Hey TheKnitKnight, cool project. I am looking to do exactly the same as you have but i am having some small problems too. Glad to hear you got your project working. Any chance you could upload your working code or what you ended up doing to make it all work so that i may see where i may be going wrong?

Cheers,

User avatar
TheKnitKnight
 
Posts: 4
Joined: Fri Jun 15, 2018 11:01 am

Re: Music maker + Reflective IR sensor

Post by TheKnitKnight »

Hi Tekka!

Here is the sketch I am using. I did switch out the reflective sensor for an Ultrasonic sensor, but it should work the same. Just make sure you have assigned your data pin correctly.

Good luck!

Code: Select all

//code rearranged by Javier Muñoz 10/11/2016 ask me at [email protected]
//Music maker support - by Flynn De Marco
#include <SPI.h>
#include <SD.h>
#include <Adafruit_VS1053.h>

#define trigPin 10 //for the distance module
#define echoPin 9

long measureDistance(int trigger,int echo){
   long duration, distance;
  
  digitalWrite(trigger, LOW);  //PULSE ___|---|___
  delayMicroseconds(2); 
  digitalWrite(trigger, HIGH);
  delayMicroseconds(10); 
  digitalWrite(trigger, LOW);
  
  duration = pulseIn(echo, HIGH);
  distance = (duration/2) / 29.1;
   Serial.println("distance:");
   Serial.println(distance);
  return distance;

}

// 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 shield-example object!
  Adafruit_VS1053_FilePlayer(SHIELD_RESET, SHIELD_CS, SHIELD_DCS, DREQ, CARDCS);

///////////////////////////////// setup  /////////////////////////////////

void setup()
{
  Serial.begin(9600); //Start our Serial coms for serial monitor in our pc
  delay(1000);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);

Serial.println("\n\nTheSuperBest");
  
  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"));
 
  //musicPlayer.sineTest(0x44, 500);    // Make a tone to indicate VS1053 is working
  
  if (!SD.begin(CARDCS)) {
    Serial.println(F("SD failed, or not present"));
    while (1);  // don't do anything more
  }
  Serial.println("SD OK!");
  
  
  // Set volume for left, right channels. lower numbers == louder volume!
  musicPlayer.setVolume(40,40);
  
// 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
  musicPlayer.useInterrupt(VS1053_FILEPLAYER_PIN_INT);  // DREQ int
  
}

//////////////////////////// loop  ///////////////////////////////////////////

void loop() {
  if(measureDistance(trigPin,echoPin)<=12){
Serial.println("\t lower than 12!");

      Serial.println("\t Playing file track001b.mp3\n");
      musicPlayer.setVolume(30,30);
      musicPlayer.stopPlaying();
      musicPlayer.playFullFile("track001.mp3");
  }    
    delay(200);


 if(musicPlayer.stopped()) {
    Serial.println("Player has stopped.\n");    
   
  }

}

User avatar
Tekkaz72
 
Posts: 3
Joined: Thu Mar 01, 2018 7:39 pm

Re: Music maker + Reflective IR sensor

Post by Tekkaz72 »

Oh wow! Thankyou so much. I too am using an ultrasonic. I will learn from this and promise to pay it forward.

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino”