/*
* elsewhere_accelerometer_audio (Version 1.8)
*
* Built with Arduino 15 on April 27, 2010
* by Elizabeth Travelsight
* for "Elsewhere" (http://elsewhere.elizabethtravelslight.com)
* & "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" (http://www.ladyada.net/make/waveshield/libraryhcplay6.html)
* and nick lally (http://nicklally.com/code/sensor_inputs.pde)
*/
//load these libraries.
#include
#include
#include
#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[] = {
"amber.wav",
"david.wav",
"donny.wav",
"eric.wav",
//"jaydee.wav",
"jomer.wav",
"kayla.wav",
//"leah.wav",
"leah2.wav",
"lisa.wav",
"mark.wav",
"maya.wav",
"maya2.wav",
"mom.wav",
//"mom2.wav",
"nicole.wav",
"ruth.wav",
"tobias.wav",
"tobias2.wav",
"wolf.wav"
};
// 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);
while(1);
}
void setup() {
// set up serial port
Serial.begin(9600);
putstring_nl("elsewhere_accelerometer_audio_1_8");
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
sdErrorCheck();
while(1); // then 'halt' - do nothing!
}
// enable optimize read - some cards may timeout. Disable if you're having problems
card.partialBlockRead(true);
// 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.
putstring_nl("Ready!");
}
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.println();
Serial.print(accelerationX); // print the x acceleration.
Serial.print("\t"); // print a tab character.
Serial.print(accelerationY); // print the y acceleration.
Serial.println();
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("\t");
Serial.print(fileName[randNumber]); //print the file name
Serial.println();
delay(t);
}
}
// Plays a full file from beginning to end with no pause.
void playcomplete(char *name) {
// call our helper to find and play this name
playfile(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 (!f.open(root, 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
wave.play();
}