Chapter 20. Basic Math

Don't you just love short chapters? Well, I promise this is going to be another extremely short chapter. So get ready to add another programming block to your collection of tools—you're going to turn your robot into a calculator.

The MATH Block

Your robots can do some very simple math: addition, subtraction, multiplication, and division. Take a look at Figure 20-1, which shows the MATH block and its configuration panel.

The MATH block and its configuration panel

Figure 20-1. The MATH block and its configuration panel

The MATH block uses two values: A and B. For NXT-G 1.0 users, these values can be positive or negative integers. An integer is a whole number with no decimal values. If you attempt to enter in a number such as −4.3 or 10.8 the MATH block will round the values up or down to the nearest integer (−4 and 11 for my examples). But for NXT-G 2.0 users, you are not limited to integer math. Decimal values are allowed.

In the Operation section, there is a drop-down menu for you to select the type of operation to be performed. If you click the drop-down menu, you should see the following options:

  • Addition: This option will add values A and B.

  • Subtraction: This option will subtract value B from value A.

  • Multiplication: This option will multiply value A by value B.

  • Division: This option will divide value A by value B.

  • Absolute value: This option will calculate the absolute value of A.

  • Square root: This option will calculate the square root of value A.

Note

NXT-G 1.0 does not offer the ability to calculate Absolute Value or Square Root.

Notice in Figure 20-1 that I've opened the data hub on the MATH block. There are two input data wire plugs (one for value A and one for value B) and three output data wire plugs. The MATH block requires one or two integer values, depending on the option you selected in the Operation section: if A or B is left blank, its value defaults to 0, except for Absolute Value or Square Root where only Value A is used.

Numbers can be entered manually by typing a value for A and a value for B in the configuration panel, or they can be provided to the MATH block by running one data wire into plug A and another data wire into plug B (see Figure 20-2).

The MATH block can receive data wires for values of A and B.

Figure 20-2. The MATH block can receive data wires for values of A and B.

Figure 20-2 shows a sample program that creates two random numbers (generated by two RANDOM blocks). Each RANDOM block uses its output data plug to run a wire into the A and B input data plugs on the MATH block.

You should understand that you really can't do anything with the MATH block without having its data hub open. The reason for this is simple: no matter what operation (addition, subtraction, multiplication, or division) you choose to perform on values A and B, the answer can only be obtained from the Result data plug (using a data wire).

Once values A and B have been added together, you want to see the answer on the LCD screen. To do this, you have to first convert the number to text. Drop in a NUMBER TO TEXT block (see Figure 20-3) and drag a data wire from the Result data plug on the MATH block into the Number data plug on the NUMBER TO TEXT block.

The NUMBER TO TEXT block will convert the numeric answer to text.

Figure 20-3. The NUMBER TO TEXT block will convert the numeric answer to text.

In order for you to see the answer on the Brick's LCD screen, you drag a data wire out of the Text data plug on the NUMBER TO TEXT block and into the Text data plug on the DISPLAY block; see Figure 20-4 (remember to change the DISPLAY block to show Text). Also, add in a small WAIT block, so you can see the results before they disappear off the screen.

The DISPLAY block will show the result on the LCD screen.

Figure 20-4. The DISPLAY block will show the result on the LCD screen.

Exercise 20-1: Calculate Total Degrees Traveled

Program your robot to roll a random number of degrees. The random number should be between −360 and −1 (negative values). After the movement is complete, generate another random number using the same range and have the robot roll that number of degrees. Display on the screen the total number of degrees turned as a positive integer. Check your solution against mine that I've provided at the end of this chapter.

What's Next?

That's it for the MATH block! Remember, value A and value B must be positive or negative integers and will default to zero (0) if you don't configure a value for them. If you plan on displaying the results of your MATH block, you'll need to convert the number to text before sending it to a DISPLAY block. Have fun! Up next, I'll show you how to prevent your robots from falling asleep.

Exercise Solution

Figures 20-5 through 20-14 show the complete program and all configuration blocks for Exercise 20-1. Notice that the values generated by the RANDOM blocks are negative values. These will be sent to a MATH block and an absolute value calculated for both values before sending to the MOVE blocks.

The complete program and the first RANDOM block's configuration panel

Figure 20-5. The complete program and the first RANDOM block's configuration panel

The first MATH block's configuration panel

Figure 20-6. The first MATH block's configuration panel

The first MOVE block's configuration panel

Figure 20-7. The first MOVE block's configuration panel

The second RANDOM block's configuration panel

Figure 20-8. The second RANDOM block's configuration panel

The second MATH block's configuration panel

Figure 20-9. The second MATH block's configuration panel

The second MOVE block's configuration panel

Figure 20-10. The second MOVE block's configuration panel

The third MATH block's configuration panel

Figure 20-11. The third MATH block's configuration panel

The NUMBER TO TEXT block's configuration panel

Figure 20-12. The NUMBER TO TEXT block's configuration panel

The DISPLAY block's configuration panel

Figure 20-13. The DISPLAY block's configuration panel

The NXT BUTTONS WAIT block's configuration panel

Figure 20-14. The NXT BUTTONS WAIT block's configuration panel

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset