Home Forums Sparkfun MP3 Shield Library Support Forum Next track by pushing button – some code enlightenment

Tagged: , , ,

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #2623
    Anonymous
    Inactive

    Hi,

    I have made the mp3 shield work with some funky textile buttons (when you press then, the resistivity changes -> they are diy pressure sensors – not very precise, but with enough variation to be able to measure it.)

    Each button is attributed to one sound. (button 1 plays track 1, button 2 plays track 2)

    But now, all tracks play until the end.

    —> I would like them to stop playing when another button is pushed – but I find myself in a math loop -> of not knowing how to solve this puzzle. It’s probably very simple, but I am a Frankenscript coder!

    Thanks a lot for the amazing Github documentation. I went through it, but some things went over my head.. I will see if my local hackerspace can shed some light on certain things like the interrupts..

    Here is my current Frankenscript code:

     

    //Program by Jeremy Blum and Wendy Van Wynsberghe
    //www.jeremyblum.com

    //This will turn on an LED after a threshold
    int sensePin0 = 0;                   //analog textile button 0
    int sensePin1 = 1;                   //analog textile button 1
    int sensePin2 = 2;                   //analog textile button 2
    int sensePin3 = 3;                   //analog textile button 3
    int sensePin4 = 4;                   //analog textile button 4
    int sensePin5 = 5;                   //analog textile button 5
    int ledPin0 = 12;                    //same led for all buttons
    //int inputPinButton0 = 5;              // digital textile button0
    int valButton0 = 0;                    // variable for reading the pin status
    //This introduces the SdFat library and the MP3shield library
    #include <SPI.h>
    #include <SdFat.h>
    #include <SdFatUtil.h>
    #include <SFEMP3Shield.h>

    SFEMP3Shield MP3player; //player
    SdFat sd; //card
    //const int pinButton = 5;
    //const int nbTracks = 5; //CHANGE THIS VALUE TO THE NUMBER OF SONGS ON YOUR SD CARD
    const int volume = 6;//-3dB. The higher number, the lower volume
    int counter = 1;

    void setup()
    {
    Serial.begin(9600);
    sd.begin(SD_SEL, SPI_HALF_SPEED); //start card

    //Note: We don’t need to specifiy sensePin as an
    //input, since it defaults to that when we read it

    //The LED pin needs to be set as an output
    //pinMode(ledPin0, OUTPUT);
    //pinMode(inputPinButton0, INPUT);     // declare pushbutton as input

    //This is the default value, but we can set it anyways
    analogReference(DEFAULT); //5V Reference on UNO

    //start the MP3shield
    MP3player.begin(); //start player
    MP3player.setVolume(volume, volume);

    }

    void loop()
    {
    // read the sensor
    int val0 = analogRead(sensePin0);         //read the textile button 0
    int val1 = analogRead(sensePin1);          //read the textile button 1
    int val2 = analogRead(sensePin2);         //read the textile button 0
    int val3 = analogRead(sensePin3);          //read the textile button 1
    int val4 = analogRead(sensePin4);         //read the textile button 0
    int val5 = analogRead(sensePin5);          //read the textile button 1
    //valButton0 = digitalRead(inputPinButton0);  // read input value
    //Serial.println (“Sensor1:”);
    //Serial.println (val0);
    // Serial.println (“Sensor2:”);
    Serial.println (val1);

    if(val0 < 50)
    {
    digitalWrite(ledPin0, HIGH);
    //start playing track 1
    MP3player.playTrack(1);
    }
    else
    {
    digitalWrite(ledPin0, LOW);

    }

    if(val1 < 80)
    {
    digitalWrite(ledPin0, HIGH);
    //start playing track 1
    MP3player.playTrack(2);
    }
    else
    {
    digitalWrite(ledPin0, LOW);
    }

    if(val2 < 80)
    {
    digitalWrite(ledPin0, HIGH);
    //start playing track 1
    MP3player.playTrack(3);
    }
    else
    {
    digitalWrite(ledPin0, LOW);
    }

    if(val3 < 80)
    {
    digitalWrite(ledPin0, HIGH);
    //start playing track 1
    MP3player.playTrack(4);
    }
    else
    {
    digitalWrite(ledPin0, LOW);
    }

    if(val4 < 80)
    {
    digitalWrite(ledPin0, HIGH);
    //start playing track 1
    MP3player.playTrack(5);
    }
    else
    {
    digitalWrite(ledPin0, LOW);
    }

    if(val5 < 80)
    {
    digitalWrite(ledPin0, HIGH);
    //start playing track 1
    MP3player.playTrack(6);
    }
    else
    {
    digitalWrite(ledPin0, LOW);
    }

    /*}
    if (valButton0 == HIGH) {            // check if the input is HIGH
    digitalWrite(ledPin0, HIGH);  // turn LED ON
    MP3player.playTrack(2);
    }
    else {
    digitalWrite(ledPin0, LOW); // turn LED OFF
    }*/

    }

     

    Thanks!

    Greetings,

    Wendy

    #2625

    You need only add

    MP3player.stopTrack();

    in front of the

    MP3player.playTrack(??);

    To stop any currently playing tracks, and it won’t care if there is no track playing, it will just fall through.

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