Nxt Lock Box Program

пятница 20 сентябряadmin
Nxt Lock Box Program Rating: 5,7/10 4056 reviews

Lego Mindstorms NXT robots are capable of displaying images, text and/or feedback on their screens. To get them to do this you have to learn how to program display blocks in the NXT programming environment. Program display blocks in the Lego Mindstorms system Click through to watch this video on ortop.org.

NXT Unplugged - Programming Without the Computer

With the NXT 1.26 (don't use that version!) and 1.28 firmware, you can write simple programs directly on the NXT brick. This is really handy for activities where you need the NXT to just do simple tasks - where the main goal is construction, rather than programming. It's under the 'NXT Program' icon. Press .
When you select it, the next screen tells you which ports you should use to connect the motors and sensors (if you're using any). Make sure your wires are connected correctly.
Now we're ready to program. Press .

The Big Picture - Five Program Blocks

This screen illustrates how you enter a program. You have 5 'blocks' that hold one step of the program. The black triangle indicates that you're going to enter the action for the first block. The five blocks are not all the same - different blocks can do different things:
Block 1 : Action (move forward, backward, turn, or make a sound).
Block 2 : Wait-For (time or sensor).
Block 3 : Action - same as before.
Block 4 : Wait-For - same as before.
Block 5 : Stop or Loop.

An example program might be:
1 - Move forward.
2 - Continue moving until the touch sensor is pressed.
3 - Back up turning right for 2 seconds.
4 - (empty)
5 - Loop 'forever'.
This program will make the robot wander around the room until the dog finds it and starts chewing on it, or something like that.
We'll now step through the process of building a program.

Fifteen Action Choices

The first and third blocks can contain actions that make the robot move or play a sound. If you scroll left and right using the arrow buttons, you'll see that there are 15 different actions:
The two musical notes play a sounds of different pitches. The empty box means 'do nothing'. You probably wouldn't want to 'do nothing' in the first action box, but you might in the second. All of the other choices are movement. The movement choices come in pairs, with and without a number. With the number, the NXT does that movement for either two or five seconds, depending on the number. Without the number, the NXT will do the action until the wait-for condition in the next block is satisfied. The possible movements are, from left to right:
  • Back up, turning left.
  • Go forward, turning left.
  • (do nothing)
  • Go straight backward.
  • Go straight forward.
  • Go forward, turning right.
  • Back up, turning right.
  • (play a sound)
Choose your action, then press .
The next screen shows two things. First, the black triangle has moved to the second block, indicating that you're going to choose a wait-for condition. Second, you can see a tiny spec of an icon in the first block - which is actually enough to tell you which action was selected. In this case, the action is forward (the arrow) for 5 seconds (the speck at the tip of the arrow).

Nine Wait-For Conditions

The second and fourth blocks can contain wait-for conditions that make therobot continue moving until the condition is satisfied. If you scroll left and right using thearrow buttons, you'll see that there are nine different conditions:
The empty box means 'don't wait - just go on to the next block'. The possible conditions are, from left to right:
  • Wait until the sound sensor hears something.
  • Wait until the light sensor sees more light.
  • Wait until the light sensor sees less light.
  • Wait until the touch sensor is pressed.
  • Don't wait - immediately cancel the previous movement.
  • Wait for 2, 5, or 10 seconds.
  • Wait until the ultrasonic range finder notices an object getting close.
If you're familiar with programming these wait-for conditions in NXT-G, you've probably noticed that there's no way to set thresholds here – how bright, how loud, or what distance. You're right. The NXT makes a guess about the threshold, probably based on the sensor value when the wait-for block starts.
When the condition is satisfied (for example, the touch sensor is pressed), the movement (and the only possibility is movement) chosen in the previous block is canceled. 'Don't wait' can be confusing, because the motors will immediately stop. So, if the motor runs for just a fraction of a second and stops, you've probably chosen a 'run until' in an action block followed by a 'don't wait' in the next block. You should only choose 'don't wait' when the previous action was a timed movement or sound.
Choose your wait-for condition, then press .
The next two blocks (3 and 4) are exactly the same as 1 and 2. So, for example, you could choose 'forward 5 seconds, wait 2 seconds, backward 5, wait 2 seconds'.
The last block lets you choose what happens when the first four blocks have finished. You've got two choices: stop and loop. Stop means just that - run the first four blocks and then stop. This will be fine for many situations, but is also useful when you're debugging a program that you plan to have loop.
Loop means to keep running the first four blocks, over and over - forever. Or, at least until the batteries run down.
Choose whether to stop or loop, then press .
Finally - you've finished the program. Just press to run it. While the program is running, the five blocks are shown with the black arrow underneath the active block. This is hand if, for example, your robot runs without stopping - you'd probably see that it was stuck in one of the wait-for blocks, waiting for.. something.
The other two choices are to go back to the main NXT menu - forgetting about the program or to save the program. Personally, I don't find the save option to be very interesting, because it takes longer to enter the name for the program than to re-create the program.

What if I make a mistake?

The dark-gray 'back' or 'up' button is your friend, at least a little. Each time you press it, you will erase one program block. So, pressing it once will take you to block 5, press again to get to block 4, etc.
I hope that you've found this tutorial helpful.


I hope you enjoyed the graphics I put in here. They are actual screen shots from my NXT, which I obtained using a handy utility called 'NXT Screen', which is part of NeXT Tools for Mac OS X, an open-source project. Those screen shots actually end up in gray scale. I massaged them into black-and-white, then changed the background color to green, using GraphicConverter.
19 Aug 2015CPOL
Program that continuously monitors the NXT via Bluetooth


Inspired by Pierre Poliakoff and bigbro, I wrote a C# program to communicate with my Mindstorms NXT that continuously displays the battery voltage and a sensor value. The two values are read from the NXT in separate threads, each created with the .NET Thread class, and displayed in scrolling text boxes.


The project consists of a C# program called NXT BT Monitor and an NXC program called NXCBTMON. I developed the C# program with Visual Studio 2010 on Windows 7 and .NET 4.0. I used BricxCC version for the NXC program; the protocol and firmware versions on my NXT are 1.124 and 1.31. The NXT light sensor is attached to port 3, and an NXT motor is attached to port A. I used the Medialink Bluetooth adapter with the Windows 7 Broadcom driver, but other Bluetooth adapters will work; on-line NXT documentation lists adapters that are compatible with the NXT. Bluetooth drivers that are not compatible with the NXT seem to be the most common source of issues.


It is assumed the reader is familiar with the NXT and its sensors and motors, the C# and NXC languages, the Visual Studio and BricxCC IDEs, as well has having experience with Bluetooth on Windows 7 and the NXT. John Hansen and Daniele Benedettelli have both written books that are great resources for information on NXC and Bluetooth on the NXT. You may also want to get a copy of the 'Mindstorms NXT Communication Protocol', 'Mindstorms NXT Direct Command', and 'Bluetooth Support for NXT' documents which can be found online or by contacting Lego directly. These documents describe the various commands which can be sent to the NXT and the data that is returned. Debugging and troubleshooting Bluetooth issues requires persistence and creativity, and may mean occasionally cycling power on the NXT, starting and stopping Visual Studio or BricxCC, or even rebooting Windows 7. The NXT must also be in close proximity to the Bluetooth adapter. Having fresh batteries on hand for the NXT is a must. Keep in mind that only one program at a time - either BricxCC or NXT BT Monitor - can be connected to the NXT.

Using the code

The program that runs on the NXT, NXCBTMON, consists of two files: NXCBTMon.nxc and protocol.h. Power on the NXT, build and download NXCBTMON using John Hansens's wonderful BricxCC IDE, then close BricxCC and power off the NXT.

Open the solution file for the C# program, NXTBTMonitor.sln, in Visual Studio. Build and run the NXT BT Monitor in Visual Studio. If you have the console window attached for debugging (described below), you can see that obtaining the available COM ports using WMI via the ManagementObjectSearcherPorcelain patch and glaze complaints. class can take several seconds. (A future enhancement is to display a 'Please Wait' dialog while the COM port query is running.)

Power up the NXT, and in NXT BT Monitor, select the port that corresponds to your Bluetooth adapter (available via Windows Control Panel=>View Devices and Printers) and click the 'Connect' button. The status should change from 'Disconnected' to 'Connected' within a few seconds. If the connection is not successful, you may want to review the troubleshooting ideas above. Note that the port you select is saved as a user setting and automatically selected the next time you run the program (see the Settings.Settings file). After NXT BT Monitor is running and connected to the NXT, you can run NXCBTMON to start sending sensor readings to NXT BT Monitor.

Points of Interest

The essential class for communication between NXT BT Monitor and the NXT is the .NET SerialPort class.

There is a Property on this class called ReadTimeout that specifies the number of milliseconds before a time-out occurs when a read operation does not finish. NXT BT Monitor has a corresponding parameter in the configuration file called ReadTimeout that you may need to experiment with. It is currently set to two seconds. After the NXT BT Monitor is connected to the NXT, two threads are started, one to monitor the light sensor and one to monitor the voltage of the batteries. The amount of time the threads sleep can be specified in the configuration file via the SensorThreadSleep and BatteryThreadSleep parameters. A consequence of updating controls from a new thread is that we must call BeginInvoke; see the code for the UpdateEditCallback and UpdateVoltageCallback delegates. Since both threads can access the serial port object, the .NET lock construct is used to only allow one thread at a time to access the Bluetooth port.

NXCBTMON has a task called ReadLightSensor() that reads the light sensor and sends the value to NXT BT Monitor. The SensorRead() thread of NXT BT Monitor receives the message and displays the value. Likewise, the BatteryRefresh() thread queries the NXT for the voltage of the batteries and displays it, both in text and in a progress bar. The progress bar will show green, yellow, or red, depending on the voltage. Clicking the 'Get Info' button will query the NXT for its name, the Bluetooth address, and free user flash memory in bytes.

Clicking the 'Get Version' button will query the NXT for the protocol and firmware major and minor revision numbers. You can send a message to an NXT mailbox by typing in the textbox above the 'Send' button, and then clicking the button. The NXCBTMON in turn will display the text on the NXT’s LCD screen. Currently NXCBTMON program only reads mailbox 0. Sending the message 'start' to NXCBTMON will cause the motor attached to port A to start.

The code in NXT BT Monitor to communicate with the NXT is encapsulated in specific classes. For example, clicking the 'Get Info' button causes the class GetDeviceInfo to be instantiated and the methods can then be called on it to get the information. In the comments of each class I have supplied the relevant information from the aforementioned 'Mindstorms NXT Communication Protocol' document and constants with meaningful names to avoid 'magic numbers' in the code. For example, here is a snippet from GetDeviceInfo.cs:

Performance Monitor and Debugging

An interesting feature of NXT BT Monitor is the ability to display the battery voltage and sensor reading obtained from the NXT in the Windows Performance Monitor. Readers who have experience with the Windows Performance Monitor and want to enable this feature can add the counters using Server Explorer running as Administrator (illustrated below), and then uncomment the relevant lines of code shown below:

See for more details.No rude, offensive, or hateful comments. Read and understand. Nba 2k13 ps3 gamestop Then read it again.We love developers. However we want to verify the identity of anyone posting on behalf of a company/project.

Nxt Lock Box Program

Here is a screen shot of the Performance Monitor displaying the values of the battery voltage (in green) and the sensor value (in red). They were added by clicking the green plus sign in the Performance Monitor toolbar.

Another nice feature of NXT BT Monitor is the attachment of a console to the Windows Forms project for debugging purposes using AllocConsole and .NET Interop. It is enabled by default; to disable it, simply comment out the first 7 lines of Main:


Version August 17, 2015