Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Servo.h>
- #include "WaveHC.h"
- #include "WaveUtil.h"
- // DEFINES
- #define DEBOUNCE 100
- #define eyeleds 18
- 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 playing
- uint8_t dirLevel; // indent level for file/dir names (for prettyprinting)
- dir_t dirBuf; // buffer for directory reads
- WaveHC wave; // This is the only wave (audio) object, since we will only play one at a time
- Servo servo;
- int inputPin = 8; // choose the input pin (for PIR sensor)
- int servoPin = 16; // choose the input pin (for Servo)
- int pirState = LOW; // we start, assuming no motion detected
- int val = HIGH; // variable for reading the pin status for motion sensor
- int minPulse = 600; // minimum servo position
- int maxPulse = 2200; // maximum servo position
- int turnRate = 1800; // servo turn rate increment (larger value, faster rate)
- int refreshTime = 20; // time (ms) between pulses (50Hz)
- int mouthchange = 6; //checks to see if mouth position needs to be changed
- int randNumber; //random number variable to allow the choosing of which wav will be played
- /** The Arduino will calculate these values for you **/
- int centerServo; // center servo position
- int pulseWidth; // servo pulse width
- long lastPulse = 0; // recorded time (ms) of the last pulse
- 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;
- }
- // Error checking the SD Card
- 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);
- putstring_nl("Talking Skull");
- 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!
- pinMode(inputPin, INPUT); // declare sensor as input for PIR
- pinMode(eyeleds, OUTPUT); // declare sensor as output for eyes
- // set up servo pin
- servo.attach(9);
- servo.write(0);
- pinMode(servoPin, OUTPUT); // Set servo pin 18 (analog 4) as an output pin
- centerServo = maxPulse - ((maxPulse - minPulse)/2);
- pulseWidth = centerServo; // Give the servo a starting point (or it floats)
- // set up waveshield pins
- pinMode(2, OUTPUT);
- pinMode(3, OUTPUT);
- pinMode(4, OUTPUT);
- pinMode(5, OUTPUT);
- if(!card.init()) { // Play with 8 MHz spi (default faster!)
- putstring_nl("Card init failed!"); // Something went wrong, let's print out why
- sdErrorCheck();
- while(1); // CHECK THIS!!!!! Might be an "l"
- }
- // 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, let's bail
- }
- if(part == 5) { // if we ended up not finding one :(
- putstring_nl("No valid FAT partition!");
- sdErrorCheck(); // Something went wrong, let's print out why
- while(1); // CHECK THIS!!!!! Might be an "l"
- }
- // Let's tell the user about what we found
- putstring("Using partition ");
- Serial.print(part, DEC);
- putstring(", type if FAT");
- Serial.println(vol.fatType(), DEC); // FAT16 or FAT32?
- // Try to open the root directory
- if(!root.openRoot(vol)) {
- putstring_nl("Ready!");
- dirLevel = 0;
- }
- } // end setup
- void loop()
- {
- val = digitalRead(inputPin);
- char name(15);
- if (val == LOW) {
- if (pirState == LOW) {
- // We have just turned on
- Serial.println("Motion!");
- // Turn eyes on
- digitalWrite(eyeleds, HIGH);
- // Play a sound:
- playcomplete("complete.wav");
- pirState = HIGH;
- }
- } else {
- if (pirState == HIGH) {
- digitalWrite(eyeleds, LOW);
- // we have just turned off
- Serial.println("Motion ended!");
- // We only want to print on the output change, not state
- pirState = LOW;
- }
- }
- } // End loop
- void playcomplete(char *name) {
- char i;
- uint8_t volume;
- int v2;
- int servoAngle;
- int v2percent;
- servoAngle = 0;
- v2percent = .06;
- playfile(name);
- while (wave.isplaying) {
- volume = 0;
- for (i=0; i<8; i++) {
- v2 = analogRead(1);
- // Serial.println(v2);
- delay(5);
- }
- servoAngle = v2 * v2percent;
- servo.write(servoAngle);
- /*
- if (v2 > 250) {
- pulseWidth = 1800;
- mouthchange = 1;
- } else {
- pulseWidth = 800;
- mouthchange = 1;
- }
- digitalWrite(servoPin, HIGH); // start the pulse
- delayMicroseconds(pulseWidth); // pulse width
- digitalWrite(servoPin, LOW); // stop the pulse
- */
- }
- f.close();
- digitalWrite(eyeleds, LOW);
- servo.write(0);
- // We have just turned off
- }
- void playfile(char *name) {
- // stop any file already playing
- if (wave.isplaying) {
- wave.stop();
- f.close();
- }
- // Look in the root directory and open the file
- if(!f.open(root, name)) {
- putstring("Couldn't open file "); Serial.print(name); return;
- }
- // OK, ready the file and turn it into a wave object
- if(!wave.create(f)) {
- putstring_nl("Not a valid WAV file."); return;
- }
- // OK, time to play! Start playback
- wave.play();
- }
Add Comment
Please, Sign In to add comment