Page 148 - The Unofficial Guide to Lego Mindstorms Robots
P. 148

137


          To dive into this a  little more, check out the whole tortask.txt example at the pbFORTH web site.

          An Expensive Thermometer

          This section contains an example that will help you  get your feet  wet with pbFORTH.  We'll build a simple Celsius
          thermometer using a temperature s ensor. Hook the sensor up to input 2 and enter the following (I've omitted pbFORTH's ''ok"
          responses for clarity):

              HEX
              : buttonState RCX_BUTTON DUP BUTTO       N_GET @ ;

              : i sRunButtonPressed buttonState 1 AND ;

              : showTempera  ture
                3003 SWAP 3001 LCD_NUMB     ER
                LCD_REFRESH
              ;

              : clear LCD_CLEAR LCD_    REFRESH ;

              : thermometer
                RCX_INIT
                S ENSOR_INIT
                BUTTON_INIT
                2 1 SENSOR_TYPE
                A0 1 SENSOR_MODE
                BEGIN
                  BEGIN
                      1 SENSOR_READ 0=
                  UNTIL
                  1 SENSOR_VALUE
                  showTemperature
                  isRunBu ttonPressed
                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 isRunButtonPre  ssed word simply tests to see if Run has been pressed, using the buttonState word. This is
          how  thermometer  knows to stop running.  ShowTe mperature  takes the top item on the stack and shows it on the
          display with the de cimal point placed to show tenth s. The  clear  word simply erases the  entire display using the
          LCD_CLEAR word.
   143   144   145   146   147   148   149   150   151   152   153