What kind of servo library is needed when using the Wave Shi

Post here about your Arduino projects, get help - for Adafruit customers!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
User avatar
isaace
 
Posts: 48
Joined: Mon Sep 08, 2014 11:18 pm

Re: What kind of servo library is needed when using the Wave

Post by isaace »

When I run my code (as it was shown in my reply from 6:04pm yesterday) only the servo moves, but it does not play wave files.

User avatar
Franklin97355
 
Posts: 23940
Joined: Mon Apr 21, 2008 2:33 pm

Re: What kind of servo library is needed when using the Wave

Post by Franklin97355 »

Your code keeps changing. Could you post your code as you are running it now?

User avatar
adafruit_support_bill
 
Posts: 88154
Joined: Sat Feb 07, 2009 10:11 am

Re: What kind of servo library is needed when using the Wave

Post by adafruit_support_bill »

When I run my code (as it was shown in my reply from 6:04pm yesterday) only the servo moves, but it does not play wave files.
As explained in my previous post. Your code never initializes the SD card, so no files are ever played.
You are referring to what is in your posted code?
No. I am referring to what is in your posted code.

User avatar
isaace
 
Posts: 48
Joined: Mon Sep 08, 2014 11:18 pm

Re: What kind of servo library is needed when using the Wave

Post by isaace »

It is still the same as yesterday at 6:04. I haven't changed it.

Code: Select all

    #include <WaveHC.h>
    #include <WaveUtil.h>
    #include <Wire.h>
    #include <Adafruit_PWMServoDriver.h>

    Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver();

    #define SERVOMIN  150 // this is the 'minimum' pulse length count (out of 4096)
    #define SERVOMAX  600 // this is the 'maximum' pulse length count (out of 4096)

    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 volumes root directory
    WaveHC wave;      // This is the only wave (audio) object, since we will only play one at a time

    int degrees;

    int ServoPulse(int degrees)
    {
        return map(degrees, 0, 180, SERVOMIN, SERVOMAX);
    }

    uint8_t servonum = 0;
    uint8_t dirLevel; // indent level for file/dir names    (for prettyprinting)
    dir_t dirBuf;     // buffer for directory reads


    /*
     * Define macro to put error messages in flash memory
     */
    #define error(msg) error_P(PSTR(msg))


    // Function definitions (we define them here, but the code is below)
    void play(FatReader &dir);

    //////////////////////////////////// SETUP
    void setup() {
     
        Serial.begin(9600);
      Serial.println("16 channel Servo test!");

      pwm.begin();
     
      pwm.setPWMFreq(60);  // Analog servos run at ~60 Hz updates
    }

    void setServoPulse(uint8_t n, double pulse) {
      double pulselength;
     
      pulselength = 1000000;   // 1,000,000 us per second
      pulselength /= 60;   // 60 Hz
      Serial.print(pulselength); Serial.println(" us per period");
      pulselength /= 4096;  // 12 bits of resolution
      Serial.print(pulselength); Serial.println(" us per bit");
      pulse *= 1000;
      pulse /= pulselength;
      Serial.println(pulse);
      pwm.setPWM(n, 0, pulse);
    //}
    //  Serial.begin(9600);           // set up Serial library at 9600 bps for debugging
     
      putstring_nl("\nWave test!");  // say we woke up!
     
      putstring("Free RAM: ");       // This can help with debugging, running out of RAM is bad
      Serial.println(FreeRam());

      //  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!) 
        error("Card init. failed!");  // Something went wrong, lets print out why
      }
     
      // 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  :(
        error("No valid FAT partition!");  // Something went wrong, lets print out why
      }
     
      // 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)) {
        error("Can't open root dir!");      // Something went wrong,
      }
     
      // Whew! We got past the tough parts.
      putstring_nl("Files found (* = fragmented):");

      // Print out all of the files in all the directories.
      root.ls(LS_R | LS_FLAG_FRAGMENTED);
    }

    //////////////////////////////////// LOOP
    void loop() {

           pwm.setPWM(servonum, 0, ServoPulse(180));
          delay(500);
          pwm.setPWM(servonum, 0, ServoPulse(0));
          delay(500);
          pwm.setPWM(servonum, 0, ServoPulse(90));
          delay(500);
          pwm.setPWM(servonum, 0, ServoPulse(120));
          delay(500);
         
      root.rewind();
      play(root);
    }

    /////////////////////////////////// HELPERS
    /*
     * print error message and halt
     */
    void error_P(const char *str) {
      PgmPrint("Error: ");
      SerialPrint_P(str);
      sdErrorCheck();
      while(1);
    }
    /*
     * print error message and halt if SD I/O error, great for debugging!
     */
    void sdErrorCheck(void) {
      if (!card.errorCode()) return;
      PgmPrint("\r\nSD I/O error: ");
      Serial.print(card.errorCode(), HEX);
      PgmPrint(", ");
      Serial.println(card.errorData(), HEX);
      while(1);
    }
    /*
     * play recursively - possible stack overflow if subdirectories too nested
     */
    void play(FatReader &dir) {
      FatReader file;
      while (dir.readDir(dirBuf) > 0) {    // Read every file in the directory one at a time
     
        // Skip it if not a subdirectory and not a .WAV file
        if (!DIR_IS_SUBDIR(dirBuf)
             && strncmp_P((char *)&dirBuf.name[8], PSTR("WAV"), 3)) {
          continue;
        }

        Serial.println();            // clear out a new line
       
        for (uint8_t i = 0; i < dirLevel; i++) {
           Serial.write(' ');       // this is for prettyprinting, put spaces in front
        }
        if (!file.open(vol, dirBuf)) {        // open the file in the directory
          error("file.open failed");          // something went wrong
        }
       
        if (file.isDir()) {                   // check if we opened a new directory
          putstring("Subdir: ");
          printEntryName(dirBuf);
          Serial.println();
          dirLevel += 2;                      // add more spaces
          // play files in subdirectory
          play(file);                         // recursive!
          dirLevel -= 2;   
        }
        else {
          // Aha! we found a file that isnt a directory
          putstring("Playing ");
          printEntryName(dirBuf);              // print it out
          if (!wave.create(file)) {            // Figure out, is it a WAV proper?
            putstring(" Not a valid WAV");     // ok skip it
          } else {
            Serial.println();                  // Hooray it IS a WAV proper!
            wave.play();                       // make some noise!
           
            uint8_t n = 0;
            while (wave.isplaying) {// playing occurs in interrupts, so we print dots in realtime
              putstring(".");
              if (!(++n % 32))Serial.println();
              delay(100);
            }       
            sdErrorCheck();                    // everything OK?
            // if (wave.errors)Serial.println(wave.errors);     // wave decoding errors
          }
        }
      }
    }

User avatar
adafruit_support_bill
 
Posts: 88154
Joined: Sat Feb 07, 2009 10:11 am

Re: What kind of servo library is needed when using the Wave

Post by adafruit_support_bill »

You need to move the SD card initialization and directory operations back into setup() as they are in the daphc example code you started with.

As you have it now, that code has been merged into your setServoPulse() function. Since you never call that function, the SD card never gets initialized and no files are available to play.

User avatar
isaace
 
Posts: 48
Joined: Mon Sep 08, 2014 11:18 pm

Re: What kind of servo library is needed when using the Wave

Post by isaace »

Ok, thanks! Now it is playing the wave files and the servo, but not simultaneously. First, the servo, then the waves, then the servo and so on.

How can I get them to play at the exact same time? I commented out the setServoPulse() function. I figured that would be the same as what you recommended.

Thanks for your help and patience.

Isaac

Code: Select all

        #include <WaveHC.h>
        #include <WaveUtil.h>
        #include <Wire.h>
        #include <Adafruit_PWMServoDriver.h>

        Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver();

        #define SERVOMIN  150 // this is the 'minimum' pulse length count (out of 4096)
        #define SERVOMAX  600 // this is the 'maximum' pulse length count (out of 4096)

        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 volumes root directory
        WaveHC wave;      // This is the only wave (audio) object, since we will only play one at a time

        int degrees;

        int ServoPulse(int degrees)
        {
            return map(degrees, 0, 180, SERVOMIN, SERVOMAX);
        }

        uint8_t servonum = 0;
        uint8_t dirLevel; // indent level for file/dir names    (for prettyprinting)
        dir_t dirBuf;     // buffer for directory reads


        /*
         * Define macro to put error messages in flash memory
         */
        #define error(msg) error_P(PSTR(msg))


        // Function definitions (we define them here, but the code is below)
        void play(FatReader &dir);

        //////////////////////////////////// SETUP
        void setup() {
         
            Serial.begin(9600);
          Serial.println("16 channel Servo test!");

          pwm.begin();
         
          pwm.setPWMFreq(60);  // Analog servos run at ~60 Hz updates
        //}

    /*    void setServoPulse(uint8_t n, double pulse) {
          double pulselength;
         
          pulselength = 1000000;   // 1,000,000 us per second
          pulselength /= 60;   // 60 Hz
          Serial.print(pulselength); Serial.println(" us per period");
          pulselength /= 4096;  // 12 bits of resolution
          Serial.print(pulselength); Serial.println(" us per bit");
          pulse *= 1000;
          pulse /= pulselength;
          Serial.println(pulse);
          pwm.setPWM(n, 0, pulse);  */
        //}
        //  Serial.begin(9600);           // set up Serial library at 9600 bps for debugging

          putstring_nl("\nWave test!");  // say we woke up!
         
          putstring("Free RAM: ");       // This can help with debugging, running out of RAM is bad
          Serial.println(FreeRam());

          //  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!)
            error("Card init. failed!");  // Something went wrong, lets print out why
          }
         
          // 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  :(
            error("No valid FAT partition!");  // Something went wrong, lets print out why
          }
         
          // 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)) {
            error("Can't open root dir!");      // Something went wrong,
          }
         
          // Whew! We got past the tough parts.
          putstring_nl("Files found (* = fragmented):");

          // Print out all of the files in all the directories.
          root.ls(LS_R | LS_FLAG_FRAGMENTED);
        }

        //////////////////////////////////// LOOP
        void loop() {

               pwm.setPWM(servonum, 0, ServoPulse(180));
              delay(500);
              pwm.setPWM(servonum, 0, ServoPulse(0));
              delay(500);
              pwm.setPWM(servonum, 0, ServoPulse(90));
              delay(500);
              pwm.setPWM(servonum, 0, ServoPulse(120));
              delay(500);
             
          root.rewind();
          play(root);
        }

        /////////////////////////////////// HELPERS
        /*
         * print error message and halt
         */
        void error_P(const char *str) {
          PgmPrint("Error: ");
          SerialPrint_P(str);
          sdErrorCheck();
          while(1);
        }
        /*
         * print error message and halt if SD I/O error, great for debugging!
         */
        void sdErrorCheck(void) {
          if (!card.errorCode()) return;
          PgmPrint("\r\nSD I/O error: ");
          Serial.print(card.errorCode(), HEX);
          PgmPrint(", ");
          Serial.println(card.errorData(), HEX);
          while(1);
        }
        /*
         * play recursively - possible stack overflow if subdirectories too nested
         */
        void play(FatReader &dir) {
          FatReader file;
          while (dir.readDir(dirBuf) > 0) {    // Read every file in the directory one at a time
         
            // Skip it if not a subdirectory and not a .WAV file
            if (!DIR_IS_SUBDIR(dirBuf)
                 && strncmp_P((char *)&dirBuf.name[8], PSTR("WAV"), 3)) {
              continue;
            }

            Serial.println();            // clear out a new line
           
            for (uint8_t i = 0; i < dirLevel; i++) {
               Serial.write(' ');       // this is for prettyprinting, put spaces in front
            }
            if (!file.open(vol, dirBuf)) {        // open the file in the directory
              error("file.open failed");          // something went wrong
            }
           
            if (file.isDir()) {                   // check if we opened a new directory
              putstring("Subdir: ");
              printEntryName(dirBuf);
              Serial.println();
              dirLevel += 2;                      // add more spaces
              // play files in subdirectory
              play(file);                         // recursive!
              dirLevel -= 2;   
            }
            else {
              // Aha! we found a file that isnt a directory
              putstring("Playing ");
              printEntryName(dirBuf);              // print it out
              if (!wave.create(file)) {            // Figure out, is it a WAV proper?
                putstring(" Not a valid WAV");     // ok skip it
              } else {
                Serial.println();                  // Hooray it IS a WAV proper!
                wave.play();                       // make some noise!
               
                uint8_t n = 0;
                while (wave.isplaying) {// playing occurs in interrupts, so we print dots in realtime
                  putstring(".");
                  if (!(++n % 32))Serial.println();
                  delay(100);
                }       
                sdErrorCheck();                    // everything OK?
                // if (wave.errors)Serial.println(wave.errors);     // wave decoding errors
              }
            }
          }
        }

