Page 149 - The Unofficial Guide to Lego Mindstorms Robots
P. 149
138
The main progra m, thermometer, begins by initializing the RCX and the input and button systems. It then configures input
1 for a temperature s ensing using the temperature type (2) and the Celsius mode (A0).
The main part of temperature is a BEGIN UNTIL loop that reads a value from the input and shows it on the display. To
retrieve the input value we first have to call SENSOR_RE AD. This call is inside its own BEGIN UNTIL loop that waits for
SENSOR_READ to return 0, indicating suc cess. Then the actual input value is read using SENSOR_VALUE. A call to
showTemperature puts the input value on the display. Each time through the loop we call isRunButtonPressed; if
the butto n is pressed, we fall out of the loop. A call to clear cleans up the display, and then
thermometer is done.
If you bought LEGO's temperature s ensor, you now have yourself a $225 digital thermometer. Chapter 11, Make Your Own
Sensors, shows how you can build y our own temperature sensor. You could use that code to make a digital thermometer for
only abo ut $202 .
Minerva Revisited
To really put pbFORTH through its paces, let's rewrite Minerva's software in pbFORTH. (For a full description of this robot,
see Chapter 5, Minerva, a Robot with an Arm.) As with any other language, the challenge with Forth is to break the large
problem into piece s that are small enough to be understood easily. Here's a set of words that will run Minerva. They are
defined in bottomup fashion, such that the most sophisticated words are at the end.
HEX
: initializ e
RCX_INIT
SENSOR_INI T
2 SENSOR _A CTIVE
3 2 SENSOR_T YPE
80 2 SENSOR_ MODE
;
: showValue
3002 SWAP 3001 LCD_NUMBER
LCD_REFRESH
;
: sleep
0 0 TIMER_SET
BEGIN DUP 0 TIMER_GET = UNTIL
DROP
;