Home Forums Sparkfun MP3 Shield Library Support Forum Help with mp3shield sensor trigger

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #2615
    Anonymous
    Inactive

    Hi guys,

    Firstly thanks heaps for the library 🙂

    I am attempting a very simple project, one of which there is many examples of over the net. Just to trigger a single sample via a sensor input to the shield. I have attempted to use the cemetary howler project, along with a bunch of other codes, to get this to work. But for whatever reason, it is just not happening!

     

    I have a number of different sensors(maxsonar, sharp distance, IR’s) so happy to change the code to suit one of those. This piece of code here I am using an IR sensor (E18-D80NK) that just returns 1’s and 0’s (0 for motion detected)

    This is the little code I have put together, any hints? I am starting again from scratch now but not very good at this and having a huge amount of trouble routing the sensor input to trigger the MP3player.playTrack(1) command.

     

    Cheers 🙂 I am all for teaching myself but its been a very stressful week and now I need some help from the experts!

     

    Jack Colley

     

    below is the code, sorry i couldnt figure out how to wrap it!

     

    // libraries
    #include <SPI.h>
    #include <SdFat.h>
    #include <SdFatUtil.h>
    #include <SFEMP3Shield.h>

    // initialize MP3 card
    SdFat sd;
    SFEMP3Shield MP3player;

    // constant variables

    int inputPin = 5;                    // IR sensor input pin
    unsigned long pauseTime = 10000;     // how long the pause will be after music turned off
    int readingInterval = 10;            // interval to read the sensor

    // changing variables

    int trigger = 1;                     // variable for pin status
    byte result;                         // variable for mp3 player shield, can be used to debug

    // setup

    void setup() {
    pinMode(inputPin, INPUT);                        // make sensor an input
    digitalWrite(inputPin, HIGH);                    // activate internal pull-up resistor
    result = MP3player.begin();                      // start mp3 player shield
    MP3player.setVolume(10, 10);                     // set volume of the mp3 player

    }

    // loop

    void loop(){

    trigger = digitalRead(inputPin);                 // read sensor
    if (trigger == 0) {                              // if sensed
    result = MP3player.playTrack(1);   // play track
    delay(pauseTime);                            // wait…

    }
    delay(readingInterval);                          // wait with reading
    }

     

     

    #2616
    Anonymous
    Inactive

    UPDATE!!!

     

    Have got it to work using both analog and digital sensors (two E18’s and a sharp distance)

     

    here is my updated code. Any advice on it would be great, but it works!

     

    // libraries
    #include <SPI.h>
    #include <SdFat.h>
    #include <SdFatUtil.h>
    #include <SFEMP3Shield.h>

    // initialize MP3 card
    SdFat sd;
    SFEMP3Shield MP3player;

    // constant variables

    int inputPin = 0;                    // IR sensor input pin
    unsigned long pauseTime = 5000;     // how long the pause will be after music turned off
    int readingInterval = 10;     // interval to read the sensor
    int SignalPin=5;
    int SignalPin2=4;
    #define SignalPin 5
    #define SignalPin2 4

    // setup

    void setup() {
    Serial.begin(9600);
    sd.begin(SD_SEL, SPI_HALF_SPEED);
    MP3player.begin();
    MP3player.setVolume(10, 10);
    pinMode(inputPin, INPUT);
    pinMode(SignalPin, INPUT);

    }

    // loop

    void loop(){
    if (analogRead(inputPin) > 450)       {
    MP3player.playTrack(2);
    delay(pauseTime);

    }
    {
    {
    int Signal = 1;

    Signal = digitalRead(SignalPin);

    if (Signal == 1){
    Serial.print (Signal);
    Serial.println(”  no obstacle”);
    }

    else {
    MP3player.playTrack(1);
    delay(pauseTime);
    }

    }

    {
    int Signal = 1;

    Signal = digitalRead(SignalPin2);

    if (Signal == 1){
    Serial.print (Signal);
    Serial.println(”  no obstacle”);
    }

    else {
    MP3player.playTrack(2);
    delay(pauseTime);
    }

    }
    }

    delay(readingInterval);                          // wait with reading

    }

    #2620

    See https://gist.github.com/mpflaga/5569322

    Signal only needs to be typed once and each digital read will over right its prior value. along with no need to compartmentalize as much.

    note the use of

    while(MP3player.isPlaying()) {
    //wait until done playing
    }

    with out it you can attempt to play others, but will have an error as it is already playing. Note the commented out line of MP3player.stopTrack(); which will help that if you desire. which won’t work with the above while(MP3player.isPlaying()) as you wont’ get to it. So you have some choices.

Viewing 3 posts - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.