that again, but we’re also going to add a custom program
switch so that we can decide on serial output for a network
of RC- 4 boards, or parallel output when using the program
with a Stamp CI board and Opto- 22 relays.
Let’s start at the very top. Using #DEFINE, we can
create a conditional symbol that will affect other parts of
the program.
#DEFINE __SerialMode = 1
#IF __SerialMode #THEN
Sio PIN 15
#ELSE
Lights VAR
#ENDIF
OUTS
Speed
PgmSelect
PIN
PIN
14
12
In order to keep conditional symbols straight in my
own head, I’ve decided to preface them with two underscore characters. As you can see, the first place we use the
__SerialMode symbol is to define our output structure. An
important thing to remember is that the only parts of the
program that get compiled and downloaded are those that
satisfy the conditional statement. In the code above, for
example, the program doesn’t ever know about the symbol
called Lights because the setting of __SerialMode excludes
it from compilation. Of course, if we want to switch to
parallel mode (for use with the Stamp CI board), we would
change the value of __SerialMode to zero.
There are two I/O pins common to both modes: one
selects the output sequence (we’re keeping this very simple
with just two sequences), and the second is used to read a
potentiometer with RCTIME so that we can control step
speed when the program is running.
Moving into the body of the program we will use
__SerialMode again, this time to set pins P0-P11 to
outputs when we’re configured for parallel mode:
Reset:
#IF (__SerialMode = 0) #THEN
DIRS = $0FFF
#ENDIF
lightVal = $0000
GOSUB Update_Outputs
In either case, the state of the light outputs (in
lightVal) is cleared and the Update_Outputs subroutine is
called to initialize this state:
Update_Outputs:
#IF __SerialMode #THEN
SEROUT Sio, Baud, [“!RC4”, %00, “S”, lightVal.NIB0,
““““““““““““““““““““““!RC4”, %01, “S”, lightVal.NIB1,
““““““““““““““““““““““!RC4”, %10, “S”, lightVal.NIB2]
#ELSE
Lights = lightVal & $0FFF
#ENDIF
RETURN
And here’s where we really get to the point of using
__SerialMode as it directs the value of lightVal to the appro-
priate destination. This is a really good demonstration of
the power and flexibility of PBASIC2. Since each RC- 4 has
four outputs (conveniently, the size of a nibble), we are able
to use the NIB variable modifier to select the appropriate
bits (used by the RC- 4’s Set command) for each board.
You may be wondering if this will work as we’ve
squeezed three output commands into a single SEROUT
statement. Yes, it does work. The reason it does is that
each RC- 4 is waiting for the “!RC4” header and its own
board address. If the board address is wrong, that RC- 4 will
go right back to waiting for the header (for those who are
also interested in programming the SX with SX/B, the RC- 4
control code is written entirely in SX/B with the same techniques we used last month with the PSX helper). By putting
all three commands into a single SEROUT statement, the
program runs a little quicker (because we don’t have to
reload and configure additional SEROUT statements) —
this will be important when we’re running quick steps.
Just a couple tips on the RC-4: It actually gets its power
from the serial line, so if you’re going to connect it to a BOE
servo header, you must
make sure the servo ■ FIGURE 2. RC- 4 Networking.
power jumper is set to
Vdd (+ 5 VDC). On the
RC- 4, you’ll see two
headers marked SER.
This allows the RC- 4 to
be daisy-chained as
shown in Figure 2. Note
that the Baud jumper is
inserted for 38.4K when
using the BS2-family. If
you’re going to connect
the RC- 4 to a Prop-1,
remove the Baud
jumper for 2400 baud
operation.
As you can see in
Figure 2, each RC- 4
has two jumpers, A0
and A1, that allow the
board to have a unique
address (%00 to % 11).
When a jumper is
inserted, that address
bit is set to one;
removed clears the bit
to zero. In Figure 2, the
top RC- 4 is set to
address %00 (both
jumpers removed) and
the lower board set to
%01 (A0 in, A1 out).
The other nice thing
about the RC- 4 is that
it conforms to the
Parallax AppMod protocol, so you can put it
February 2006 85