I started work tonight on making the joystick on Molly’s Bar do something.
I used the adafruit Wave Shield to make an arduino play sounds when buttons are pressed.
Here’s the arduino sourcecode:
/*
MollyBar
Controls the lighting, music and breathalyzer on Molly's Bar according to the joystick input
*/
#include <FatReader.h>
#include <SdReader.h>
#include <avr/pgmspace.h>
#include "WaveUtil.h"
#include "WaveHC.h"
// The density of output to Serial is determined by the value of this constant
#define DEBUG 0
// Action queue
#define QUEUESIZE 16
volatile int QueueStart = 0;
volatile int QueueEnd = 0;
volatile int QueueSize = 0;
volatile int Queue[QUEUESIZE];
// Buttons
#define BUTTONZERO 6
#define NBUTTONS 6
#define NSOUNDBUTTONS 2
#define DEBOUNCE 100 // button debouncer; don't press the buttons too quickly
// Sound files
char* SoundFileNames[] = { "Shots.wav", "LineEmUp.wav" };
// Wave Shield variables
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
// 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 freememory;
if((int)__brkval == 0) {
freememory = ( (int)&freememory ) - ( (int)&__bss_end );
}
else {
freememory = ( (int)&freememory ) - ( (int)__brkval );
}
return freememory;
}
// Check for an error with the SD card
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);
}
// Setup
void setup() {
// Set up serial port
Serial.begin(9600);
Debug( "Molly's Bar Joystick!", true );
if (DEBUG) {
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. This pins are defined in the library
pinMode( 2, OUTPUT );
pinMode( 3, OUTPUT );
pinMode( 4, OUTPUT );
pinMode( 5, OUTPUT );
// pin13 LED
pinMode( 13, OUTPUT );
// Enable joystick buttons
for ( byte i = 0; i < NBUTTONS; i++ ) {
pinMode( BUTTONZERO + i, INPUT );
digitalWrite( BUTTONZERO + i, HIGH );
}
// 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!
}
// Tell the user about what we found
if (DEBUG) {
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.
Debug( "Ready!", true );
}
// The main execution loop
void loop() {
// Enqueue any new button press
int buttonpressed = ButtonPressed();
if ( buttonpressed >= 0 ) {
if ( buttonpressed < NSOUNDBUTTONS ) {
if( Enqueue(buttonpressed) ) {
Debug( "Enqueued \"", false );
Debug( SoundFileNames[buttonpressed], false);
Debug( "\"", true );
}
else {
Debug( "Enqueue failed: queue is full.", true );
}
}
else if (DEBUG) {
putstring("No action associated with button ");
Serial.println( buttonpressed, DEC );
}
}
// Dequeue an existing action
int action = Dequeue();
if ( action >= 0 ) {
Debug( "Dequeue \"", false );
Debug( SoundFileNames[action], false );
Debug( "\"", true );
PlayFile(SoundFileNames[action]);
}
}
// Output to Serial if the DEBUG flag is on
void Debug(char *message, boolean newline) {
if (DEBUG) {
Serial.print(message);
if (newline) {
Serial.println();
}
}
}
// Check to see if a button was pressed
int ButtonPressed() {
static byte previous[NBUTTONS];
static long time[NBUTTONS];
byte reading;
int pressed;
byte index;
pressed = -1;
for ( byte i = 0; i < NBUTTONS; i++ ) {
reading = digitalRead( BUTTONZERO + i );
if ( reading == LOW && previous[i] == HIGH && millis() - time[i] > DEBOUNCE ) {
// Button pressed
time[i] = millis();
pressed = i;
break;
}
previous[i] = reading;
}
// return button number (0 - 5)
return pressed;
}
// Enqueue an action
boolean Enqueue(int action) {
if ( QueueSize < QUEUESIZE ) {
QueueSize++;
Queue[QueueEnd] = action;
QueueEnd = ( QueueEnd + 1 ) % QUEUESIZE;
return true;
}
return false;
}
// Dequeue an action
int Dequeue() {
int action;
if ( QueueSize > 0 ) {
action = Queue[QueueStart];
QueueStart = ( QueueStart + 1 ) % QUEUESIZE;
QueueSize--;
}
else {
action = -1;
}
return action;
}
// 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();
}
And here’s some video of the thing in action:
20110718-200559.mov






















