User avatar
adafruit_support_bill
 
Posts: 88154
Joined: Sat Feb 07, 2009 10:11 am

Re: What kind of servo library is needed when using the Wave

Post by adafruit_support_bill »

You still have that while loop in your play function. That keeps your from returning from the play function until the file has finished playing. You need to remove that if you want to keep moving the servos.

Code: Select all

                while (wave.isplaying) {// playing occurs in interrupts, so we print dots in realtime
                  putstring(".");
                  if (!(++n % 32))Serial.println();
                  delay(100);
                }       

User avatar
isaace
 
Posts: 48
Joined: Mon Sep 08, 2014 11:18 pm

Re: What kind of servo library is needed when using the Wave

Post by isaace »

Thanks, so I tried by commenting it out. Now just the servo moves and I don't hear the wave files. According to the serial, it looks like it thinks its playing the waves.

Code: Select all

16 channel Servo test!
Estimated pre-scale: 112.03
Final pre-scale: 112

Wave test!
Free RAM: 381
Using partition 1, type is FAT16
Files found (* = fragmented):
BATHROOM.WAV  
NAME.WAV  
BYE.WAV  
UM.WAV  
FROM.WAV  
HELLO.WAV  
HI.WAV  
AREYOU.WAV  

Playing BATHROOM.WAV

Playing NAME.WAV

Playing BYE.WAV

Playing UM.WAV

Playing FROM.WAV

Playing HELLO.WAV Not a valid WAV
Playing HI.WAV Not a valid WAV
Playing AREYOU.WAV Not a valid WAV
Playing BATHROOM.WAV

Playing NAME.WAV

Playing BYE.WAV

Playing UM.WAV

Playing FROM.WAV

Playing HELLO.WAV Not a valid WAV
Playing HI.WAV Not a valid WAV
Playing AREYOU.WAV Not a valid WAV
Playing BATHROOM.WAV

Playing NAME.WAV

Playing BYE.WAV

Playing UM.WAV

Playing FROM.WAV

Playing HELLO.WAV Not a valid WAV
Playing HI.WAV Not a valid WAV
Playing AREYOU.WAV Not a valid WAV
Playing BATHROOM.WAV

Playing NAME.WAV

Playing BYE.WAV

Playing UM.WAV

Playing FROM.WAV

Playing HELLO.WAV Not a valid WAV
Playing HI.WAV Not a valid WAV
Playing AREYOU.WAV Not a valid WAV

Code: Select all

            #include <WaveHC.h>
            #include <WaveUtil.h>
            #include <Wire.h>
            #include <Adafruit_PWMServoDriver.h>

            Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver();

            #define SERVOMIN  150 // this is the 'minimum' pulse length count (out of 4096)
            #define SERVOMAX  600 // this is the 'maximum' pulse length count (out of 4096)

            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 volumes root directory
            WaveHC wave;      // This is the only wave (audio) object, since we will only play one at a time

            int degrees;

            int ServoPulse(int degrees)
            {
                return map(degrees, 0, 180, SERVOMIN, SERVOMAX);
            }

            uint8_t servonum = 0;
            uint8_t dirLevel; // indent level for file/dir names    (for prettyprinting)
            dir_t dirBuf;     // buffer for directory reads


            /*
             * Define macro to put error messages in flash memory
             */
            #define error(msg) error_P(PSTR(msg))


            // Function definitions (we define them here, but the code is below)
            void play(FatReader &dir);

            //////////////////////////////////// SETUP
            void setup() {
             
                Serial.begin(9600);
              Serial.println("16 channel Servo test!");

              pwm.begin();
             
              pwm.setPWMFreq(60);  // Analog servos run at ~60 Hz updates
            //}

        /*    void setServoPulse(uint8_t n, double pulse) {
              double pulselength;
             
              pulselength = 1000000;   // 1,000,000 us per second
              pulselength /= 60;   // 60 Hz
              Serial.print(pulselength); Serial.println(" us per period");
              pulselength /= 4096;  // 12 bits of resolution
              Serial.print(pulselength); Serial.println(" us per bit");
              pulse *= 1000;
              pulse /= pulselength;
              Serial.println(pulse);
              pwm.setPWM(n, 0, pulse);  */
            //}
            //  Serial.begin(9600);           // set up Serial library at 9600 bps for debugging

              putstring_nl("\nWave test!");  // say we woke up!
             
              putstring("Free RAM: ");       // This can help with debugging, running out of RAM is bad
              Serial.println(FreeRam());

              //  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!)
                error("Card init. failed!");  // Something went wrong, lets print out why
              }
             
              // 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  :(
                error("No valid FAT partition!");  // Something went wrong, lets print out why
              }
             
              // 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)) {
                error("Can't open root dir!");      // Something went wrong,
              }
             
              // Whew! We got past the tough parts.
              putstring_nl("Files found (* = fragmented):");

              // Print out all of the files in all the directories.
              root.ls(LS_R | LS_FLAG_FRAGMENTED);
            }

            //////////////////////////////////// LOOP
            void loop() {

                   pwm.setPWM(servonum, 0, ServoPulse(180));
                  delay(500);
                  pwm.setPWM(servonum, 0, ServoPulse(0));
                  delay(500);
                  pwm.setPWM(servonum, 0, ServoPulse(90));
                  delay(500);
                  pwm.setPWM(servonum, 0, ServoPulse(120));
                  delay(500);
                 
              root.rewind();
              play(root);
            }

            /////////////////////////////////// HELPERS
            /*
             * print error message and halt
             */
            void error_P(const char *str) {
              PgmPrint("Error: ");
              SerialPrint_P(str);
              sdErrorCheck();
              while(1);
            }
            /*
             * print error message and halt if SD I/O error, great for debugging!
             */
            void sdErrorCheck(void) {
              if (!card.errorCode()) return;
              PgmPrint("\r\nSD I/O error: ");
              Serial.print(card.errorCode(), HEX);
              PgmPrint(", ");
              Serial.println(card.errorData(), HEX);
              while(1);
            }
            /*
             * play recursively - possible stack overflow if subdirectories too nested
             */
            void play(FatReader &dir) {
              FatReader file;
              while (dir.readDir(dirBuf) > 0) {    // Read every file in the directory one at a time
             
                // Skip it if not a subdirectory and not a .WAV file
                if (!DIR_IS_SUBDIR(dirBuf)
                     && strncmp_P((char *)&dirBuf.name[8], PSTR("WAV"), 3)) {
                  continue;
                }

                Serial.println();            // clear out a new line
               
                for (uint8_t i = 0; i < dirLevel; i++) {
                   Serial.write(' ');       // this is for prettyprinting, put spaces in front
                }
                if (!file.open(vol, dirBuf)) {        // open the file in the directory
                  error("file.open failed");          // something went wrong
                }
               
                if (file.isDir()) {                   // check if we opened a new directory
                  putstring("Subdir: ");
                  printEntryName(dirBuf);
                  Serial.println();
                  dirLevel += 2;                      // add more spaces
                  // play files in subdirectory
                  play(file);                         // recursive!
                  dirLevel -= 2;   
                }
                else {
                  // Aha! we found a file that isnt a directory
                  putstring("Playing ");
                  printEntryName(dirBuf);              // print it out
                  if (!wave.create(file)) {            // Figure out, is it a WAV proper?
                    putstring(" Not a valid WAV");     // ok skip it
                  } else {
                    Serial.println();                  // Hooray it IS a WAV proper!
                    wave.play();                       // make some noise!
                   
                    uint8_t n = 0;
             /*       while (wave.isplaying) {// playing occurs in interrupts, so we print dots in realtime
                      putstring(".");
                      if (!(++n % 32))Serial.println();
                      delay(100);  */
                    }       
                    sdErrorCheck();                    // everything OK?
                    // if (wave.errors)Serial.println(wave.errors);     // wave decoding errors
                  }
                }
              }

