Guest User

Untitled

a guest
Sep 12th, 2021
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <Servo.h>
  2. #include "WaveHC.h"
  3. #include "WaveUtil.h"
  4.  
  5. // DEFINES
  6. #define DEBOUNCE 100
  7. #define eyeleds 18
  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. FatReader f;      // This holds the information for the file we're playing
  13. uint8_t dirLevel; // indent level for file/dir names (for prettyprinting)
  14. dir_t dirBuf;     // buffer for directory reads
  15. WaveHC wave;      // This is the only wave (audio) object, since we will only play one at a time
  16. Servo servo;
  17.  
  18. int inputPin = 8;               // choose the input pin (for PIR sensor)
  19. int servoPin = 16;              // choose the input pin (for Servo)
  20. int pirState = LOW;             // we start, assuming no motion detected
  21. int val = HIGH;                    // variable for reading the pin status for motion sensor
  22. int minPulse     =  600;  // minimum servo position
  23. int maxPulse     =  2200; // maximum servo position
  24. int turnRate     =  1800;  // servo turn rate increment (larger value, faster rate)
  25. int refreshTime  =  20;   // time (ms) between pulses (50Hz)
  26. int mouthchange = 6;  //checks to see if mouth position needs to be changed
  27. int randNumber;   //random number variable to allow the choosing of which wav will be played
  28.  
  29.  
  30. /** The Arduino will calculate these values for you **/
  31. int centerServo;         // center servo position
  32. int pulseWidth;          // servo pulse width
  33. long lastPulse = 0;    // recorded time (ms) of the last pulse
  34.  
  35. int freeRam(void)
  36. {
  37.   extern int  __bss_end;
  38.   extern int  *__brkval;
  39.   int free_memory;
  40.   if((int)__brkval == 0) {
  41.     free_memory = ((int)&free_memory) - ((int)&__bss_end);
  42.   }
  43.   else {
  44.     free_memory = ((int)&free_memory) - ((int)__brkval);
  45.   }
  46.   return free_memory;
  47. }
  48.  
  49. // Error checking the SD Card
  50.  
  51. void sdErrorCheck(void)
  52. {
  53.   if (!card.errorCode()) return;
  54.   putstring("\n\rSD I/O error: ");
  55.   Serial.print(card.errorCode(), HEX);
  56.   putstring(", ");
  57.   Serial.println(card.errorData(), HEX);
  58.   while(1);
  59. }
  60.  
  61. void setup() {
  62.   // set up serial port
  63.   Serial.begin(9600);
  64.  
  65.   putstring_nl("Talking Skull");
  66.  
  67.   putstring("Free RAM: ");       // This can help with debugging, running out of RAM is bad
  68.   Serial.println(freeRam());     // if this is under 150 bytes it may spell trouble!
  69.  
  70.   pinMode(inputPin, INPUT);     // declare sensor as input for PIR
  71.   pinMode(eyeleds, OUTPUT);     // declare sensor as output for eyes
  72.  
  73.   // set up servo pin
  74.   servo.attach(9);
  75.   servo.write(0);
  76.   pinMode(servoPin, OUTPUT);  // Set servo pin 18 (analog 4) as an output pin
  77.   centerServo = maxPulse - ((maxPulse - minPulse)/2);
  78.   pulseWidth = centerServo;   // Give the servo a starting point (or it floats)
  79.  
  80.  
  81.   // set up waveshield pins
  82.   pinMode(2, OUTPUT);
  83.   pinMode(3, OUTPUT);
  84.   pinMode(4, OUTPUT);
  85.   pinMode(5, OUTPUT);
  86.  
  87.   if(!card.init()) {                   // Play with 8 MHz spi (default faster!)
  88.     putstring_nl("Card init failed!"); // Something went wrong, let's print out why
  89.     sdErrorCheck();
  90.     while(1); // CHECK THIS!!!!! Might be an "l"
  91.   }
  92.  
  93.   // enable optimize read - some cards may timeout. Disable if you're having problems
  94.   card.partialBlockRead(true);
  95.  
  96.   // Now we will look for a FAT partition!
  97.   uint8_t part;
  98.   for(part = 0; part < 5; part++) {  // we have up to 5 slots to look in
  99.     if(vol.init(card, part))
  100.       break;                         // we found one, let's bail
  101.   }
  102.   if(part == 5) {                    // if we ended up not finding one :(
  103.     putstring_nl("No valid FAT partition!");
  104.     sdErrorCheck();                  // Something went wrong, let's print out why
  105.     while(1);  // CHECK THIS!!!!! Might be an "l"
  106.   }
  107.  
  108.   // Let's tell the user about what we found
  109.   putstring("Using partition ");
  110.   Serial.print(part, DEC);
  111.   putstring(", type if FAT");
  112.   Serial.println(vol.fatType(), DEC); // FAT16 or FAT32?
  113.  
  114.   // Try to open the root directory
  115.   if(!root.openRoot(vol)) {
  116.     putstring_nl("Ready!");
  117.     dirLevel = 0;
  118.   }
  119. } // end setup
  120.  
  121. void loop()
  122. {
  123.  
  124.   val = digitalRead(inputPin);
  125.   char name(15);
  126.  
  127.   if (val == LOW) {          
  128.     if (pirState == LOW) {
  129.       // We have just turned on
  130.       Serial.println("Motion!");
  131.      
  132.       // Turn eyes on
  133.       digitalWrite(eyeleds, HIGH);
  134.  
  135.       // Play a sound:
  136.       playcomplete("complete.wav");
  137.       pirState = HIGH;
  138.     }
  139.   } else {
  140.     if (pirState == HIGH) {
  141.       digitalWrite(eyeleds, LOW);
  142.       // we have just turned off
  143.       Serial.println("Motion ended!");
  144.       // We only want to print on the output change, not state
  145.       pirState = LOW;
  146.      }
  147.   }
  148. } // End loop
  149.  
  150.  
  151.  
  152. void playcomplete(char *name) {
  153.   char i;
  154.   uint8_t volume;
  155.   int v2;
  156.   int servoAngle;
  157.   int v2percent;
  158.   servoAngle = 0;
  159.  
  160.   v2percent = .06;
  161.  
  162.   playfile(name);
  163.  
  164.   while (wave.isplaying) {
  165.     volume = 0;
  166.     for (i=0; i<8; i++) {
  167.         v2 = analogRead(1);
  168.         // Serial.println(v2);
  169.        
  170.         delay(5);
  171.     }
  172.  
  173.     servoAngle = v2 * v2percent;
  174.     servo.write(servoAngle);
  175.    
  176.     /*
  177.     if (v2 > 250) {
  178.       pulseWidth = 1800;
  179.       mouthchange = 1;
  180.     } else {
  181.       pulseWidth = 800;
  182.       mouthchange = 1;
  183.     }
  184.  
  185.     digitalWrite(servoPin, HIGH);   // start the pulse
  186.     delayMicroseconds(pulseWidth);  // pulse width
  187.     digitalWrite(servoPin, LOW);    // stop the pulse
  188.     */
  189.   }
  190.  
  191.   f.close();
  192.   digitalWrite(eyeleds, LOW);
  193.   servo.write(0);
  194.  
  195.   // We have just turned off
  196. }
  197.  
  198.  
  199. void playfile(char *name) {
  200.   // stop any file already playing
  201.   if (wave.isplaying) {
  202.     wave.stop();
  203.     f.close();
  204.   }
  205.  
  206.   // Look in the root directory and open the file
  207.   if(!f.open(root, name)) {
  208.     putstring("Couldn't open file "); Serial.print(name); return;
  209.   }
  210.  
  211.   // OK, ready the file and turn it into a wave object
  212.   if(!wave.create(f)) {
  213.     putstring_nl("Not a valid WAV file."); return;
  214.   }
  215.  
  216.   // OK, time to play! Start playback
  217.   wave.play();
  218. }
Add Comment
Please, Sign In to add comment