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 »

The transistor on the PCA board. There is only one. I can't read the part number off of it.

User avatar
Franklin97355
 
Posts: 23910
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 disconnect or move the servo to a different position does the problem persist?

User avatar
adafruit_support_bill
 
Posts: 88088
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 »

The transistor on the PCA board. There is only one.
The only 3-pin device on the board is the reverse-polarity protection device. Are you sure you have the servo power polarity correct?

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 »

Oh, I thought it was a transistor. I tried it with and without a servo. I also disconnected the PCA from the Arduino. It still gets hot - as long as the PCA is connected to power. Then I noticed that my 5 volt power supply was AC! I soldered in a bridge rectifier and now it is on 5 volts DC.

It still gets hot - even when I unplug everything else. Since the terminal block is reverse polarity protected, it should still be fine, right?

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

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

Post by Franklin97355 »

Since the terminal block is reverse polarity protected, it should still be fine, right?
Possibly, you should get a proper dc power supply and try it. You may have damaged it by running it for so long or the power you are feeding it is too rough. What did you do besides add the rectifier to the circuit for filtering the output?

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 »

I am using a wall wart 4.5 volt power supply. I assumed it was DC, but when I checked it with a meter, I saw it was AC. I put a 2KBP04M rectifier between the voltage output of the wall wart and the PCA. I double checked it with a meter and I am getting a solid 4.12 VDC after I rectified it (it is normal to lose a little voltage when rectifying the current), but the protector is still getting hot. I could try something higher - around 6VDC?

I could use 4 AA batteries, but the only better harness I had lying around has corroded contacts. I'll give that a shot (clean it up) and let you know if it works.

Isaac

User avatar
adafruit_support_bill
 
Posts: 88088
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 »

I am getting a solid 4.12 VDC after I rectified it
Unless you have filter capacitors on there, what you are getting is 4.12v RMS. That is not the same as 4.12v DC. The peaks are considerably higher. You can't run a servo on rectified AC.

Image

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 »

I am so embarrassed. I thought I could get away with just the rectifier.

It is working AND I can use it with my Wave Shield! I have it hooked up to 4AA's. Now I can build my Cylon army and conquer the earth.

The problem is always something simple and stupid. Thanks for the help!

Isaac

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 now have the PCA working with the Wave shield, but I still can't use both simultaneously. I combined my previous code with daphc sample sketch. I can only either play waves or move servos, but not both, simultaneously. When I use this code (below), it only moves my servo and that is it, but I don't hear any sound files play.

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: 88088
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 are sitting in a while loop while the sound file is playing. No need to do that. It will play in the backgroudn via interrupts. Just return from the play() function and go back to moving 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 »

I wasn't hearing the wav files with my code. It was only moving the servo.

Thanks
Isaac

User avatar
adafruit_support_bill
 
Posts: 88088
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 »

Post the output from the serial monitor.

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 »

This is all it says on the serial.

Code: Select all

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

User avatar
adafruit_support_bill
 
Posts: 88088
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 have tacked the SD card initialization and directory operations onto the end of your setServoPulse() function - which never gets called. The SD card initialization and directory operations should be part of your setup() code.

Code: Select all

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);
}


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 »

You are referring to what is in your posted code? I did put all of that after void setup. I don't think I understand. Can you dumb it down for me?

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

Return to “Arduino”