Advertisement
Guest User

Arduino Doorbell

a guest
Mar 5th, 2013
380
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.82 KB | None | 0 0
  1. #include <PinChangeInt.h>
  2. #include <FatReader.h>
  3. #include <SdReader.h>
  4. #include <avr/pgmspace.h>
  5. #include <avr/sleep.h>
  6. #include "WaveUtil.h"
  7. #include "WaveHC.h"
  8.  
  9. SdReader card;    // This object holds the information for the card
  10. FatVolume vol;    // This holds the information for the partition on the card
  11. FatReader root;   // This holds the information for the filesystem on the card
  12.  
  13. WaveHC wave;      // This is the only wave (audio) object, since we will only play one at a time
  14.  
  15. int ledPin = 6;
  16. int intPin = 7;
  17. volatile boolean triggered = false;
  18.  
  19. /*
  20.  * print dir_t name field. The output is 8.3 format, so like SOUND.WAV or FILENAME.DAT
  21.  */
  22. void printName(dir_t &dir)
  23. {
  24.   for (uint8_t i = 0; i < 11; i++) {     // 8.3 format has 8+3 = 11 letters in it
  25.     if (dir.name[i] == ' ')
  26.         continue;         // dont print any spaces in the name
  27.     if (i == 8)
  28.         Serial.print('.');           // after the 8th letter, place a dot
  29.     Serial.write(dir.name[i]);      // print the n'th digit
  30.   }
  31.   if (DIR_IS_SUBDIR(dir))
  32.     Serial.print('/');       // directories get a / at the end
  33. }
  34.  
  35. /*
  36.  * print error message and halt if SD I/O error, great for debugging!
  37.  */
  38. void sdErrorCheck(void)
  39. {
  40.   if (!card.errorCode()) return;
  41.   putstring("\n\rSD I/O error: ");
  42.   Serial.print(card.errorCode(), HEX);
  43.   putstring(", ");
  44.   Serial.println(card.errorData(), HEX);
  45.   while(1);
  46. }
  47.  
  48. void beginSleep() {
  49.   putstring_nl("Going to sleep.");
  50.   delay(100);
  51.  
  52.  
  53.   set_sleep_mode(SLEEP_MODE_PWR_DOWN);
  54.   sleep_enable();
  55.   digitalWrite(ledPin, LOW);
  56.  
  57.   // Debounce the interrupt
  58.   while (digitalRead(intPin) == LOW) {
  59.       PCintPort::attachInterrupt(intPin, &interruptHandler, RISING);
  60.       sleep_cpu();
  61.       delay(10);
  62.   }
  63.  
  64.   sleep_disable();
  65.   digitalWrite(ledPin, HIGH);
  66. }
  67.  
  68. void setup() {
  69.   Serial.begin(9600);
  70.  
  71.   pinMode(intPin, INPUT);
  72.  
  73.   pinMode(ledPin, OUTPUT);
  74.   digitalWrite(ledPin, HIGH);
  75.  
  76.   // Audio shield
  77.   pinMode(2, OUTPUT);
  78.   pinMode(3, OUTPUT);
  79.   pinMode(4, OUTPUT);
  80.   pinMode(5, OUTPUT);
  81.  
  82.   if (!card.init()) {         //play with 8 MHz spi (default faster!)  
  83.     putstring_nl("Card init. failed!");  // Something went wrong, lets print out why
  84.     sdErrorCheck();
  85.     while(1);                            // then 'halt' - do nothing!
  86.   }
  87.  
  88.   // enable optimize read - some cards may timeout. Disable if you're having problems
  89.   card.partialBlockRead(true);
  90.  
  91.   // Now we will look for a FAT partition!
  92.   uint8_t part;
  93.   for (part = 0; part < 5; part++) {     // we have up to 5 slots to look in
  94.     if (vol.init(card, part))
  95.       break;                             // we found one, lets bail
  96.   }
  97.   if (part == 5) {                       // if we ended up not finding one  :(
  98.     putstring_nl("No valid FAT partition!");
  99.     sdErrorCheck();      // Something went wrong, lets print out why
  100.     while(1);                            // then 'halt' - do nothing!
  101.   }
  102.  
  103.   // Try to open the root directory
  104.   if (!root.openRoot(vol)) {
  105.     putstring_nl("Can't open root dir!"); // Something went wrong,
  106.     while(1);                             // then 'halt' - do nothing!
  107.   }
  108.  
  109.   putstring_nl("Ready.");
  110.   delay(100);
  111. }
  112.  
  113. void loop()
  114. {
  115.   dir_t dirBuf;
  116.   FatReader file;
  117.  
  118.   root.rewind();
  119.   while (root.readDir(dirBuf) > 0) {
  120.     if (dirBuf.name[0] == '.')
  121.       continue;
  122.  
  123.     if(file.open(vol, dirBuf) && !file.isDir()) {
  124.       putstring("Enqueued ");
  125.       printName(dirBuf);
  126.       putstring_nl("");
  127.      
  128.       beginSleep();
  129.      
  130.       if (wave.create(file)) {
  131.         wave.play();
  132.         putstring("Playing...");
  133.         while (wave.isplaying) {
  134.           delay(100);
  135.         }
  136.         putstring_nl("Done.");
  137.       }
  138.      
  139.       sdErrorCheck();
  140.     }
  141.   }
  142. }
  143.  
  144. void interruptHandler() {
  145.   PCintPort::detachInterrupt(intPin);
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement