CRTC Electronics

Click here to edit subtitle

Hello World

The Two Main Files

MainActivity.java 

MainActivity.java    This is where all of our logic code is, (the code that does stuff)

main.xml

main.xml   This is a layout of how all our buttons and information is shown (how things will look)
 

MainActivity.java

The Four Important Methods

OnCreate()

  • onCreate() is called one time when your app is created
  • this is where we make a variable called button_ that links us to the toggle button


public void onCreate(Bundle savedInstanceState) 

{

   super.onCreate(savedInstanceState);

   setContentView(R.layout.main);

   button_ = (ToggleButton) findViewById(R.id.button); 

}

 

looper

  • looper contains setup() and loop()
  • initialize your variables

class Looper extends BaseIOIOLooper 

{

   private DigitalOutput led_;

...

setup()

  • setup() is called every time a connection with IOIO has been established
  • variable led_ is an instance of openDigitalOutput
  • openDigitalOutput's first argument is the pin number (0 = on-board LED)
  • openDigitalOutput's second argument is the initial state of the pin 

protected void setup() throws ConnectionLostException 

{

   showVersions(ioio_, "IOIO connected!");

   led_ = ioio_.openDigitalOutput(0, true);

   enableUi(true);

}



loop()

  • loop() is called repetitively while the IOIO is connected
  • here we are setting our led_ to the inverse of the button (on-board LED need a negative to turn on)

public void loop() throws ConnectionLostException, InterruptedException 

{

   led_.write(!button_.isChecked());

   Thread.sleep(100);

}

 

Challenge 

Turn a pin on and off

Use the toggle button to turn on and off a pin by making a second instance of openDigitalOutput and using a different pin.