Back

Sensors

Create Sensor objects using the CreateSensor function of the app object:

 sns = app.CreateSensor( type, options );

Types available are: "Accelerometer", "MagneticField", "Orientation", "Light" and "Proximity".

You can use the SetOnChange function of the Sensor to set the name of a function you want to be called when a the changes occur.

Change the rate that a sensor checks for changes by adding one the options "Fastest", "Fast", "Medium" or "Slow". "Slow" is the default.

Example - Accelerometer

function OnStart()
{
  lay = app.CreateLayout( "Linear", "VCenter,FillXY" );

  txt = app.CreateText( "", 0.8, 0.3, "Multiline" );
  lay.AddChild( txt );
  app.AddLayout( lay );

  sns = app.CreateSensor( "Accelerometer" );
  sns.SetOnChange( sns_OnChange );
  sns.Start();

}

function sns_OnChange( x, y, z, time )
{
  txt.SetText( "x="+x + "\n y="+y + "\n z="+z );
}
  Copy   Copy All    Run   

Example - Orientation

function OnStart()
{
  lay = app.CreateLayout( "Linear", "VCenter,FillXY" );

  txt = app.CreateText( "", 0.8, 0.3, "Multiline" );
  lay.AddChild( txt );
  app.AddLayout( lay );

  sns = app.CreateSensor( "Orientation" );
  sns.SetOnChange( sns_OnChange );
  sns.Start();

}

function sns_OnChange( azimuth, pitch, roll, time )
{
  var msg = " azimuth = " + azimuth.toFixed(1);
  msg += "\n pitch = " + pitch.toFixed(1);
  msg += "\n roll = " + roll.toFixed(1);
  txt.SetText( msg );
}
  Copy   Copy All    Run   

Example - Light

function OnStart()
{
  lay = app.CreateLayout( "Linear", "VCenter,FillXY" );

  txt = app.CreateText( "", 0.8, 0.3 );
  lay.AddChild( txt );
  app.AddLayout( lay );

  sns = app.CreateSensor( "Light" );
  sns.SetOnChange( sns_OnChange );
  sns.Start();

}

function sns_OnChange( lux )
{
  txt.SetText( "level = " + lux + " lux" );
}
  Copy   Copy All    Run   
number
number - fraction of object size
string
string - comma separated