User avatar
adafruit_support_bill
 
Posts: 88154
Joined: Sat Feb 07, 2009 10:11 am

Re: What kind of servo library is needed when using the Wave

Post by adafruit_support_bill »

Your function is trying to play the entire directory. It skip through the whole list. And since the last file is not a valid wav file it ends up not laying anything.

You need to decide which files you want to play and pay those. The playfile function from Play6 is a better example to start from.

https://learn.adafruit.com/adafruit-wav ... o/play6-hc
void playfile(char *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();
}

User avatar
isaace
 
Posts: 48
Joined: Mon Sep 08, 2014 11:18 pm

Re: What kind of servo library is needed when using the Wave

Post by isaace »

Ok, I am back. I admit. I have changed the code significantly. I am still using the PCA and wave shield, with BitVoicer. When I say a specific command, I want the servo and wave to play simultaneously. It works, but the waves don't play regardless of which command I give. Note that the commands are hello, name, goodbye, etc.

As soon as I comment out this line, it works, but it gives me the same result for every command: //if (bvSerial.strData == "hello"

If I don't comment it out, it works like it is supposed to, but I don't get the servo. By the way, as you can see I added some h-bridge code in there to drive a PMDC. It seems to react just like the servo, but other than that, it doesn't interfere with what is going on.

