The last thing you want to lose during every robotics competition is time for changing the programs on the brick. This could require seconds and as we all know every second counts. There are a few ways to improve the way of choosing programs. In this video lesson we will show you how to program a menu directly in your program.
- 15 Mar 2013
The menu could contain 4 subprograms and you will skip pressing the "Back" button of the brick or pressing the "Orange" button twice to run a program. Just select and run. It is a very powerful paradigm that includes the use of variables, range block, switches. You could also use it in connection with sensors so that different programs are automatically started in a specific time, or on a specific sensor input. We will show you all this in details in one of the next video lessons.
- The source code of the Menu written in NXT-G. Download and run it.
Today, I want to show you how to program a menu. You can use it to choose from different programs for instance and save time during the competition. In this video the task, that we would solve is as follows: When we start the program on the screen of the brick is showed 1. When we push the right button we increase the value on the screen with one and if we press the left button, we decrease the value with one. The cool thing is that the maximal value is four and the minimal is one. When we reach four and want to increase, we set the value to one and when we reach one and want to decrease, we set the value to four. We select the value we want with the enter button. After we have selected the value the brick says the number we have selected. Okay, so let's begin with the program. First we have to initialize the value of our variable. For convenience we will make a new variable. This is by entering the edit menu and choosing define variables. It pop-ups this window. We click create and set the name and the type of the variable. We will call it count and set in to number. Now we have our new variable and we initialize it with value 1. Then we will put a switch block to check whether the right button is pressed. It is very important that we have set the condition to bumped. Otherwise it will count it several times as pressed, because the operations of the brick are faster than our finger. If true we read the value of the variable, add 1 and write it as the new value of the variable. Then we compare the value with 4 and if it is greater than 4 we set the value to 1. If the right button is not pressed we check whether the left one is. If so we decrease the value with 1 and compare the new value with 1. If it is less than 1 we set the value to 4. So now we are ready we put all of that in one big loop with condition to repeat until the enter button is pressed. Parallel we put a loop with the same condition which will be responsible for the visualization of the value of the variable. So we read the value, use number to text block to convert the value to text and then we put display block. Don't forget to set the display to text. Now we have only to make the robot say the value of the variable. I will show you two ways to do that: The first one is by putting four switches each comparing the value with 1, 2, 3 or 4 and if the value is equal the robot says the corresponding number. But this is not cool enough Now I will show you another way. First we have a switch that compares the value with 3. That's by reading the value of the variable and use compare block. Then set the switch to value and use the value of the compare block. If it is less than three than it is one or two. So we put another switch that compares the value with 2. If it is less than two the value is 1 and if not is 2. In the case that the value is not less than 3 it is three or four. So we put a switch that compare the value with 3. If it is greater than 3 it's 4 and if not its 3. So our menu is ready. Let's see how it is working! CamVideos: Now, let's see how the program works. We start the program and on the display is written one. We can move around the numbers using the left and the right button. As you can see after four follows one and vice versa. Let's choose three. As you can hear the brick said three so the program is working pretty well. Now, let's see how the second version of the program works. Again we can cycle around the numbers. This time let's choose one. As you saw there is no visible difference between the two versions, but in case, when the choices are more than four, the second way can be notably faster.