In this video lesson, we will show you how to build a menu as a MyBlock in LEGO Mindstorms NXG-G. Having a menu is a must at robotics competitions since it saves time and gives easy access to program functionalities.
- 24 Mar 2013
The menu block created in the tutorial takes three params - the min number that could be selected, the max number that could be selected and the Y coordinate for displaying the menu on the NXT screen.
In this tutorial we use the menu created in Episode 7 as a starting point for building the myBlock.
In episode 7 – Programming a Menu, we have built the following program. You can find it under the video.
The program was a menu with four items and you can cycle around them using the left and the right NXT buttons. When you select one of the items, the brick says which one is it.
In today's video I want to show you how to build a my block for a menu. “My Block” receives as arguments the first item in the menu, the last item in the menu and the position of the menu on the screen. Here is a simple example. I will make two menus, one under another. The first one will begin from 1 till 5 and the second one from 4 to 9.
The first menu begins from 1 to 5. After we have selected an item, the second one appears over it and it is from 4 to 9 and we select an item.
Okay so now let's begin with the programming. First I will delete the binary search part, because we will not need it. Then I want to add the ability to change the first and the last item in the menu. I will define two new variable – the first one called minValue, which will store the minimal value and maxValue, storing the last item in the menu. In the case when we increase the current value with one we compare it with four. Now we do not know the maximal value, so I will compare it with the maxValue variable. If the current value is greater than the maximal one I will have to set it to the minimal one. The same I will do with the case when we decrease the value. This time instead of comparing the current value to the maximal one, I will compare it to the minimal one. If the current value is less than the minimal one I will set the current value to the maximal one. Now I have to initialize the minimal and the maximal value in order to test the program. First let's do this with the minValue. Let's set it to 3 and the maxValue set to 8. Of course, the current item should begin from the first item in the menu, so I set the count variable to the value of the minValue variable. We are ready to test it!
Okay, so now we have a menu starting from 3 till 8 and we can cycle around it.The cool thing about the program is that with only two clicks we change the menu to begin from 10 to 15.
The next feature I would like to add is to change the position of the menu on the screen. For this purpose I will create a new variable called Y, which will store the position of the menu on the y-axis. Now I want to set the display block to position the items on the same coordinates as the value of Y. Next I have to initialize the Y variable. Let's set the coordinate to 10. Now let's try the program.
Now as you can see, when we changed the coordinate to 10, the menu is a little bit lower than it was before. Still it's from 3 to 8 and we can cycle around it.
Now I want to add some sound effects. Each time after I have clicked, I want the robot to make a click sound. So if we have pushed the right button, I put a click sound. I do the same, when we push the left one and after we have selected the item in the menu we want. Next I will add the ability to change the minValue, maxValue and Y variable, outside of the program. That's by taking the predefined number 1 variable and using it initialize the values of the three variables. Then I want to optimize a little bit the number of the blocks, so I will delete this one and initialize the count variable from the minValue here. At the end we will take the current item after we have selected it and store it in the number 1 variable. Now we select all of the blocks except of the number 1 variable blocks like this and click on make a new myBlock. Let's call it Menu, click next, give it an appropriate icon and click finish. Now we have a new myBlock with three inputs and one output. I will click on it to edit the names of the inputs. So this will be maxValue, this one will be called Y and this one will be minValue. This we will call result. I save the program. Open a new one, go to myBlocks and take the Menu myBlock and now we have the myBlock created.