#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 // CHAIR VARIABLES boolean seated = false; // this defines a boolean state of seated (true) or not seated (false); we are starting it at not seated boolean lastSeated = false; // stores the last state of the seated varible for next time through the loop #define LEDgreen 15 // the pin for the LED #define LEDred 14 // the pin for the LED #define seatSensor 7 // this is the sensor, we will need to define it as "on" or "off", this currently sets its input to analog pin 0 int sitting = 0; // sitting will be used to store the state of the input pin unsigned long sitStart; // this will mark the time that sitting starts, in millis unsigned long lastSit; // this will mark the time that sitting stops, in millis unsigned long sitTime; // this is how long someone has been sitting int played = 0; int comeBack = 0; int sitCategory; // this is the sit length category, for time ranges that a person is sitting long timeout = 10000; // length of time you have to be standing to end the seated session // DONE CHAIR VARIABLES #define DEBOUNCE 100 // button debouncer // 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); // SETUP CHAIR pinMode(LEDgreen, OUTPUT); // tell Arduino LED is an output pinMode(LEDred, OUTPUT); // tell Arduino LED is an output pinMode(seatSensor, INPUT); // and seatSensor is an input 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); // 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() { // DETERMINE THE STATE BY SEEING IF SOMEONE IS SITTING // sitting = digitalRead(seatSensor); // read to see if someone is sitting if(sitting == HIGH) { // if sitting is true lastSit = millis(); // this is constantly updating while the person is sitting seated = true; // change state to seated = true } if(sitting == LOW && (millis()-lastSit > timeout) ){ seated = false; } // DEFINING ACTIONS DURING THE BOOLEAN STATES if (seated == true && lastSeated == false){ sitStart = millis(); // then mark time that sitting started } sitTime = millis()-sitStart; //Determine the sitCategory // this determines categories by sitTime range if (sitTime < (5*1000)) { sitCategory = 1; } else if (sitTime >= (5*1000) && sitTime < (10*1000)) { sitCategory = 2; } else if (sitTime >= (15*1000) && sitTime < (20*1000)) { sitCategory = 3; } else if (sitTime >= (20*1000) && sitTime < (25*1000)) { sitCategory = 4; } else if (sitTime >= (30*1000) && sitTime < (35*1000)) { sitCategory = 5; } // else if (sitTime >= (40*1000) && sitTime < (50*1000)) { // can't get the chair out of cat 5 and into cat 6 for some reason? // sitCategory = 6; // } else { // random noises inbetween categories - works!! sitCategory = 7; } if (seated == true && sitting == true){ //Switch cases tell the chair what to do during each time range switch (sitCategory) { case 1: Serial.println("Welcome the sitter"); Serial.println(millis()); digitalWrite(LEDgreen, HIGH); // turn green led ON digitalWrite(LEDred, LOW); // turn red led off if (played != 1) { int randomGreeting = random (1,5); // picks a number between 1 and 4 (5 excluded) switch (randomGreeting) { case 1: playcomplete("1_2.wav"); break; case 2: playcomplete("1_3.wav"); break; case 3: playcomplete("1_4.wav"); break; case 4: playcomplete("1_6.wav"); break; } played = 1; comeBack = 0; } break; case 2: Serial.println("Prod 2"); Serial.println(millis()); if (played != 2) { int randomProd2 = random (1,5); // picks a number between 1 and 4 (5 excluded) switch (randomProd2) { case 1: playcomplete("2_1.wav"); break; case 2: playcomplete("2_2.wav"); break; case 3: playcomplete("2_3.wav"); break; case 4: playcomplete("2_4.wav"); break; } played = 2; comeBack = 0; } break; case 3: Serial.println("Prod 3"); Serial.println(millis()); if (played != 3) { int randomProd3 = random (1,4); // picks a number between 1 and 3 (4 excluded) switch (randomProd3) { case 1: playcomplete("3_1.wav"); break; case 2: playcomplete("3_2.wav"); break; case 3: playcomplete("3_3.wav"); break; } played = 3; comeBack = 0; } break; case 4: Serial.println("Prod 4"); Serial.println(millis()); if (played != 4) { int randomProd4 = random (1,4); // picks a number between 1 and 3 (4 excluded) switch (randomProd4) { case 1: playcomplete("4_1.wav"); break; case 2: playcomplete("4_2.wav"); break; case 3: playcomplete("4_3.wav"); break; } played = 4; comeBack = 0; } break; case 5: Serial.println("Prod 5"); Serial.println(millis()); if (played != 5) { int randomProd5 = random (1,6); // picks a number between 1 and 5 (6 excluded) switch (randomProd5) { case 1: playcomplete("5_1.wav"); break; case 2: playcomplete("5_2.wav"); break; case 3: playcomplete("5_3.wav"); break; case 4: playcomplete("6_1.wav"); break; case 5: playcomplete("6_2.wav"); break; } played = 5; comeBack = 0; } break; // case 6: // Serial.println("Prod 6"); // if (played != 6) { // int randomProd6 = random (1,3); // picks a number between 1 and 2 (3 excluded) // switch (randomProd6) { // case 1: // playcomplete("6_1.wav"); // break; // case 2: // playcomplete("6_2.wav"); // break; // } // played = 6; // } // break; // // // Can`t get it out of case 5 and into case 6 for some reason...? case 7: // random noises played inbetween other categories Serial.println("RANDOM sitCategory!!"); Serial.println(millis()); // if (played != 6) { long randomNum = random(1000); if (randomNum < 5) { Serial.println("random noise!!"); // playcomplete("x_4.wav"); int randomNoise = random (1,10); // picks a number between 1 and 9 (10 excluded) switch (randomNoise) { case 1: playcomplete("x_1.wav"); break; case 2: playcomplete("x_4.wav"); break; case 3: playcomplete("x_5.wav"); break; case 4: playcomplete("x_7.wav"); break; case 5: playcomplete("x_8.wav"); break; case 6: playcomplete("x_9.wav"); break; case 7: playcomplete("x_10.wav"); break; case 8: playcomplete("x_11.wav"); break; case 9: playcomplete("x_12.wav"); break; } } break; } } if (seated == false) { digitalWrite(LEDgreen, LOW); // turn green led off digitalWrite(LEDred, HIGH); // turn red led on Serial.println("No one sitting"); // what do we want the chair to do when no one is around? } else { digitalWrite(LEDgreen, HIGH); // turn green led ON digitalWrite(LEDred, LOW); // turn red led off } lastSeated = seated; if(sitting == LOW && seated == true && lastSeated == true ){ Serial.println("Sitter left! Come back!"); if (comeBack != 1){ delay(2000); int randomLeft = random (1,9); // picks a number between 1 and 5 (6 excluded) switch (randomLeft) { case 1: playcomplete("left_1.wav"); break; case 2: playcomplete("left_2.wav"); break; case 3: playcomplete("left_3.wav"); break; case 4: playcomplete("left_4.wav"); break; case 5: playcomplete("left_5.wav"); break; case 6: playcomplete("left_6.wav"); break; case 7: playcomplete("left_7.wav"); break; case 8: playcomplete("left_8.wav"); break; } comeBack = 1; } } } // 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(); }