Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- This is the sketch that is causing issues when I use the WAV shield:
- [code]
- #include <Wire.h>
- //WAV shield vars
- #include <FatReader.h>
- #include <SdReader.h>
- #include <avr/pgmspace.h>
- #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
- //WAV shield vars
- //motion sensor vars
- int timer = 500;
- int alarmPin = 0;
- int alarmValue = 0;
- //motion sensor vars
- #include <ServoTimer2.h> // the servo library
- //servo vars
- int activeServo= 0;
- ServoTimer2 myservo1;
- ServoTimer2 myservo2;
- int servoPin1 = 6;//9;
- int servoPin2 = 7;//10;
- #define degreesToUS( _degrees) (_degrees * 6 + 900) // macro to convert degrees to microseconds
- int pos = 50; // variable to store the servo position
- //servo vars
- boolean motionIsOn = false;
- boolean speakJetIsOn = false;
- void setup()
- {
- Wire.begin(4); // join i2c bus with address #4
- Wire.onReceive(receiveEvent); // register event
- Serial.begin(9600); // start serial for output
- Serial.println("slave setup");
- initWAVShield();
- initMotionSensor();
- initServo();
- }
- // 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);
- }
- //TODO: Initialize WAV shield when Twitter searches yeild 0 results
- void initWAVShield()
- {
- Serial.println("initWAVShield");
- 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(6, OUTPUT);
- // pinMode(7, OUTPUT);
- // pinMode(8, OUTPUT);
- // pinMode(5, OUTPUT);
- pinMode(2, OUTPUT);
- pinMode(3, OUTPUT);
- pinMode(4, OUTPUT);
- pinMode(5, 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 initMotionSensor()
- {
- Serial.println("initMotionSensor");
- pinMode(alarmPin, INPUT);
- }
- void initServo()
- {
- Serial.println("initServo");
- pinMode(servoPin1, OUTPUT);
- pinMode(servoPin2, OUTPUT);
- // set up servo pin
- myservo1.attach(servoPin1);
- myservo2.attach(servoPin2);
- stepper1();
- delay(5000);
- stepper2();
- delay(5000);
- stepper1();
- stepper2();
- }
- void loop()
- {
- if(motionIsOn==true){
- alarmValue = analogRead(alarmPin);
- //Serial.println(alarmValue);
- if(alarmValue < 100) {
- Serial.println("motion detected");
- playRandomSound();
- }
- delay(timer);
- }
- if(speakJetIsOn==true){
- animateMouth();
- }
- }
- void playRandomSound(){
- int i;
- i = random(3);
- Serial.println (i);
- switch (i) {
- case 1:
- playcomplete("test0.wav");
- break;
- case 2:
- playcomplete("test1.wav");
- break;
- case 3:
- playcomplete("test2.wav");
- break;
- }
- }
- // 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
- //animateMouth();
- animateMouthWAV();
- }
- // now its done playing
- }
- void animateMouthWAV(){
- Serial.println("animateMouthWAV");
- char i;
- uint8_t volume;
- int v2;
- volume = 0;
- for (i=0; i<8; i++) {
- v2 = analogRead(1) - 512;
- if (v2 < 0)
- v2 *= -1;
- if (v2 > volume)
- volume = v2;
- delay(5);
- }
- if (volume > 200) {
- for(pos = 1400; pos < 1700; pos += 15) // goes from 0 degrees to 180 degrees
- { // in steps of 1 degree
- myservo1.write(pos); // tell servo to go to position in variable 'pos'
- delay(15); // waits 15ms for the servo to reach the position
- }
- } else {
- for(pos = 1700; pos>=1400; pos -=15) // goes from 180 degrees to 0 degrees
- {
- myservo1.write(pos); // tell servo to go to position in variable 'pos'
- delay(15); // waits 15ms for the servo to reach the position
- }
- }
- // Serial.println("volume");
- // Serial.println(volume);
- }
- void animateMouth(){
- if(activeServo==0){
- stepper1();
- } else if(activeServo==1){
- stepper2();
- }
- }
- int stepper1(){
- Serial.println("stepper1");
- for(pos = 1400; pos < 1700; pos += 15) // goes from 0 degrees to 180 degrees
- { // in steps of 1 degree
- myservo1.write(pos); // tell servo to go to position in variable 'pos'
- delay(15); // waits 15ms for the servo to reach the position
- }
- for(pos = 1700; pos>=1400; pos -=15) // goes from 180 degrees to 0 degrees
- {
- myservo1.write(pos); // tell servo to go to position in variable 'pos'
- delay(15); // waits 15ms for the servo to reach the position
- }
- }
- int stepper2(){
- Serial.println("stepper2");
- for(pos = 1400; pos < 1700; pos += 15) // goes from 0 degrees to 180 degrees
- { // in steps of 1 degree
- myservo2.write(pos); // tell servo to go to position in variable 'pos'
- delay(15); // waits 15ms for the servo to reach the position
- }
- for(pos = 1700; pos>=1400; pos -=15) // goes from 180 degrees to 0 degrees
- {
- myservo2.write(pos); // tell servo to go to position in variable 'pos'
- delay(15); // waits 15ms for the servo to reach the position
- }
- }
- void playfile(char *name) {
- Serial.println(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();
- }
- // function that executes whenever data is received from master
- // this function is registered as an event, see setup()
- void receiveEvent(int howMany)
- {
- while(1 < Wire.available()) // loop through all but the last
- {
- char c = Wire.read(); // receive byte as a character
- //Serial.print(c); // print the character
- }
- int x = Wire.read(); // receive byte as an integer
- //Serial.println(x); // print the integer
- if(x==0){
- motionIsOn=false;
- speakJetIsOn=false;
- } else if (x==1) {
- motionIsOn=true;
- } else if (x==2 && speakJetIsOn == false) {
- activeServo = random(2);
- Serial.println("activeServo");
- Serial.println(activeServo);
- speakJetIsOn=true;
- }
- //Serial.println(x);
- }[/code]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement