Scale model animation: Adding life to a layout Table 1: Arduino program for building lighting animation
T
his is an example of how you can program an Arduino to control the lighting in a building. It assumes that each Arduino pin is attached to negative side of a LED through a resistor (like 2000 Ohms ¼ Watt), so setting the pin LOW will turn the LED on. Use a USB cable to con- nect the Arduino board to your computer. You will need to type in the text that appears in red. This is the actual program (called an Ar-
duino sketch). The black text next to it is a running explanation of what we are doing and must not be entered. It is only there to help modelers new to Arduino understand the setup process. We’ve broken it up into four sections in a effort to make it easier to understand.
Definition: #define numleds 16 (This line sets the number of LEDs this program will control.) byte ledpins [ ] = { 0,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17 } ; (This line creates a list of each Arduino pin we will use with a LED.) The setup: void setup( ) {
(This is a program setup section and it runs once after power-up.)
randomSeed ( analogRead ( 2 ) ) ; (This sets up a mathematical random number generator.) The following loop sets up the LED pins as outputs. for ( int i=1; i <= numleds; i++ ) {
(This starts a loop at 1 and runs to 16 [the value we set for numleds before]).
pinMode ( ledpins [ i ], OUTPUT) ; (This just sets each pin I as a driver [OUTPUT]). digitalWrite ( ledpins [ i ] , HIGH) ; (This turn the pin on HIGH, which turns the attached LED off.) } (This marks the end of the loop.) } (This marks the end of the setup section.) Programming the loop section: void loop ( ) { (This section runs over and over again automatically digitalWrite ( ledpins [ random ( 0, numleds+1 ) ], lightsw ( )
.) ); (This line picks a LED at random in the list of ledpins we defined before;
the digitalWrite command turns the LED on or off depending on what happens in lightsw ( ) it will return either HIGH or LOW.) delay ( 900 ) ; (This waits 900 milliseconds, which is the same as 0.9 seconds.) } (This is the end of the loop section.) Programing the lightsw program: boolean lightsw ( ) {
(This little program, “lightsw, ” will return a LOW 60% of the time on average, randomly
. Remember that setting a pin
low in our case turns the LED on!) if ( random (0,100) > 40 ) return LOW ; (This will generate a random number from 0-99. If the number is over 40 it will return a LOW.) else return HIGH ; (If the number was from 0-40 this returns a HIGH.) } (This is the end of the “lightsw” section.)
After you’ve entered all the text in red into the Arduino editor, push the “→” (arrow) after connecting to your Arduino Pro Mini and you are done programming. If you’d like more information, search the web for “Arduino getting started.”
FRA requirement, and the Arduino can easily handle timing and delay with ac- curacy to the thousandth of a second. In some cases, it can control timing to ¹₁,₀₀₀,₀₀₀th
of a second!
Table 2: Sequence of grade crossing actions Step Crossing action
Animation action
1 Wait for train approaching Look for block occupancy or train detector #1 2 Start crossing flashers Turn blinking lights on 3 Turn on bell
Turn bell sound on
4 Wait two seconds 5 Move gates down 6 Wait for train to pass 7 Move gates up
10 Go back to step 1 78 Delay for two seconds
Activate gate mechanism down (possibly a servo motor) Look for block occupancy or train detector #2 Activate gate mechanism up (possibly a servo motor)
8 Turn off crossing flashers Turn blinking lights off 9 Turn off bell
Turn bell sound off Restart the sequence
Complex sequencing can also be ac- complished in other ways. Many model railroaders operate with DCC control. By thinking of DCC as beyond the loco- motive, we can also consider scripting via JMRI. JMRI is used by many mod- elers for easing decoder programming tasks, but it can also be used with its “Jython” scripting tools using the tim- ing and control of a laptop or personal computer. With Jython we can create simple to complex sequences. Let’s use a DCC-controlled crane as an example. The model used here has tiny LEDs under the boom and larger LEDs for the work lights, three degrees of movement with the cab, boom, and main hook, together with prototypical steam boiler, whistle, bell, and mechan- ical sounds. Using JMRI scripting, you can orchestrate an animated scene with the crane. A portion of the script is shown as Table 3.
Opportunities beyond the norm I started using JMRI scripts to con- trol DCC sound decoders like the Digi- trax SDN144PS, which I embedded in
MAY 2014
Page 1 |
Page 2 |
Page 3 |
Page 4 |
Page 5 |
Page 6 |
Page 7 |
Page 8 |
Page 9 |
Page 10 |
Page 11 |
Page 12 |
Page 13 |
Page 14 |
Page 15 |
Page 16 |
Page 17 |
Page 18 |
Page 19 |
Page 20 |
Page 21 |
Page 22 |
Page 23 |
Page 24 |
Page 25 |
Page 26 |
Page 27 |
Page 28 |
Page 29 |
Page 30 |
Page 31 |
Page 32 |
Page 33 |
Page 34 |
Page 35 |
Page 36 |
Page 37 |
Page 38 |
Page 39 |
Page 40 |
Page 41 |
Page 42 |
Page 43 |
Page 44 |
Page 45 |
Page 46 |
Page 47 |
Page 48 |
Page 49 |
Page 50 |
Page 51 |
Page 52 |
Page 53 |
Page 54 |
Page 55 |
Page 56 |
Page 57 |
Page 58 |
Page 59 |
Page 60 |
Page 61 |
Page 62 |
Page 63 |
Page 64 |
Page 65 |
Page 66 |
Page 67 |
Page 68 |
Page 69 |
Page 70 |
Page 71 |
Page 72 |
Page 73 |
Page 74 |
Page 75 |
Page 76 |
Page 77 |
Page 78 |
Page 79 |
Page 80 |
Page 81 |
Page 82 |
Page 83 |
Page 84 |
Page 85 |
Page 86 |
Page 87 |
Page 88 |
Page 89 |
Page 90 |
Page 91 |
Page 92 |
Page 93 |
Page 94 |
Page 95 |
Page 96 |
Page 97 |
Page 98 |
Page 99 |
Page 100