Wednesday, February 12, 2020

Animation and Sound With an Arduino



I plan to have several areas around the layout that have sound and animation.  There will be a 20 stamp ore mill with a detailed interior.  While I don't have plans to animate it I do have a sound track of the mill in operation.  In town there will be a saloon with a dedicated sound track and at the roundhouse there will also be sounds of work being done.  The main focus of animation with sound will be at the engine service facility.  I've been using this as a learning module to develop scratch building  modeling techniques, learn about Arduino programming and create sound files from scratch.

The engine service facility consists of a water tank, coaling tower, sand house and ash pit.  I'm using an Arduino Mega 2560 with a motor control board and a WAV Player from Sparkfun for sound and one single sketch to control everything in seven different scenes.  Both sound and animation are initiated by a pushbutton mounted on the fascia of the layout.  I'll use the animation for the water tank as an example of what was involved.

The water tank scene was first designed by making a timeline of what I wanted to happen.

T0 - Depress fascia mounted button - initiates sequence

T0 to T+4 - Open tenders water hatch (sound only) - On the WAV Player track #1 is initiated and enters a 4 second sound of the water hatch being opened on the tender.

T+4 to T+14 - Lower Spout (sound and animation) - trigger is sent to the motor control board to begin spout being lowered.  Sound track enters 10 seconds of sound of cables running through pulleys while spout is lowered.

T+14 to T+126 - Pause - sound track enters 5 seconds of silence.  Motor drive stops spout motion and begins 112 second delay while sound track plays water flowing through spout into tender’s water box.

T+126 to T+136 - Raise Spout (sound and animation) - Sound track 1 enters sound of spout being raised.  MS sequence completes 112 second count and triggers spout being raised (10 second animation).

T+136 to T+144 - Pause - Sound track 1 enters 3 second silence then enters 5 second sound of tender's water hatch being closed.  Motor drive completes sequence.  End of Wave Player sound track 1.  End of Water Tank scene.

The sketch I built combines 7 different operations: sand house, ash pit, water tank, coaling tower spout, coaling tower bucket and coaling tower pit.  Separate buttons mounted on the fascia panel initiate different parts of the sketch.  In the sketch I used the “delay” command because it works with milliseconds and this allows tiny corrections with the Audacity program for perfect coordination with the animations.  The portion of the sketch for the water tank follows the timeline and goes like this:

void waterSpout() {

  // play sounds of water filling operation
  // run motor to animate water tank spout

  musicPlayer.startPlayingFile("Track006.wav"); // play sounds for water tank

  delay(4000);  // allow 4 seconds before motor operation

  waterSpoutMotor ->run(FORWARD);  // start motor forward

  delay(10000);  // allow 10 seconds to lower spout

  waterSpoutMotor ->run(RELEASE);  // stop motor

  delay(112000);  // allow 112 seconds for sound of water flowing

  waterSpoutMotor ->run(BACKWARD);  // start motor backward

  delay(10000);  // allow 10 seconds while spout raises to stored position

  waterSpoutMotor ->run(RELEASE);  // stop motor

  delay(8000);  // allow 8 seconds for sound track to complete

  musicPlayer.stopPlaying();  // stop playing sound file since animation is complete


  }


Sounds were developed using an ap called Streaming Audio Recorder that captures sounds from from the internet through your computer.  Using it you can make course changes to start and stop of the file and choose to make it a WAV file (my preferred ).  Once you have a usable sound you can modify it using Audacity, an ap used for developing sound files.  For the sound of water I combined 7 different WAV files to get the sound I wanted.   Then I took that small file and copied it and pasted end to end to make a seamless sound of water falling into the tender's tank for 112 seconds with it tapering in at the begining and out at the end.  The sounds of individual drops and echoes inside a metal chamber played an important part in all of this.

Each section of the sketch has it's own sound track and for me, making the sound tracks was the most fun.  There are a ton of sound files on the internet but you have to careful about sound quality and maybe most importantly, about background sounds.  You can use Audacity to clean up these files and even eliminate those background sounds but it's always easier to start with a clean sound file.

Audacity (https://www.audacityteam.org/) is an excellent program for editing sound tracks and it’s free.  It allows zooming into a graphic display of your track so you can work in milliseconds to clean up and modify files.  You can eliminate individual sound spikes that create “ticks” or other annoying anomalies.  You can stitch together multiple files to get the length of time you need and blend multiple files using separate tracks to layer sounds together.  It’s a very cool program and doesn’t take long to become familiar with.  Take the time to read the manual first.

Adding sound and animation to the layout adds a whole new level of “realism” and can add to your modeling skills too.  You can keep it simple and buy ready-made packages or take it to a whole new level and build your own.  Sound and animation made from scratch have the advantage of being higher quality (usually, you have total control) and exactly what you need for your specific scene.  I found the learning curve for building sound files to be fairly easy.

Learning the programming (modified C++) for the Arduino was more difficult for me.  It’s well worth investing the time though.  Arduino hardware is inexpensive and the software is encouraged to be “freeware”.  There are numerous forums available for learning and people seem anxious to help.  As an example, I wanted to write a sketch (program) to run my turntable.  I knew I wanted to use a keypad, LCD display, Arduino Mega and a Sparkfun stepper motor controller called Easydriver.  I had no idea how to make it all work.  I did find a sketch using these components that was written to control a feed table for a cutoff saw.  I copied that thinking I could make it work but had no idea what to do.  I found a forum for Arduino with model railroading and explained what I wanted to do.  A group of members participated but two very knowledgeable folks from opposite ends of the planet spent two months helping me write the sketch and explained in detail all their recommendations.  By the time the sketch was complete I had over 500 lines of code and what is probably the most complete turntable sketch in existence.  Plus I understood every bit of it.

For me, Arduino controlled animation and sound make the hobby way more fun and take it to a level never before possible and all at a reasonable cost.  The biggest expense is my time.  Just like developing every other skill needed for this hobby.







No comments:

Post a Comment

Painting Plaster Castings

 Today I painted the remaining walls of the roundhouse and thought I'd add a blurb here to share my technique for painting plaster stone...