The NXT-G language for the LEGO Mindstorms NXT lacks a simple bitwise AND operator between integer numbers. Mask is a simple block you can import into your NXT environment to add the AND function. For example if you only interested in the lower 4 bits of a number, set the Mask value to 15 which is a binary 00001111.
The Mask Block icon is shown above. The A input is any number you need to mask. The second input is the Mask itself. You can either enter the values with the block's configuration window or as input data wires. The Result is available on the lowest output data hub. It should be pretty obvious to use, so here is all the online help that comes with the block.
Click here to DOWNLOAD the Mask Block files as a zip file
1. Extract the files into a folder named Mask
2. Launch MINDSTORMS software and create a new program
3. Tools>>Block Import and Export Wizard
4. Click Browse and browse to find the Mask folder with the extracted files
5. Select the Mask block in the Import listbox
6. Choose the palette you want to add the block to.
7. Click Import
8. Close MINDSTORMS and reopen it to use the new Mask block
I wrote a general purpose Bit Logic Block and sent it to Steve Hassenplug to test drive. He improved it and sent it to Brady at National Instruments who made some more improvements. Then Steve made even more improvements, so I hardly consider this to be my work any more. This much much better Bit Logic Block is available from Steve's Site. I offer my humble Bit Logic Block only as an example on how to build a very simple NXT-G block.
If you happen to need more logic operations like AND, OR, XOR, and NOT the Bit Logic Block gives you all of the bit logic functions in one spot.
My Humble Bit Logic Block
The Bit Logic Block icon is shown above. The inputs are any two numbers (A and B) you need to do bitwise logic with. You can either enter the values with the block's configuration window shown below or as input data wires. The results are available on the output data hubs as AND NOTA OR and XOR in that order. It should be pretty obvious to use this block too, so here is all the online help that comes with the block.
Click here to DOWNLOAD my humble Bit Logic Block files as a zip file. Use the same step-by-step instructions as the Mask block only substitute Bit Logic wherever it says Mask.
Homebrew NXT-G Blocks
Random Number w/Seed Block
The NXT-G language for the LEGO Mindstorms NXT lacks a random number block that allows you to initialize or seed the random number stream. This capability is very useful for producing random and yet repeatable behavior. SRand allows you to enter a seed value and set the minimum and maximum values for the generated numbers.
It uses the simple random number generator shown above. The while loop generates a 15 bit random number that is remembered from call to call with a LabView shift register represented by the down and up triangles on the left and right walls of the loop. That is when the value of Seed equals 0. Othwise, if Seed is not 0 it takes the value and initializes the shift register as well as generating the first random number. This special case is shown below.
An example use of SRand is shown in the NXT-G program below. The first call seeds the generator with the value 12345. The next two calls have seed values of 0 and pick values from 0-63 and 0-99 to make X and Y coordinates to plot a dot on the NXT's display. The program slowly blacks the screen as random numbers are generated. Every time the program is run, the exact same pattern of dots will be generated unless you change the seed value.
Click here to DOWNLOAD the SRand Block files as a zip file. Use the same step-by-step instructions as the Mask block only substitute SRand wherever it says Mask.
POW X to the Y power function Block
The NXT-G language for the LEGO Mindstorms NXT lacks a power function that can raise X to the Y. POW is a simple block that inputs X and Y and calculates X raised to the Y power by repetitively multiplying X Y times. Except for the special case of X to the 0 which is 1.
The following is a simple example where powers of two are calculated and then inverted to create a pattern with one zero which is useful for output to a PCF8574 where a zero turns on an LED.
Click here to DOWNLOAD the POW Block files as a zip file. Use the same step-by-step instructions as the Mask block only substitute POW wherever it says Mask.