Back

CreateIOIO

The CreateIOIO function creates an object which manages the connection between your Android phone and the IOIO board.

 ioio = app.CreateIOIO( mode );

The mode parameter can be set to either "ADB" or "Bluetooth", the default is "ADB". If you are using Bluetooth then make sure you have paired with the IOIO board first (enter the pin code "4545").

Once it is created you can use the SetOnConnect function to set a callback function which will be called every time an IOIO board is connected to your phone.

In your OnConnect callback you need to call functions on the IOIO object to create objects which can control the resources of your IOIO board. For example you might want two Digital outputs and one Analog input so you would use the CreateDigitalOutput and CreateAnalogInput functions.

You will probably want to call the CheckConnection function shortly after you have created the IOIO object to check if the IOIO board is already connected, in which case your OnConnect callback will be called immediately.

Example - Flash LED every 50ms

function OnStart()
{
  ioio = app.CreateIOIO();
  ioio.SetOnConnect( ioio_OnConnect );
  ioio.CheckConnection();
}

function ioio_OnConnect()
{
  app.ShowPopup( "Connected!" );
  out = ioio.CreateDigitalOutput( 0,true );
  setInterval( "FlashLED()",50 );
  led = true;
}

function FlashLED()
{
  led = !led;
  out.Write( led );
}
  Copy   Copy All    Run   

Example - Read voltage on pin 40

function OnStart()
{
  ioio = app.CreateIOIO();
  ioio.SetOnConnect( ioio_OnConnect );
  ioio.CheckConnection();
}

function ioio_OnConnect()
{
  app.ShowPopup( "Connected!" );
  input = ioio.CreateAnalogInput( 40 );
  setInterval( "ReadVoltage()",3000 );
}

function ReadVoltage() {
  app.ShowPopup( input.GetVoltage(),"Short" );
}
  Copy   Copy All    Run   

Example - Fade LED using PWM

function OnStart()
{
  ioio = app.CreateIOIO();
  ioio.SetOnConnect( ioio_OnConnect );
  ioio.CheckConnection();
}

function ioio_OnConnect()
{
  app.ShowPopup( "Connected!" );
  pwm = ioio.CreatePwmOutput( 0,100 );
  setInterval( "Dimmer()",10 );
  count = 0; }

function Dimmer()
{
  pwm.SetDutyCycle( count/100 );
  if( count++ > 100 ) count = 0;
}
  Copy   Copy All    Run