Code: Select all

#include <FatReader.h>
#include <SdReader.h>
#include <avr/pgmspace.h>
#include "WaveUtil.h"
#include "WaveHC.h"
#include <BitVoicer11.h>
#include <Wire.h>
#include <Adafruit_PWMServoDriver.h>

Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver();

#define SERVOMIN  150 // this is the 'minimum' pulse length count (out of 4096)
#define SERVOMAX  600 // this is the 'maximum' pulse length count (out of 4096)

int led = 6;
BitVoicerSerial bvSerial = BitVoicerSerial(); //Stores true if the Audio Streaming Calibration tool is running
boolean sampleTest = false;  //Stores the data type retrieved by getData()
byte dataType = 0; //Sets up the pins and default variables

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

int mot1ana=6;
int mot1a=1;
int mot1b=8;

int degrees;
int ServoPulse(int degrees)
           {
                return map(degrees, 0, 180, SERVOMIN, SERVOMAX);
            }
            uint8_t servonum = 0;
            uint8_t dirLevel; // indent level for file/dir names    (for prettyprinting)
            dir_t dirBuf;     // buffer for directory reads
            #define error(msg) error_P(PSTR(msg))



#define DEBOUNCE 5  // button debouncer

// here is where we define the buttons that we'll use. button "1" is the first, button "6" is the 6th, etc
byte buttons[] = {14, 15, 16, 17, 18, 19};
// This handy macro lets us determine how big the array up above is, by checking the size
#define NUMBUTTONS sizeof(buttons)
// we will track if a button is just pressed, just released, or 'pressed' (the current state
volatile byte pressed[NUMBUTTONS], justpressed[NUMBUTTONS], justreleased[NUMBUTTONS];
void play(FatReader &dir);
// 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);
}
void setup() {
pinMode(mot1ana,OUTPUT);
pinMode(mot1a,OUTPUT);
pinMode(mot1b,OUTPUT);
  
  
pwm.begin();
             
pwm.setPWMFreq(60);  // Analog servos run at ~60 Hz updates
  pinMode(led, OUTPUT);     

  
  bvSerial.setAudioInput(1);  
  Serial.begin(115200);  //Sets up the pinModes
  byte i;
  putstring_nl("WaveHC with ");
  Serial.print(NUMBUTTONS, DEC);
  putstring_nl("buttons");
  
  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!
  
  for (i=0; i< NUMBUTTONS; i++) {
    pinMode(buttons[i], INPUT);
    digitalWrite(buttons[i], HIGH);
  }
  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!
  }
  card.partialBlockRead(true);
  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!
  }
  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!");
  
  TCCR2A = 0;
  TCCR2B = 1<<CS22 | 1<<CS21 | 1<<CS20;

  //Timer2 Overflow Interrupt Enable
  TIMSK2 |= 1<<TOIE2;
}

SIGNAL(TIMER2_OVF_vect) {
  check_switches();
}

