« Ben's Sound Sketch 2 | Main | Meng Tang's Embodied Sound »

Arduino/Wave Shield Code for Globe Project

So, here are a few helpful hints for anybody who is brave enough to tackle to wave shield. Most of the work stems from this sketch on the Wave Shield site: http://www.ladyada.net/forums/viewtopic.php?t=6120

It plays sound files based on switches which are attached to the 6 analog inputs of the Arduino. What it doesn't tell you is that there are also 4 unused digital pins that can be used in the same way. You can simply add this code to the setup() function to do so:

pinMode(6, INPUT);
pinMode(7, INPUT);
pinMode(8, INPUT);
pinMode(9, INPUT);

pins 6 - 9 are the four digital pins that are open, and pins 14 - 19 in code represent analog pins 0 - 5 on the arduino board.

Now in order to play different combinations of files, we had to tell the code that "if this button is pressed, and then this button, do this. Or if..."

There is a simple way to do this with "for" loops, but it couldn't be figured out in time. So each possible button combination was hardcoded (written individually). Below is the code. It gets ugly. If you figure out a way to simplify it with the for loops, let us know!!


#include
#include
#include "util.h"
#include "wave.h"

#define DEBOUNCE 20

#define swPin 14

AF_Wave card;
File f;
Wavefile wave;

void setup() {
byte i;

// set up serial port
Serial.begin(9600);

// set up waveshield pins
for (byte i = 2; i <= 5; ++i) {
pinMode(i, OUTPUT);
}
pinMode(6, INPUT);
pinMode(7, INPUT);
pinMode(8, INPUT);
pinMode(9, INPUT);
// enable pull-up resistors on
// switch pins (analog inputs)
for (byte i = swPin; i <= swPin + 5; ++i) {
digitalWrite(i, HIGH);
}

// open memory card
if (!card.init_card()) {
putstring_nl("Card init failed!"); return;
}
if (!card.open_partition()) {
putstring_nl("No partition!"); return;
}
if (!card.open_filesys()) {
putstring_nl("Couldn't open filesys"); return;
}
if (!card.open_rootdir()) {
putstring_nl("Couldn't open dir"); return;
}
}

void loop() {
if(digitalRead(6) == HIGH) {
playfile("America.wav");
if(digitalRead(7) == HIGH) {
playfile("America - Africa.wav");
}
if(digitalRead(8) == HIGH) {
playfile("America - Australia.wav");
}
if(digitalRead(9) == HIGH) {
playfile("America - British.wav");
}
if(digitalRead(14) == HIGH) {
playfile("America - China.wav");
}
if(digitalRead(15) == HIGH) {
playfile("America - Germany.wav");
}
if(digitalRead(16) == HIGH) {
playfile("America - India.wav");
}
if(digitalRead(17) == HIGH) {
playfile("America - Italy.wav");
}
if(digitalRead(18) == HIGH) {
playfile("America - Russia.wav");
}
if(digitalRead(19) == HIGH) {
playfile("America - Spanish.wav");
}
}

if(digitalRead(7) == HIGH) {
playfile("Africa.wav");
if(digitalRead(14) == HIGH) {
playfile("Africa - China.wav");
}
}

if(digitalRead(8) == HIGH) {
playfile("Australia.wav");
if(digitalRead(7) == HIGH) {
playfile("Australia - Africa.wav");
}
if(digitalRead(6) == HIGH) {
playfile("Australia - America.wav");
}
if(digitalRead(9) == HIGH) {
playfile("Australia - British.wav");
}
if(digitalRead(14) == HIGH) {
playfile("Australia - China.wav");
}
if(digitalRead(15) == HIGH) {
playfile("Australia - Germany.wav");
}
if(digitalRead(16) == HIGH) {
playfile("Australia - India.wav");
}
if(digitalRead(17) == HIGH) {
playfile("Australia - Italy.wav");
}
if(digitalRead(18) == HIGH) {
playfile("Australia - Russia.wav");
}
if(digitalRead(19) == HIGH) {
playfile("Australia - Spanish.wav");
}
}

if(digitalRead(9) == HIGH) {
playfile("British.wav");
}

if(digitalRead(14) == HIGH) {
playfile("China.wav");
if(digitalRead(7) == HIGH) {
playfile("China - Africa.wav");
}
if(digitalRead(8) == HIGH) {
playfile("China - Australia.wav");
}
if(digitalRead(9) == HIGH) {
playfile("China - British.wav");
}
if(digitalRead(6) == HIGH) {
playfile("China - America.wav");
}
if(digitalRead(15) == HIGH) {
playfile("China - Germany.wav");
}
if(digitalRead(16) == HIGH) {
playfile("China - India.wav");
}
if(digitalRead(17) == HIGH) {
playfile("China - Italy.wav");
}
if(digitalRead(18) == HIGH) {
playfile("China - Russia.wav");
}
if(digitalRead(19) == HIGH) {
playfile("China - Spanish.wav");
}
}

if(digitalRead(15) == HIGH) {
playfile("Germany.wav");
if(digitalRead(7) == HIGH) {
playfile("Germany - Africa.wav");
}
if(digitalRead(8) == HIGH) {
playfile("Germany - Australia.wav");
}
if(digitalRead(9) == HIGH) {
playfile("Germany - British.wav");
}
if(digitalRead(14) == HIGH) {
playfile("Germany - China.wav");
}
if(digitalRead(6) == HIGH) {
playfile("Germany - America.wav");
}
if(digitalRead(16) == HIGH) {
playfile("Germany - India.wav");
}
if(digitalRead(17) == HIGH) {
playfile("Germany - Italy.wav");
}
if(digitalRead(18) == HIGH) {
playfile("Germany - Russia.wav");
}
if(digitalRead(19) == HIGH) {
playfile("Germany - Spanish.wav");
}
}

if(digitalRead(16) == HIGH) {
playfile("India.wav");
if(digitalRead(7) == HIGH) {
playfile("India - Africa.wav");
}
if(digitalRead(8) == HIGH) {
playfile("India - Australia.wav");
}
if(digitalRead(9) == HIGH) {
playfile("India - British.wav");
}
if(digitalRead(14) == HIGH) {
playfile("India - China.wav");
}
if(digitalRead(15) == HIGH) {
playfile("India - Germany.wav");
}
if(digitalRead(6) == HIGH) {
playfile("India - America.wav");
}
if(digitalRead(17) == HIGH) {
playfile("India - Italy.wav");
}
if(digitalRead(18) == HIGH) {
playfile("India - Russia.wav");
}
if(digitalRead(19) == HIGH) {
playfile("India - Spanish.wav");
}
}

if(digitalRead(17) == HIGH) {
playfile("Italy.wav");
if(digitalRead(6) == HIGH) {
playfile("Italy - America.wav");
}
}

if(digitalRead(18) == HIGH) {
playfile("Russia.wav");
if(digitalRead(7) == HIGH) {
playfile("Russia - Africa.wav");
}
if(digitalRead(8) == HIGH) {
playfile("Russia - Australia.wav");
}
if(digitalRead(9) == HIGH) {
playfile("Russia - British.wav");
}
if(digitalRead(14) == HIGH) {
playfile("Russia - China.wav");
}
if(digitalRead(15) == HIGH) {
playfile("Russia - Germany.wav");
}
if(digitalRead(16) == HIGH) {
playfile("Russia - India.wav");
}
if(digitalRead(17) == HIGH) {
playfile("Russia - Italy.wav");
}
if(digitalRead(6) == HIGH) {
playfile("Russia - America.wav");
}
if(digitalRead(19) == HIGH) {
playfile("Russia - Spanish.wav");
}
}

if(digitalRead(19) == HIGH) {
playfile("Spanish.wav");
}


byte check_switches()
{
static byte previous[6];
static long time[6];
byte reading;
byte pressed;
byte index;

for (index = 0; index < 6; ++index) {
reading = digitalRead(swPin + index);
if (reading == LOW && previous[index] == HIGH && millis() - time[index] > DEBOUNCE)
{
// switch pressed
time[index] = millis();
pressed = index + 1;
break;
}
else {
pressed = 0;
}
previous[index] = reading;
}
if(pressed == 0) {
for(index = 6; index <= 9; ++index) {
reading = digitalRead(index);
if(reading == LOW && previous[index] == HIGH && millis() - time[index] > DEBOUNCE)
{
time[index] = millis();
pressed = index + 1 + 14;
break;
}
previous[index] = reading;
}
}
// return switch number (1 - 6)
return (pressed);
}

void playfile(char *name) {
// stop any file already playing
if (wave.isplaying) {
wave.stop();
}
// close file if open
if (f) {
card.close_file(f);
}
// play specified file
f = card.open_file(name);
if (f && wave.create(f)) {
wave.play();
}
}