// Source code used for John Wilds Descartes Corridor. Shown at Elevator Gallery London 28/10/10 Ð 14/11/10 // The program uses a LV MaxSonar-EZ0 + an Adafruit Wave Shield v1.1 // Wire LV MaxSonar-EZ0 // GRD -> GRD // +5 -> 5v // pw -> Arduino pin 7 //Digital pin 7 for reading in the pulse width from the MaxSonar device. //This variable is a constant because the pin will not change throughout execution of this code. // Audio Files Stored on the Wave Shield /* 10 - t .wav 11 Ð l .wav 12 Ð z .wav 13 Ð m .wav 14 Ð o .wav 15 Ð k .wav 16 Ð x .wav 17 Ð q .wav 18 Ð b .wav 19 Ð y .wav 20 - w .wav 30 - r .wav 40 - f .wav 50 - i .wav 60 - s .wav 70 - v .wav 80 - e .wav 90 - n .wav h - hundred d - the distence from here to you is a Ð and c Ð cm */ #include "SdReader.h" #include "FatReader.h" #include "WaveHC.h" #include #include "WaveUtil.h" SdReader card; FatVolume vol; FatReader root; FatReader f; WaveHC wave; // only one! char teens[10] = { 't', 'l', 'z', 'm', 'o', 'k', 'x', 'q', 'b', 'y'}; char tens[10] = { 'j', 't', 'w', 'r', 'f', 'i', 's', 'v', 'e', 'n'}; const int pwPin = 7; //variables needed to store values long pulse, inches; int cm; String distance = 'xxx'; String distanceCode = 'xxxx'; String units = 'x'; void setup() { //This opens up a serial connection to shoot the results back to the PC console //Only needed when testing. Serial.begin(9600); Serial.print('started'); pinMode(2, OUTPUT); pinMode(3, OUTPUT); pinMode(4, OUTPUT); pinMode(5, OUTPUT); if (!card.init()) { putstring_nl("Card init. failed!"); return; } if (!vol.init(card)) { putstring_nl("No partition!"); return; } if (!root.openRoot(vol)) { putstring_nl("Couldn't open dir"); return; } putstring_nl("Files found:"); ls(); } void loop() { pinMode(pwPin, INPUT); //Used to read in the pulse that is being sent by the MaxSonar device. //Pulse Width representation with a scale factor of 147 uS per Inch. pulse = pulseIn(pwPin, HIGH); //147uS per inch inches = pulse/147; //change inches to centimetres cm = inches * 2.54; distance = String(cm); ////////////////////// if (cm < 620) // ignore all values above 620 { //Add case codes u- units e - teens t - tens (10,20,30....) h - hundreds if (cm > 0 && cm <= 9) { units = 'u'; } else if (cm > 9 && cm < 20) { units = 'e'; } else if (cm > 20 && cm < 100) { units = 't'; } else if (cm > 99 && cm <= 620) { units = 'h'; //deal with straight houndreds. if (cm == 100 || cm == 200 || cm == 300 || cm == 400 || cm == 500 || cm == 600 || cm == 700) { units = 'H'; } // } // add case code to distance distanceCode = String(units + distance); // char c = 'x'; int var = 0; Serial.print(cm); Serial.print("cm :: distanceCode = "); Serial.print(distanceCode); Serial.println(); // speak switch (distanceCode.charAt(0)) { // 0 - 9cm case 'u': // u- units c = distanceCode.charAt(1); speaknum(c); delay(10); c = 'c'; // centemeters speaknum(c); delay(30); break; // 10 - 19 case 'e': // e - teens var = distanceCode.charAt(2)-48; c = teens[var]; speaknum(c); delay(10); c = 'c'; // centemeters Serial.print(c); speaknum(c); delay(30); break; // 20 -99 case 't': //t - tens (10,20,30....) Serial.println("t"); var = distanceCode.charAt(1)-48; c = tens[var]; speaknum(c); delay(20); if (distanceCode.charAt(2) != 0) { c = distanceCode.charAt(2); speaknum(c); delay(10); } c = 'c'; // centemeters speaknum(c); delay(30); break; // 100 - 900 case 'h': Serial.println("h"); c = distanceCode.charAt(1); speaknum(c); delay(10); c = 'h'; //houndred speaknum(c); delay(20); c = 'a'; //and speaknum(c); delay(20); var = distanceCode.charAt(2)-48; c = tens[var]; speaknum(c); delay(20); if (distanceCode.charAt(3) != 0) { c = distanceCode.charAt(3); speaknum(c); delay(10); } c = 'c'; // centemeters speaknum(c); delay(30); break; // straight houndreds case 'H': // h - hundreds Serial.println("H"); c = distanceCode.charAt(1); speaknum(c); delay(10); c = 'h'; //houndred speaknum(c); delay(20); c = 'c'; // centemeters speaknum(c); delay(30); break; default: Serial.println("NON"); } // delay(1000); }// endif cm < 620 } char filename[13]; void speaknum(char c) { filename[0] = c; int i = 1; filename[i] = '.'; filename[i+1] = 'W'; filename[i+2] = 'A'; filename[i+3] = 'V'; filename[i+4] = 0; Serial.print(filename); playcomplete(filename); } /* * print dir name field * */ void printName(dir_t &dir) { for (uint8_t i = 0; i < 11; i++) { if (dir.name[i] == ' ')continue; if (i == 8) Serial.print('.'); Serial.print(dir.name[i]); } if (DIR_IS_SUBDIR(dir)) Serial.print('/'); } void ls() { dir_t d; root.rewind(); while (root.readDir(d) > 0) { printName(d); Serial.println(); } } void playcomplete(char *name) { playfile(name); while (wave.isplaying); } void playfile(char *name) { if (wave.isplaying) {// already playing something, so stop it! wave.stop(); // stop it } if (!f.open(root, name)) { putstring("Couldn't open file "); Serial.print(name); return; } if (!wave.create(f)) { putstring_nl("Not a valid WAV"); return; } // ok time to play! wave.play(); }