for the arduino: "elsewhere_accelerometer_audio (Version 1.8)"

  • description
  • how-to/where-to...
  • artist bio
  • artist statement
  • comments
  • elizabeth (home)
    *  elsewhere_accelerometer_audio (Version 1.8)
    *  Built with Arduino 15 on April 27, 2010
    *      by Elizabeth Travelsight
    *        for "Elsewhere" (
    *          & "Things that are Possible" the Digital Arts New Media MFA Exhibition
    *              at the University of California Santa Cruz, April 29-May 9, 2010
    *  Based upon examples from ladyada's "WaveHC with 6 buttons" (
    *     and nick lally (
    //load these libraries.
    #include "WaveUtil.h"
    #include "WaveHC.h"
    SdReader card;    // This object holds the information for the card
    FatVolume vol;    // This holds the information for the partition on the card
    FatReader root;   // This holds the information for the filesystem on the card
    FatReader f;      // This holds the information for the file we're play
    WaveHC wave;      // This is the only wave (audio) object, since we will only play one at a time
    #define DEBOUNCE 100  // button debouncer
    const int xPin = 6;		// X output of the accelerometer
    const int yPin = 7;		// Y output of the accelerometer
    int t = 3000; // sets delay time between laughs
    int pulseX;  // variables to read the pulse width from accelerometer
    int pulseY;
    int accelerationX;  // convert the pulse width into acceleration; accelerationX and accelerationY are in milli-g's; earth's gravity is 1000 milli-g's, or 1g.
    int accelerationY;
    long randNumber;   //declares variable for random number with min/max range
    /*create array of wav file names. (these people laughed for me! some of the audio files
     are commented out because the software got buggy when I listed more than 18 files.
    i don't know why.  if you do, i'd love to hear all about it.)*/
    char* fileName[] = {
    // this handy function will return the number of bytes currently free in RAM, great for debugging!   
    int freeRam(void)
      extern int  __bss_end; 
      extern int  *__brkval; 
      int free_memory; 
      if((int)__brkval == 0) {
        free_memory = ((int)&free_memory) - ((int)&__bss_end); 
      else {
        free_memory = ((int)&free_memory) - ((int)__brkval); 
      return free_memory; 
    void sdErrorCheck(void)
      if (!card.errorCode()) return;
      putstring("\n\rSD I/O error: ");
      Serial.print(card.errorCode(), HEX);
      putstring(", ");
      Serial.println(card.errorData(), HEX);
    void setup() {
      // set up serial port
       putstring("Free RAM: ");       // This can help with debugging, running out of RAM is bad
      Serial.println(freeRam());      // if this is under 150 bytes it may spell trouble!
      // Set the output pins for the DAC control. These pins are defined in the library
      pinMode(2, OUTPUT);
      pinMode(3, OUTPUT);
      pinMode(4, OUTPUT);
      pinMode(5, OUTPUT);
      // pin13 LED
      pinMode(13, OUTPUT);
     // initialize the pins connected to the accelerometer as inputs.
      pinMode(xPin, INPUT);
      pinMode(yPin, INPUT);
      //  if (!card.init(true)) { //play with 4 MHz spi if 8MHz isn't working for you
      if (!card.init()) {         //play with 8 MHz spi (default faster!)  
        putstring_nl("Card init. failed!");  // Something went wrong, lets print out why
        while(1);                            // then 'halt' - do nothing!
      // enable optimize read - some cards may timeout. Disable if you're having problems
    // Now we will look for a FAT partition!
      uint8_t part;
      for (part = 0; part < 5; part++) {     // we have up to 5 slots to look in
        if (vol.init(card, part)) 
          break;                             // we found one, lets bail
      if (part == 5) {                       // if we ended up not finding one  :(
        putstring_nl("No valid FAT partition!");
        sdErrorCheck();      // Something went wrong, lets print out why
        while(1);                            // then 'halt' - do nothing!
      // Lets tell the user about what we found
      putstring("Using partition ");
      Serial.print(part, DEC);
      putstring(", type is FAT");
      Serial.println(vol.fatType(),DEC);     // FAT16 or FAT32?
      // Try to open the root directory
      if (!root.openRoot(vol)) {
        putstring_nl("Can't open root dir!"); // Something went wrong,
        while(1);                             // then 'halt' - do nothing!
      // Whew! We got past the tough parts.
    void loop() {
      //putstring(".");            // uncomment this to see if the loop isnt running
      int pulseX, pulseY;  // variables to read the pulse width.
      int accelerationX, accelerationY;  // variables to contain the resulting accelerations.
      int randNmber; // variable to select wav file at random.
      pulseX = pulseIn(xPin,HIGH);  // read pulse from x- and y-axes:
      pulseY = pulseIn(yPin,HIGH);
      // convert the pulse width into acceleration; accelerationX and accelerationY are in milli-g's; earth's gravity is 1000 milli-g's, or 1g.
      accelerationX = ((pulseX / 10) - 500) * 8;
      accelerationY = ((pulseY / 10) - 500) * 8;
      randNumber = random(0,18);  //choose random number between 0 (inclusive) and 18(exclusive).
      Serial.print(randNumber);  //print the number.
      Serial.print(accelerationX);  // print the x acceleration.
      Serial.print("\t");  // print a tab character.
      Serial.print(accelerationY);  // print the y acceleration.
      delay(100);  // wait 0.1 seconds
    //this is where the accelerometer triggers the audio player
     if (accelerationX < -150 || accelerationX > -80){  // these number depend upon the resting position of the boat.
        playcomplete(fileName[randNumber]);  //if the accelerometer is moved from its resting position... select a random number and play that file.
        Serial.print(randNumber); // print that number
        Serial.print(fileName[randNumber]); //print the file name
    // Plays a full file from beginning to end with no pause.
    void playcomplete(char *name) {
      // call our helper to find and play this name
      while (wave.isplaying) {
      // do nothing while its playing
      // now its done playing
    void playfile(char *name) {
      // see if the wave object is currently doing something
      if (wave.isplaying) {// already playing something, so stop it!
        wave.stop(); // stop it
      // look in the root directory and open the file
      if (!, name)) {
        putstring("Couldn't open file "); Serial.print(name); return;
      // OK read the file and turn it into a wave object
      if (!wave.create(f)) {
        putstring_nl("Not a valid WAV"); return;
      // ok time to play! start playback;