Table of Contents

ReadDistanceSensor

The ReadDistanceSensor function reads the distance measured by the ultrasonic sensor in centimeters.

The input parameter should be an NXT input port number between 1 and 4 (which the ultrasonic sensor is plugged into).

dist = nxt.ReadDistanceSensor( input );

Example - Read distance sensor on port 4

function OnStart()
{
  nxt = app.CreateNxt();
  app.SetMenu( "Connect,Distance" );
}
 
function OnMenu( name )
{
  if( name=="Connect" )
    nxt.ShowDevices();
 
  else if( name=="Distance" )
  {
    dist = nxt.ReadDistanceSensor( 4 );
    app.ShowPopup( dist + " cm" );
  }
}