Playing sound (with the MP3 shield) & pressure plates

From Interaction Station Wiki
Jump to navigation Jump to search

Intro


In this tutorial we will be attaching a pressure plate or button to an Arduino. When the pressure plate is activated the Arduino plays a .MP3 file. This tutorial relies heavily on the Sparkfun Hookup guide that can be found online: https://learn.sparkfun.com/tutorials/mp3-player-shield-hookup-guide-v15

Requirements


Arduino Uno (+ usb cable)
Micro SD card (this stores the .MP3 files)
Sparkfun MP3 player shield (available for rent at the Interaction Station)
Pressure plates or buttons (available for rent at the Interaction Station)
An active speaker or headphones
A few jumper wires (male to male)
1k Ohm resistor
MP3 files

Getting ready


Step 1: installing the Arduino IDE
Make sure you have downloaded the Arduino IDE (Integrated Development Environment) for the uploading the code to the Arduino board. If you don't have it, please go to https://www.arduino.cc/en/Main/OldSoftwareReleases , click on your operating system followed by pressing the 'just download' button. Install the software on your computer, but don't open it yet! If it opens automatically, don't worry, just close it.

Step 2: installing the libraries for the Sparkfun MP3 shield
The Sparkfun MP3 shield uses some libraries we need to set up in the Arduino IDE. Download MP3Libraries and extract it on your computer. To install these libraries, extract the contents of the MP3Libraries folder to the corresponding path for your operating system. This guide will continue on Mac OSX.

Mac OSX
/Users/your_user_name/Documents/Arduino
Windows
C:\Users\your_user_name\Documents\Arduino

Finally connect the Arduino to your computer by using the usb cable.

Uploading code to the Arduino


Step 1: opening and configuring the Arduino IDE
Open up the Arduino IDE while your Arduino is connected to your computer. Make sure your board and port is selected by the IDE similar to the screenshots beneath.

Arduino audio1.png
The port can be named a bit different, that's not a problem.
Arduino audio2.png
If you are using a Arduino Uno, select Arduino Uno. Otherwise change it to the name of the board you are using.

Step 2: uploading code
First, remove the few lines of code that the software automatically generates so that you have a empty file. Then, paste the code underneath in the Arduino IDE.

/*
  MP3 Shield Trigger
  by: Jim Lindblom
      SparkFun Electronics
  date: September 23, 2013

  This is an example MP3 trigger sketch for the SparkFun MP3 Shield.
  Pins 0, 1, 5, 10, A0, A1, A2, A3, and A4 are setup to trigger tracks
  "track001.mp3", "track002.mp3", etc. on an SD card loaded into
  the shield. Whenever any of those pins are shorted to ground,
  their respective track will start playing.

  When a new pin is triggered, any track currently playing will
  stop, and the new one will start.

  A5 is setup to globally STOP playing a track when triggered.

  If you need more triggers, the shield's jumpers on pins 3 and 4 
  (MIDI-IN and GPIO1) can be cut open and used as additional
  trigger pins. Also, because pins 0 and 1 are used as triggers
  Serial is not available for debugging. Disable those as
  triggers if you want to use serial.

  Much of this code was grabbed from the FilePlayer example
  included with the SFEMP3Shield library. Major thanks to Bill
  Porter and Michael Flaga, again, for this amazing library!
*/

#include <SPI.h>           // SPI library
#include <SdFat.h>         // SDFat Library
#include <SdFatUtil.h>     // SDFat Util Library
#include <SFEMP3Shield.h>  // Mp3 Shield Library

SdFat sd; // Create object to handle SD functions

SFEMP3Shield MP3player; // Create Mp3 library object
// These variables are used in the MP3 initialization to set up
// some stereo options:
const uint8_t volume = 0; // MP3 Player volume 0=max, 255=lowest (off)
const uint16_t monoMode = 1;  // Mono setting 0=off, 3=max

/* Pin setup */
#define TRIGGER_COUNT 9
int triggerPins[TRIGGER_COUNT] = {0, 1, 5, 10, A0, A1, A2, A3, A4};
int stopPin = A5; // This pin triggers a track stop.
int lastTrigger = 0; // This variable keeps track of which tune is playing

void setup()
{
  /* Set up all trigger pins as inputs, with pull-ups activated: */
  for (int i=0; i<TRIGGER_COUNT; i++)
  {
    pinMode(triggerPins[i], INPUT_PULLUP);
  }
  pinMode(stopPin, INPUT_PULLUP);

  initSD();  // Initialize the SD card
  initMP3Player(); // Initialize the MP3 Shield
}

// All the loop does is continuously step through the trigger
//  pins to see if one is pulled low. If it is, it'll stop any
//  currently playing track, and start playing a new one.
void loop()
{
  for (int i=0; i<TRIGGER_COUNT; i++)
  {
    if ((digitalRead(triggerPins[i]) == LOW) && ((i+1) != lastTrigger))
    {
      lastTrigger = i+1; // Update lastTrigger variable to current trigger
      /* If another track is playing, stop it: */
      if (MP3player.isPlaying())
        MP3player.stopTrack();

      /* Use the playTrack function to play a numbered track: */
      uint8_t result = MP3player.playTrack(lastTrigger);
      // An alternative here would be to use the
      //  playMP3(fileName) function, as long as you mapped
      //  the file names to trigger pins.

      if (result == 0)  // playTrack() returns 0 on success
      {
        // Success
      }
      else // Otherwise there's an error, check the code
      {
        // Print error code somehow, someway
      }
    }
  }
  // After looping through and checking trigger pins, check to
  //  see if the stopPin (A5) is triggered.
  if (digitalRead(stopPin) == LOW)
  {
    lastTrigger = 0; // Reset lastTrigger
    // If another track is playing, stop it.
    if (MP3player.isPlaying())
      MP3player.stopTrack();
  }
}

// initSD() initializes the SD card and checks for an error.
void initSD()
{
  //Initialize the SdCard.
  if(!sd.begin(SD_SEL, SPI_HALF_SPEED)) 
    sd.initErrorHalt();
  if(!sd.chdir("/")) 
    sd.errorHalt("sd.chdir");
}

// initMP3Player() sets up all of the initialization for the
// MP3 Player Shield. It runs the begin() function, checks
// for errors, applies a patch if found, and sets the volume/
// stero mode.
void initMP3Player()
{
  uint8_t result = MP3player.begin(); // init the mp3 player shield
  if(result != 0) // check result, see readme for error codes.
  {
    // Error checking can go here!
  }
  MP3player.setVolume(volume, volume);
  MP3player.setMonoMode(monoMode);
}


Check if the code returns any errors by pressing the check mark icon (✓) to verify the code. If everything is alright, upload the code to the board by pressing the arrow (➔).

Connecting the wires


Using the image below as a reference, connect all the wires to the Arduino board after inserting the Sparkfun MP3 shield on top.

Arduino audio schematic.png
The schematic uses a button instead of pressure plates. To use the the pressure plates, just replace the two connectors of the button with the wires of the pressure plate.

Uploading music


Grab a micro SD card and upload a mp3 with the name 'track002.mp3'. Because we connected the button to pin 1 on the Arduino we need to name it this way. (Pin 0 would mean that we need to name our file to 'track001.mp3'). After uploading, unmount the SD card and insert it into the Sparkfun MP3 shield.

Finalising


Reconnect the Arduino to your computer or to a power socket using a 5v adapter (e.g. phone charger). Make sure that the Sparkfun MP3 Shield is connected to a active speaker or headphone. listen carefully and press the pressure plate or button. If there are no errors, congratulations! You've finished your Arduino project.

Errors


Sometimes the pin you connected your button to isn't redirecting to the corresponding audio file, you can try uploading about ten of the same .MP3 files, with names ranging from 'track000.mp3' to 'track009.mp3' to ensure that the file is being played.