void check_switches()
{
  static byte previousstate[NUMBUTTONS];
  static byte currentstate[NUMBUTTONS];
  byte index;

  for (index = 0; index < NUMBUTTONS; index++) {
    currentstate[index] = digitalRead(buttons[index]);   // read the button
    
    if (currentstate[index] == previousstate[index]) {
      if ((pressed[index] == LOW) && (currentstate[index] == LOW)) {
          // just pressed
          justpressed[index] = 1;
      }
      else if ((pressed[index] == HIGH) && (currentstate[index] == HIGH)) {
          // just released
          justreleased[index] = 1;
      }
      pressed[index] = !currentstate[index];  // remember, digital HIGH means NOT pressed
    }
    //Serial.println(pressed[index], DEC);
    previousstate[index] = currentstate[index];   // keep a running tally of the buttons

  }
}
void loop() {

  if (sampleTest == true)
  {
    bvSerial.BANNED(46);
  }
  if (bvSerial.engineRunning)
  {
    bvSerial.BANNED(46);
  }
}
void serialEvent()
{
  dataType = bvSerial.getData(); 
  if (dataType == BV_COMMAND)
      sampleTest = bvSerial.cmdData;
  if (dataType == BV_STATUS && bvSerial.engineRunning == true)
    bvSerial.startStopListening();
  

  if (dataType == BV_STR)

  //if (bvSerial.strData == "hello")
  playcomplete("hello.WAV");
  
                  pwm.setPWM(servonum, 0, ServoPulse(180));
                  delay(500);
                  pwm.setPWM(servonum, 0, ServoPulse(0));
                  delay(500);
                  pwm.setPWM(servonum, 0, ServoPulse(90));
                  delay(500);
                  pwm.setPWM(servonum, 0, ServoPulse(120));
                  delay(500);
                  
                    analogWrite(mot1ana,500);  //Forward left
  digitalWrite(mot1a,HIGH);
  digitalWrite(mot1b,LOW);
  delay(2000);
    digitalWrite(mot1a,LOW);
  digitalWrite(mot1b,LOW);
  delay(200);
  analogWrite(mot1ana,500);
  digitalWrite(mot1a,LOW);
  digitalWrite(mot1b,HIGH);
  delay(2000);
    digitalWrite(mot1a,LOW);
  digitalWrite(mot1b,LOW);
  delay(200);  
  
//  if (bvSerial.strData == "hi")  
//  playcomplete("hello.wav");
  if (bvSerial.strData == "name")
  playcomplete("name.wav");
  if (bvSerial.strData == "bathroom")
  playcomplete("bathroom.WAV");
    if (bvSerial.strData == "from")
  playcomplete("from.WAV");
    if (bvSerial.strData == "bye")
  playcomplete("bye.WAV");
  if (bvSerial.strData == "creator")
  playcomplete("creator.WAV");
    if (bvSerial.strData == "um")
  playcomplete("um.WAV");
  
  byte i;

}

void playcomplete(char *name) {
  playfile(name);
  
  // now its done playing
}

void playfile(char *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();
}

User avatar
Franklin97355
 
Posts: 23940
Joined: Mon Apr 21, 2008 2:33 pm

Re: What kind of servo library is needed when using the Wave

Post by Franklin97355 »

it works, but it gives me the same result for every command: //if (bvSerial.strData == "hello"
What result is that?

User avatar
isaace
 
Posts: 48
Joined: Mon Sep 08, 2014 11:18 pm

Re: What kind of servo library is needed when using the Wave

Post by isaace »

If I comment out the said line (189) it does the same thing, regardless of what word I say. I could say 'hello,' 'bye' or 'from' and it plays the same "hello" wave file and simultaneously moves the servos and DC motors accordingly. I just want different commands to control a different arrangement of outputs, especially a different wave file.

If I don't comment out 'if (bvSerial.strData == "hello")' then it doesn't play any wave file. Only the servos and the DC motor actuate as programmed.

I have several command words loaded into the Bitvoicer GUI. You can see them in lines 190 and 218 through 228.

Isaac

User avatar
Franklin97355
 
Posts: 23940
Joined: Mon Apr 21, 2008 2:33 pm

Re: What kind of servo library is needed when using the Wave

Post by Franklin97355 »

If you replace the "hello" in the "if" line with one of the other phrases does it work? It looks like your if statements are not working.

User avatar
isaace
 
Posts: 48
Joined: Mon Sep 08, 2014 11:18 pm

Re: What kind of servo library is needed when using the Wave

Post by isaace »

If I replace "hello" in line 190 [playcomplete("hello.WAV")] with 'name,' then it plays the "name.wav" file regardless of what I say (as long as I use one of my commands).

If I change the said IF statement (on line 189) from "hello" to 'name,' then I still don't get any wave files to play (unless I comment out the if statement).

User avatar
Franklin97355
 
Posts: 23940
Joined: Mon Apr 21, 2008 2:33 pm

Re: What kind of servo library is needed when using the Wave

Post by Franklin97355 »

Then you have a problem with the bitvoice library and you should ask these questions on their site. There seems to be nothing wrong with the wave shield or your microcontroller.

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino”