Wednesday, April 6, 2011

Sound work continued - Motor shield integration

Be prepared... long post is loooooooooooooooooooooooooong...

Today was scary.  Really scary.  Really REALLY scary.  So scary, that had I screwed something up while doing what I was doing today... I would have had no droid for my final project.  But as Seth Rogan said in the movie Paul, "Sometimes you just gotta roll the dice."  So I rolled.  And after a couple of hours, I breathed a sigh of relief.  My motor shield still works.  My wave shield works.  And most importantly, they work TOGETHER.  Let me share with you what I did today.

First, it had been a while since I loaded up the wave shield, so I brought that out and put the new R2D2 files on it.  Works like a charm.  Next, I needed to be able to access each file directly instead of consecutively, so I grabbed some of the sample code from the library and there it was.  Piece of cake.  Then, I said to myself, "Self... let's stack these things."  So I did.  I stacked the wave shield on top of my motor shield (which is on top of my arduino), and I copied in just the part of the setup code that would read the available files on the SD card (for now). 

As expected (because I've been lucky with a bunch of things on the project so far, so I figured that I would run out about now) the files could not be read.  I was prepared for this, thanks to Mike's truth table he sent earlier this semester.  So I went to work un-soldering 4 of the 5 jumpers from pins 2-5.  While I was at it, I decided to add some headers so I could test back and forth until I got the right combination right.  The prototyping jumpers are a little less permanent, but that will be ok for the final project.  After I put the headers in, I put the jumpers in the way they were first to test out the shield (by itself again).  Success.  Now came the first hard part... figuring out how to modify which pins the wave shield is looking to use for the DAC control. 

Luckily, there wasn't much code in the WaveHC library to go through, so in the WavePinDefs.h file, I found four sets of declarations like this (this is what my code looks like now, I put the original lines in comments at the end):

     // use arduino pins 2, 3, 4, 5 for DAC                             
     // pin 2 is DAC chip select                                        
     /** Data direction register for DAC chip select. */                
     #define MCP_DAC_CS_DDR PIN19_DDRREG // originally PIN2_DDRREG      
     /** Port register for DAC chip select. */                          
     #define MCP_DAC_CS_PORT PIN19_PORTREG // originally PIN2_PORTREG   
     /** Port bit number for DAC chip select. */                        
     #define MCP_DAC_CS_BIT PIN19_BITNUM // originally PIN2_BITNUM      

So I followed the definitions of PINx_DDRREG back to ArduinoPins.h, and saw a bunch more definitions, I believe the were some 70 pin definitions for each grouping (probably for Mega support).  So I said "let's give it a whirl and reset these definitions to the analog pins (referenced as digital pins 14-19)."  So I did that and put the jumper wires in the proper places.  Success, my wave shield was now running off the digital pins 16-19, leaving pins 2-5 available for the motor shield.  So I tried the two shields together again.  Fail.  What NOW?

I went back to the truth table (thanks again Mike!) and there were still some pin interactions that I needed to resolve.  But now I had no idea how, because the motor shield PCB is made in this way... I didn't add jumper wires like the wave shield.  So I did a search for wave and motor shield and came across this forum thread (http://www.adafruit.com/forums/viewtopic.php?f=31&t=8066#p63795).  I took a double take.  Cut the what?  Traces?  Never done that before... let's google it! (http://www.diystompboxes.com/smfforum/index.php?topic=81879.0;prev_next=next)  So at this juncture, I was floored.  Take my perfectly fine working on its own motor shield, solder some new wires and TAKE AN X-ACTO KNIFE TO THE BOARD??  ARE YOU NUTS??  I looked again, and there really was no other way around it... the SD card needs pins 11, 12, and 13 (from its documentation), so if I want these two to work together, gonna have to get in there.  This is where the dice rolling came in.  Soldering mistakes I can fix, but scraping out the traces is kinda permanent.  I highly doubted that I could've ordered a new motor shield in time, but I pretty much had no other option.  I followed the instructions by poster 'jonharrell' and soldered in new wires and scraped out the traces for the other two pins.  His pictures were life-saving with this.  I am (hopefully only) sacrificing servo capabilities to have 4 DC motors and wave shield functionality.

Well, lady luck was once again with me after all and I succeeded.  Now I have control of at least the two drive motors (haven't rigged up the third yet to test) and I can trigger a sound to be played with any of the controller buttons.  ::phew::  Now the only concern I have is whether the sounds will still be audible from inside the droid's body as well as heard over the motors (even on the other side of the shield where I could specify a higher frequency, the motors were still loud), but that's completely out of my hands unless I find a much louder speaker...

But that'll have to wait until everything else is finished if I have some time left.



1 comment: