In this lesson we build a simple LEGO Mindstorms NXT calculator program that sums, multiplies, subsctracts or divides two numbers between 1 and 50.
- 06 Apr 2013
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.
- Download the whole calculator, ready for use.
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.
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.
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.
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.
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.
Now I will add the possibility to read the value of the count variable outside of the program and create a myBlock from the program. For this purpose I will read the value of the count variable after we have selected the operation we want and write it into the number 1 variable. Then I select all of the blocks except for the number 1 variable and create a myBlock from them. Let’s name it sign, because we choose the sign in it, click Next, choose an icon and click Finish. Now, we have a new myBlock and I will go back to the program we have created earlier. Instead of just sum up the two numbers, I will do the operation we have selected. I will delete this Display block and on its place the myBlock we have just created. I will store the count variable’s value into a new variable called operation, so I will take the variable block and write into operation. Next we need again a binary search in order to know what is the value of operation. I won’t do it again, but I will copy it from the myBlock we have just created and paste it in the new program. I have spotted that this wire is broken, so I will delete it and reconnect the two blocks, select them and move them just before displaying the result. The string here is broken also, so I will reposition the blocks and again reconnect them. I will delete the sum up blocks and these three reposition and delete the broken strings. The binary search is about the count variable, but we need it about operation, so I will change the variable here, here and here. Instead of the display blocks we will do the operation with the two numbers. So I read the B variable and the A variable and in this case, where is plus, I will sum up them. The result I will write into a new variable called “result”. I have created it earlier. I repeat this procedure with the other three cases. In here it will be subtraction, so again I read the values of A and B, (4:21,4) using the math block I will subtract the two numbers and then store the result from the subtraction into the result variable. Okay, next. Here we have division so again I read the values of the A and B and using the math block I divide them. The result I write into the result variable. Last, we have to multiply the two numbers, so I read the B variable, the A variable and multiply them and write the result into the result variable. I have forgotten to set it to write. Now we have the result into the result variable and we have only left to display the result variable, so I will read the result variable, convert it to text and display it. Now, we have a working calculator I have only to download it and try it.
011Okay, so finally we have build a calculator. Let's say we choose 11, we divide it by 3 and the result is 3.667.