
BEGIN
BEGIN
1 SENSOR_READ 0=
UNTIL
1 SENSOR_VALUE
showTemperature
isRunButtonPressed
UNTIL
clear
;
To run this program, use the
thermometer
word. The LCD display will show the temperature read by the sensor until you press the
Run
button.
The
isRunButtonPressed
word simply tests to see if
Run
has been pressed, using the
buttonState
word. This is how
thermometer
knows to stop running.
showTemperature
takes the top item on the stack and shows it on the display with the decimal point placed to show tenths. The
clear
word simply erases the entire display using the
LCD_CLEAR
word.
Page 138
The main program,
thermometer
, begins by initializing the RCX and the input and button systems. It then configures input 1 for a temperature sensing 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_READ
. This call
is inside its own
BEGIN UNTIL
loop that waits for
SENSOR_READ
to return 0, indicating success. 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 button 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 sensor, you now have yourself a $225 digital thermometer. Chapter 11,
Make Your Own Sensors,
shows how you can build your own temperature sensor. You
could use that code to make a digital thermometer for only about $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 pieces that are small enough to be understood easily. Here's a set of words that will run Minerva. They are defined in bottom-
up fashion, such that the most sophisticated words are at the end.
HEX
: initialize
RCX_INIT
SENSOR_INIT
2 SENSOR_ACTIVE
3 2 SENSOR_TYPE
80 2 SENSOR_MODE
;
: showValue
3002 SWAP 3001 LCD_NUMBER
LCD_REFRESH
;
: sleep
0 0 TIMER_SET
BEGIN DUP 0 TIMER_GET = UNTIL
DROP
;
Содержание MINDSTORMS Robots
Страница 22: ...Page 18 The back tread wheels are anchored to the shafts with the 16t gears ...
Страница 23: ...Page 19 Page 20 Next start building support for the drive motors ...
Страница 25: ...Page 22 Attach the motor wires to output A and output C Next build the platform for the front bumpers ...
Страница 26: ...Page 23 The touch sensors are attached only by the shaft that runs through them ...
Страница 41: ...Page 41 ...
Страница 43: ...Next build the support for the light sensor ...
Страница 80: ...Page 85 Make sure the bump on the long pin is up against the 4u beam ...
Страница 82: ......
Страница 84: ...Page 89 ...
Страница 85: ...Step 14 is similar to Step 11 take a deep breath and go slowly ...
Страница 86: ...Page 90 Grabber Arm In Step 17 the half bushings go between the center block and the cams pear shaped pieces ...
Страница 87: ...Page 91 ...
Страница 88: ...Page 92 Make sure that the two sides are at the same angle They should mirror each other ...
Страница 89: ...Page 93 Page 94 ...
Страница 90: ...Structural Support Page 95 ...
Страница 91: ...Idler Wheel Page 96 ...
Страница 92: ...Page 97 Drive Motor ...
Страница 93: ...While you re putting the motor in hold on to the worm gear so it doesn t slip off Page 98 ...
Страница 94: ...Grabber Arm Motor ...
Страница 95: ...Page 99 ...
Страница 96: ...Page 100 RCX Attach the RCX on both sides as shown Page 101 ...
Страница 158: ......
Страница 159: ...Page 175 The 16t gears are nestled inside the tread wheels ...
Страница 160: ...Page 176 ...
Страница 161: ...Page 177 Attach the motors to output A and output C as shown Page 178 ...
Страница 162: ...The light sensor which is mounted on the bumper is attached to input 2 The touch sensor goes on input 1 Page 179 ...