ARDUINO CODE - SUPERNATURAL - AUDIONATION PROJECT

//#include <NewSoftSerial.h>

#include <AF_Wave.h>

#include <avr/pgmspace.h>

#include "util.h"

#include "wave.h"

//#define BT_RX 12

//#define BT_TX 13

//NewSoftSerial Serial(BT_RX, BT_TX);

//#define ONE "01.WAV"

AF_Wave card;

File f;

Wavefile wave; // only one!

#define redled 9

uint16_t samplerate;

///////////////////////////////////////////////////////////

////////// BT RADIO Control //////////

////////////////////////////////////////////////////////////

#define TEMP_ARRAY 195

#define CLIENT_BT_ADDRESS 21

#define CLIENT_BT_MAX 9

char btClientsArray[CLIENT_BT_MAX][CLIENT_BT_ADDRESS];

char btClientTemp[TEMP_ARRAY];

////////////////////////////////////////////////////////////

////////// Scan Radio Helper Functions /////////////////////

////////// Author: Elio Bidinost /////////////////////

////////////////////////////////////////////////////////////

void initArray(){

for (int i = 0; i < TEMP_ARRAY; i++) { btClientTemp[i] = 0; }

}

////////////////////////////////////////////////////////////

void initMasterArray(){

for (int i = 0; i < CLIENT_BT_MAX; i++)

{

for (int j = 0; j < CLIENT_BT_ADDRESS; j++)

{

btClientsArray[i][j] = 0;

}

////////////////////////////////////////////////////////////

void btRadioReboot()

{

Serial.print("R,1\r");

delay(2500);

}

////////////////////////////////////////////////////////////

int waitOK(char delimit)

{

char waitByte = 0;

int messageValue = 0;

int cnt = 0;

while(waitByte != delimit)

{

if(Serial.available())

{

waitByte = Serial.read();

messageValue += waitByte;

//Serial.print(waitByte);

if(delimit == ' ') { btClientTemp[cnt] = waitByte; cnt++; }

}

}

return messageValue;

}

////////////////////////////////////////////////////////////

void enterCmdMode()

{

Serial.flush();

Serial.print("$$$");

//Serial.print("$$$");

// CMD

if (waitOK('\r') == 225)

{

delay(500);

}

////////////////////////////////////////////////////////////

void exitCmdMode()

{

Serial.flush();

Serial.print("---\r");

//Serial.print("---\r");

// END

if (waitOK('\r') == 228)

{

delay(500);

}

////////////////////////////////////////////////////////////

void enterScanMode()

{

Serial.flush();

Serial.print("IN\r");

//Serial.print("I\r");

delay(25);

initArray();

int nodesFound = 0;

int stripOut = waitOK('\n');

while(!(waitOK(' ')))

{

// Get All BT Clients.

}

Serial.flush();

}

////////////////////////////////////////////////////////////

int countAndSortClients()

{

int cnt = 0;

int numClients = 0;

initMasterArray();

// Check if No Clients (78 + 110 = 189)

if ((numClients = btClientTemp[0] + btClientTemp[1]) == 189)

{

Serial.println("No Devices Found");

numClients = 0;

}

else

{

int i, j, k;

i = j = k = 0;

while((k < TEMP_ARRAY) && (j < CLIENT_BT_ADDRESS))

{

if(btClientTemp[k] != 13)

{

btClientsArray[i][j] = btClientTemp[k];

k++; j++;

}

else

{

k += 2; i++; j = 0;

}

}

numClients = i;

}

return numClients;

}

////////////////////////////////////////////////////////////

void ls() {

char name[13];

int ret;

card.reset_dir();

putstring_nl("Files found:");

while (1) {

ret = card.get_next_name_in_dir(name);

if (!ret) {

card.reset_dir();

return;

}

Serial.println(name);

}

}

////////////////////////////////////////////////////////////

void playfile(char *name) {

f = card.open_file(name);

if (!f) {

putstring_nl(" Couldn't open file"); return;

}

if (!wave.create(f)) {

putstring_nl(" Not a valid WAV"); return;

}

wave.play();

while (wave.isplaying == 1){

delay(1);

}

//if (wave.isplaying == 1) {

// delay(500);

// if (wave.isplaying == 1) {

// delay(500);

// if (wave.isplaying == 1) {

// delay(500);

// if (wave.isplaying == 1) {

// delay(500);

//}

//else (){

card.close_file(f);

//}

}

////////////////////////////////////////////////////////////

void setup()

{

Serial.begin(9600);

//Serial.begin(9600);

pinMode(2, OUTPUT);

pinMode(3, OUTPUT);

pinMode(4, OUTPUT);

pinMode(5, OUTPUT);

pinMode(redled, OUTPUT);

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; }

// Reboot the BT Radio - Arduino has been Reset!

btRadioReboot();

initArray();

Serial.println("System Started - Initial BT Scan");

//ls();

}

////////////////////////////////////////////////////////////

void loop()

{

char name = 0;

delay(25);

enterCmdMode();

enterScanMode();

exitCmdMode();

delay(25);

int btClients = countAndSortClients();

delay(10);

Serial.print("BT Clients Found: ");

Serial.println(btClients);

//for (int i = 0; i < TEMP_ARRAY; i++) { Serial.print(btClientTemp[i]); }

for (int i = 0; i < btClients; i++)

{

for (int j = 0; j < CLIENT_BT_ADDRESS; j++)

{

//Serial.print(btClientsArray[i][j]);

//int s = btClientsArray[i][j];

//map(s, 0, 127, 0, 28);

//Serial.println("new S is");

//Serial.print(s);

//Serial.println(Sound[x][0]);

getName(btClientsArray[i][j]);

//playfile(getName(s));

//playfile(ONE);

// PLAN B

//analogWrite(6, btClientsArray[i][j]*10);

}

Serial.println("");

}

Serial.println("");

}

////////////////////////////////////////////////////////////

void getName(int s) {

Serial.print(s);

char* file;

switch(s)

{

//////////////////////////////////////////////////////////////

///Artist/Composer: Clara Massa

///http://www.archive.org/details/Audio_clips_41

///Creative Commons license: Attribution 3.0

///guitar_drone.wav

//////////////////////////////////////////////////////////////

case 0:

playcomplete("01.WAV");

break;

case 1:

playcomplete("02.WAV");

break;

case 2:

playcomplete("03.WAV");

break;

case 3:

playcomplete("04.WAV");

break;

case 4:

playcomplete("05.WAV");

break;

case 5:

playcomplete("06.WAV");

break;

case 6:

playcomplete("07.WAV");

break;

case 7:

playcomplete("08.WAV");

break;

case 8:

playcomplete("09.WAV");

break;

case 9:

playcomplete("10.WAV");

break;

case 44:

playcomplete("12.WAV");

break;

case 45:

playcomplete("11.WAV");

break;

case 46:

playcomplete("06.WAV");

break;

case 47:

playcomplete("59.WAV");

break;

case 48:

playcomplete("58.WAV");

break;

case 49:

playcomplete("57.WAV");

break;

case 50:

playcomplete("56.WAV");

break;

case 51:

playcomplete("55.WAV");

break;

case 52:

playcomplete("05.WAV");

break;

case 53:

playcomplete("53.WAV");

break;

case 54:

playcomplete("52.WAV");

break;

case 55:

playcomplete("51.WAV");

break;

case 56:

playcomplete("50.WAV");

break;

case 57:

playcomplete("03.WAV");

break;

case 58:

playcomplete("48.WAV");

break;

case 59:

playcomplete("47.WAV");

break;

case 60:

playcomplete("46.WAV");

break;

case 61:

playcomplete("45.WAV");

break;

case 62:

playcomplete("44.WAV");

break;

case 63:

playcomplete("43.WAV");

break;

case 64:

playcomplete("42.WAV");

break;

case 65:

playcomplete("11.WAV");

break;

case 66:

playcomplete("12.WAV");

break;

case 67:

playcomplete("13.WAV");

break;

case 68:

playcomplete("14.WAV");

break;

case 69:

playcomplete("15.WAV");

break;

case 70:

playcomplete("16.WAV");

break;

case 71:

playcomplete("17.WAV");

break;

case 72:

playcomplete("18.WAV");

break;

case 73:

playcomplete("19.WAV");

break;

case 74:

playcomplete("20.WAV");

break;

case 75:

playcomplete("21.WAV");

break;

case 76:

playcomplete("22.WAV");

break;

case 77:

playcomplete("23.WAV");

break;

case 78:

playcomplete("24.WAV");

break;

case 79:

playcomplete("25.WAV");

break;

case 80:

playcomplete("26.WAV");

break;

case 81:

playcomplete("27.WAV");

break;

case 82:

playcomplete("28.WAV");

break;

case 83:

playcomplete("29.WAV");

break;

case 84:

playcomplete("30.WAV");

break;

case 85:

playcomplete("31.WAV");

break;

case 86:

playcomplete("32.WAV");

break;

case 87:

playcomplete("33.WAV");

break;

case 88:

playcomplete("34.WAV");

break;

case 89:

playcomplete("35.WAV");

break;

case 90:

playcomplete("36.WAV");

break;

case 91:

playcomplete("37.WAV");

break;

case 92:

playcomplete("38.WAV");

break;

case 93:

playcomplete("39.WAV");

break;

case 94:

playcomplete("40.WAV");

break;

case 95:

playcomplete("41.WAV");

break;

default:

playcomplete("01.WAV");

break;

}

 

}

void playcomplete(char *name)

{

// call our helper to find and play this name

playfile(name);

//while (wave.isplaying) {

// do nothin while its playing

//}

// now its done playing

}