EasyManuals Logo

LEGO MINDSTORMS Robots User Manual

LEGO MINDSTORMS Robots
226 pages
To Next Page IconTo Next Page
To Next Page IconTo Next Page
To Previous Page IconTo Previous Page
To Previous Page IconTo Previous Page
Page #114 background imageLoading...
Page #114 background image
Bitwise operators are also defined: AND, OR, XOR.
You can change the number base you're working in using the HEX and DECIMAL words. For example, if you wanted to work in hexidecimal (base 16), you would type HEX. Base 10 is
represented by DECIMAL. Most of the numbers in this chapter will be base 16.
Page 123
BASE is a word (a variable, which I'll talk about soon) that contains the current number base. To print out the current number base, for example, do this:
BASE @ .
10 ok
Interestingly, the current base is printed in terms of the current base, so it will always be 10. Suppose you're working in base 8. If you print out the current base, 8, in base 8, it's expressed as 10.
Retrieving the current base with BASE @ may seem like a pointless exercise, but it's useful if you want to save the current base away to be restored at some later time.
If you want to work in a number system other than HEX or DECIMAL, you can use BASE ! to store any base number. The following example shows two ways you can use Forth to convert
numbers between different bases:
HEX 20 DECIMAL . (20 in base 16 is 32 in base 10)
32 ok
8 BASE ! 20 DECIMAL . (20 in base 8 is 16 in base 10)
16 ok
The open parenthesis ( is used to indicate a comment. The pbFORTH interpreter simply ignores the rest of the line after it sees the (.
Defining words
Writing programs in Forth is a matter of defining your own words in the dictionary. Word definitions begin with a colon and end with a semicolon. Here's a simple example:
: threeTimes DUP DUP + + ;
ok
The colon tells the Forth interpreter that the words that follow define a new word for the dictionary and should be stored to run later. The new word needs a name, threeTimes in this example,
which is supplied immediately after the colon. Subsequent words will be executed when the new word is executed. The semicolon tells the interpreter that the new word definition has ended.
threeTimes duplicates the top item on the stack twice, so there are three copies of it, then adds them all together. Now that you've defined a new word, you can use it like any other Forth word:
5 threeTimes .
15 ok
And you can, of course, use it in subsequent definitions:
: nineTimes threeTimes threeTimes ;
ok
5 nineTimes .
45 ok

Table of Contents

Questions and Answers:

Question and Answer IconNeed help?

Do you have a question about the LEGO MINDSTORMS Robots and is the answer not in the manual?

LEGO MINDSTORMS Robots Specifications

General IconGeneral
Product LineLEGO MINDSTORMS
CategoryToy
Age Range10+
Batteries RequiredYes
Memory64 MB RAM, 16 MB Flash
ConnectivityBluetooth, USB
SensorsTouch, Color, Gyro
Battery TypeAA
Interactive FeaturesProgrammable, Remote Control
App CompatibilityiOS, Android
ProcessorARM9
Pieces Count601
Battery Count6
App NameLEGO MINDSTORMS EV3 Programmer App

Related product manuals