Hardware: I/O and Sensors
|
Hardware: I/O and Sensors
This section covers the classes for user input and output, and also the standard NXT sensors. The NXT hardware has buttons for input, a Liquid Crystal Display (LCD) and a small speaker for output. leJOS NXJ provides software abstractions for all these bits of hardware. LCDThe LCD class has no instances (there being only one LCD on the NXT), so all the methods are static. It can be used in text mode and graphics mode. LCD Text methodsAs a text display, the NXT LCD screen is 16 characters wide and eight characters deep. It is addressed using (x, y) co-ordinates as follows: x ranges from 0 to 15, and y from 0 to 7. The methods to write to the LCD in text mode are :-
Example:
Note that you can also write to the LCD display with System.out.println(String str). This scrolls the display up one line and writes to the bottom line of the screen. Note, also, that by default, the LCD display is refreshed automatically. If you want to control when the LCD is refreshed, you can call LCD.setAutoRefresh(0) to turn off auto-refreshing and call LCD.refresh() when you want to refresh the display. Back to topLCD Graphics methodsAs a graphics display, the NXT LCD screen is 100 pixels wide and 64 pixels deep. It is addressed using (x, y) pixel co-ordinates in the same way as for text co-ordinates: x ranges from 0 to 99, and y from 0 to 63. To display graphics on the LCD screen, you can use the Graphics class from the package javax.microedition.lcdui. See the Graphics class API. With this class, you can draw lines, rectangles, arcs, and position strings with pixel accuracy. Example:
ButtonsThe Button class has four instances, accessed by static fields:
To test if a button is pressed, you use:
Example:
To wait for a specific button to be pressed and released, you use:
Example:
To wait for any button to be pressed, you use:
button ENTER LEFT RIGHT ESCAPE Code 1 2 4 8 To specify a listener to listen for button events for this button, you: use
To read the current state of all the buttons, you use:
SoundThis class controls the single speaker so it has no instances and all the methods are static. To play a single tone, use
Example:
There are two ways to play system sounds. One is:
The values of code are: code = 0 Short beep Individual methods to play a particular system sound, if you don’t remember the code, are
There is also a method to produce a rest when playing a tune; time in milliseconds
leJOS NXJ has methods that can also play 8-bit WAV files:
The return value of milliseconds the sample will play for or < 0 if there is an error. To play a musical note, use:
BatteryThere are two static methods to get the battery voltage:
Example:
Back to top
SensorsThe NXT comes with four sensors; the touch sensor, the sound sensor, the light sensor and the ultrasonic sensor. leJOS NXJ provides software abstractions of all these sensor types, as well as many provided by third parties. A physical sensor must be connected to a port, and the sensor object must know which port this is. To provide this information, you create an instance of the sensor, and pass this information in its constructor. The possibilities are: SensorPort.S1, S2, S3 or S4. Touch SensorTo use a touch sensor, you create an instance of it, using the constructor:
To test if the touch sensor is pressed, you use the isPressed() method:
Example:
Back to top
Light SensorTo use a light sensor, you create an instance of it using the constructor:
Example:
Sound SensorThe sound sensor supports two modes: DB and DBA. These modes give different frequency response, so that it may be possible to get an idea of the frequency of a sound by switching between modes. There are two constructors:
You can switch modes with:
Example using DB mode only: The above example gives a graphical display of the way the sound reading varies over a two-second period.
Back to top
Ultrasonic SensorTo create an instance, use the constructor:
The sensor operates in two modes, continuous (default) and ping. When in continuous mode the sensor sends out pings as often as it can and the most recently obtained result is available via a call to
Example:
When in ping mode, a ping is sent only when a call is made to
Program: multiple echoesWrite a program that displays the distances from multiple echoes in a column. The program should make four calls to ping(), and display the four columns of results, then wait for a button press. Exit if the ESCAPE button was pressed. Solution |