# Implementing a Calculator on LEGO Mindstorms NXT-G ProPreview

In this lesson we build a simple LEGO Mindstorms NXT calculator program that sums, multiplies, subsctracts or divides two numbers between 1 and 50.

• #12
• 06 Apr 2013
• 14:56

The goal of the tutorial is to show you how you could implement a calculator using LEGO Mindstorms NXT-G software. There surely are many ways to achieve this. In one of the next lessons we are thinking of doing it even in Reverse Polish Notation and for this lesson we have decided to focus on the immediate execution part.

The input to out calculator comes from the user, but it might often come from a sensor. Selecting the required operation and doing the calculation between sensor values is a knowledge that could make your competition robots perform much better.

### English

002

In episode 10 – Making a menu as myBlock, we built the following myBlock. We can set the first and the last item in the menu, as well as the position on the y axis of the menu on the screen.

004

Now let's build a simple program, that sums up two numbers between one and fifty. I will use the created myBlock, set the minimal value to 1 and the maximal to 50. The y coordinate I will set to 50, so that the menu appears on the top of the screen. The selected item I will store in a new variable called A, so I create it, then I take a variable block and store the result. Now I want to show on the screen the plus sign, so I go to text, set the text to plus and change the y coordinate to 40. So that the plus sign appears under the selected item. Then I will need again a menu myBlock, set again the minimal value to 1 and the maximal one to 50 and the y coordinate I will set to 30. This time the result will be stored in a variable called B, again number type. So we are almost ready. Now I need to sum up the two values of the variables. I take two variable blocks, read the values of the variables and sum up them. The result I will convert to text using the number to text block and I will display it using the display block and set the y coordinate to 20, so that the result will appear at the bottom of the screen. We put a wait block for 5 seconds in order to see the result on the screen. At the end I will download the program.

006

Now I will show you how to solve this problem. We open the menu myBlock and go to the visualization part of it. We will concatenate the number with several spaces, so that the spaces will take the place of the zero and we connect the text block with the display block. We save the myBlock and download the program.

008

Now I will show you how to make a menu, which visualizes items different from numbers. In our case this would be the plus, the minus, the multiply and the division signs. I will use the program we have built in episode 7 – Programming a menu, but this time the binary search will not occur after the program, but in the visualization part of it. So that each time we visualize, we will know what is the current value and display the corresponding sign. I will delete this three blocks. Instead of the sound blocks I will put display blocks with the four signs. First is the plus. I will change the y-coordinate to 40, so that the sign appears under the selected number. Then I put the minus sign, again change the y-coordinate to 40. Now I will put the division sign and again change the y coordinate to 40. Last I will put the multiply sign with y-coordinate set to 40. Great! Now we have a menu, which visualizes the four signs. The last thing I want to add is the sound effects. I will put a click sound after each click we made. So here too – when we press the left button. Last, here, after we press the enter button. So now our program is ready and I will download it.

009

Okay, so now our menu is consisted of the plus, the minus, the division and the multiply signs and we can go around them using the left and the right buttons.

010a+b