Advertisement
joric

Exterminate.uno

Jun 16th, 2016
322
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.14 KB | None | 0 0
  1. // exterminate! video: http://webm.armarium.org/i/zBTtIo.mp4
  2.  
  3. #include <SPI.h>
  4. #include <SD.h>
  5. #include <TMRpcm.h>
  6. #include <Ultrasonic.h>
  7. #include <Servo.h>
  8.  
  9. const int chipSelect = 10;
  10. const int speaker = 9;
  11. const int servoPin = A0;
  12. const int ultraTrq = 2;
  13. const int ultraEcho = 3;
  14.  
  15. char * fname = (char*)"EXTERMIN.WAV";
  16.  
  17. Servo servo;
  18. TMRpcm tmrpcm;
  19. Ultrasonic ultrasonic(ultraTrq, ultraEcho);
  20.  
  21. Sd2Card card;
  22. SdVolume volume;
  23. SdFile root;
  24.  
  25. void setup(){  
  26.   Serial.begin(9600);
  27.   delay(2000);
  28.  
  29.   Serial.print("\nInitializing SD card...");
  30.  
  31.   // we'll use the initialization code from the utility libraries
  32.   // since we're just testing if the card is working!
  33.   if (!card.init(SPI_HALF_SPEED, chipSelect)) {
  34.     Serial.println("initialization failed. Things to check:");
  35.     Serial.println("* is a card inserted?");
  36.     Serial.println("* is your wiring correct?");
  37.     Serial.println("* did you change the chipSelect pin to match your shield or module?");
  38.     return;
  39.   } else {
  40.     Serial.println("Wiring is correct and a card is present.");
  41.   }
  42.  
  43.   // print the type of card
  44.   Serial.print("\nCard type: ");
  45.   switch (card.type()) {
  46.     case SD_CARD_TYPE_SD1:
  47.       Serial.println("SD1");
  48.       break;
  49.     case SD_CARD_TYPE_SD2:
  50.       Serial.println("SD2");
  51.       break;
  52.     case SD_CARD_TYPE_SDHC:
  53.       Serial.println("SDHC");
  54.       break;
  55.     default:
  56.       Serial.println("Unknown");
  57.   }
  58.  
  59.   // Now we will try to open the 'volume'/'partition' - it should be FAT16 or FAT32
  60.   if (!volume.init(card)) {
  61.     Serial.println("Could not find FAT16/FAT32 partition.\nMake sure you've formatted the card");
  62.     return;
  63.   }
  64.  
  65.   // print the type and size of the first FAT-type volume
  66.   uint32_t volumesize;
  67.   Serial.print("\nVolume type is FAT");
  68.   Serial.println(volume.fatType(), DEC);
  69.   Serial.println();
  70.  
  71.   volumesize = volume.blocksPerCluster();    // clusters are collections of blocks
  72.   volumesize *= volume.clusterCount();       // we'll have a lot of clusters
  73.   volumesize *= 512;                            // SD card blocks are always 512 bytes
  74.   Serial.print("Volume size (bytes): ");
  75.   Serial.println(volumesize);
  76.   Serial.print("Volume size (Kbytes): ");
  77.   volumesize /= 1024;
  78.   Serial.println(volumesize);
  79.   Serial.print("Volume size (Mbytes): ");
  80.   volumesize /= 1024;
  81.   Serial.println(volumesize);
  82.  
  83.   Serial.println("\nFiles found on the card (name, date and size in bytes): ");
  84.   root.openRoot(volume);
  85.  
  86.   // list all files in the card with date and size
  87.   root.ls(LS_R | LS_DATE | LS_SIZE);
  88.  
  89.   // see if the card is present and can be initialized:
  90.   if (!SD.begin(chipSelect)) {
  91.     Serial.println("Card failed, or not present");
  92.     // don't do anything more:
  93.     return;
  94.   }
  95.   Serial.println("card initialized.");
  96.  
  97.   tmrpcm.speakerPin = speaker;
  98.   tmrpcm.volume(1);
  99. }
  100.  
  101. void loop(){
  102.   float dist_cm = ultrasonic.Ranging(CM);
  103.   if (dist_cm<10) {
  104.     tmrpcm.play(fname);
  105.     delay(2000);          
  106.     servo.attach(servoPin);    
  107.     servo.write(180);
  108.     delay(500);
  109.     servo.write(0);
  110.     delay(500);    
  111.     servo.write(90);
  112.     delay(500);
  113.     servo.detach();
  114.     delay(1000);
  115.   }
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement