UltraSonic sensor in a listener

This is where you talk about the NXJ software itself, installation issues, and programming talk.

Moderators: 99jonathan, roger, imaqine

UltraSonic sensor in a listener

Postby spunky99 » Mon Aug 27, 2007 5:48 am

I have worked with the RCX brick, and to set up a listener, the type and mode need to be specified. However with the NXT, i don't know what type and mode to use for the UltraSonic sensor. With the RCX the sensors and modes were constants, and there are constants with the NXT, but i don't think any of those will work for the UltraSonic sensor. I do understand that the UltraSonic sensor is a I2C sensor, but i don't know what to put in the SensorPort.S1.setTypeAndMode(??,??), or even if it can be done this way... Here is the code that i would like to run:

Code: Select all
import lejos.nxt.*;
public class SonicTest {
   public static void main(String[] args) throws Exception
   {
      // Initialize the sensors
      SensorPort.S1.setTypeAndMode(<TYPE>, <MODE>);

      // Initialize the listener
      SonicListener listener = new SonicListener();
      SensorPort.S1.addSensorPortListener(listener);
      
      Button.ESCAPE.waitForPressAndRelease();
   }
   public static class SonicListener implements SensorPortListener
   {            
      public void stateChanged(SensorPort source, int oldValue, int newValue)
      {
         Sound.playTone(50,10);
         LCD.clear();
         LCD.drawInt(newValue, 0, 0);
         LCD.refresh();
      }
   }   
}
spunky99
New User
 
Posts: 1
Joined: Mon Aug 27, 2007 5:37 am

Postby edalquist » Tue Aug 28, 2007 1:10 am

You want to use the lejos.nxt.UltrasonicSensor class to wrap the sensor port.

This should work:
Code: Select all
private static UltrasonicSensor ultrasonicSensor = new UltrasonicSensor(SensorPort.S1);


As for listeners, I think they only work with non I2C sensors at this point.
edalquist
Novice
 
Posts: 48
Joined: Sun Apr 08, 2007 5:55 pm


Return to NXJ Software

Who is online

Users browsing this forum: Baidu [Spider] and 1 guest

more stuff