Posted on by & filed under Content - Highlights and Reviews, Programming & Development.

codeA guest post by Scott Sullivan, an independent Digital Product Designer. He has a background in technology-based art, visual design, and User Experience design. He can be found on Twitter @scotsullivan.

For this project you’re going to use Processing, a language built on Java to build an Android application that will simply turn on and off a LED every time you touch the screen using the Android IOIO (“yoyo”) board. The IOIO is a microcontroller and has very similar capabilities to an Arduino, except this microcontroller specifically plays nicely with Android devices. In this post, we’ll be using the IOIO board for simple digital output, the most basic IOIO capability. We can also use the IOIO for pulse-width-modulated digital output and analog input.

pioio

If you’re not familiar with using Processing to write Android applications, check out this Android Processing Tutorial by Jer Thorp and the Android Processing Wiki. We’ll specificially be using the PIOIO library for Processing by PinkHatSpike.

Here’s what it will look like (link to Vimeo page): 

And you can access the project on Github here: https://github.com/scottsullivan/pIOIO_blink.

Hardware used

Before we dig in to the code, you’ll need to pick up this hardware. You can get most of it from Sparkfun.

LED wiring

The wiring for this project is extremely simple, only two connections are needed!

  • Connect the shorter lead (negative, cathode) of the LED to ground (GND) on the IOIO board
  • Connect the longer lead (positive, anode) of the LED to the #1 pin on the IOIO board

Step 1: Importing Libraries / Android

First we have to add appropriate IOIO libraries and parts of the Android API – this happens before the setup loop.

Step 2: Adding global variables

Also before the setup loop, we name our LED and declare a boolean to control our light and an integer for our color.

2a. Name the LED variable and specify that it’s a digital output.

2b. Make a boolean to control the light.

2c. Create an integer for our background color.

Step 3: Processing setup

The setup chunk is run once at the beginning of the sketch and is in the void setup() function. Here we start the PIOIO communication and declare the size of our sketch as well as choose the orientation of our sketch.

3a. Instantiate pIOIO.

3b. Set the size of the Android application.

You can specify specific pixel dimensions or have it auto-detect the display width and display height of the device. P3D is the render mode, in case later you need something to be 3D.

3c. Set the orientation of the Android application.

Here it is in portrait mode, but you could also choose orientation(LANDSCAPE) or don’t include this if you don’t want to lock the orientation.

Step 4: Processing draw loop

The draw loop is run ~60 times per second by default, and is in the void draw() function. In the draw loop, we’re only drawing the background color of our screen.

Step 5: The mouse event

Inside the void mousePressed() function we have an if-else statement where if the screen is tapped, it checks if the light is on or off. If it’s on, the light will turn off and the background color of the application will go black. If it’s off, the light will turn on and the background color will turn white.

Step 6: IOIO thread setup

The IOIO functionality resides in a separate paralell thread that is structured similarly to the Processing void setup() and void draw() functions. The IOIO setup is in the void ioioSetup(IOIO ioio) function and is basically Java and only executes if it’s connected to the IOIO. The function declaration is followed by throws ConnectionLostException before the opening curly bracket.

In the IOIO thread setup we link our led1 variable to pin #1 on the board and declare that it is to be used for digital output.

Step 7: IOIO thread loop

The IOIO thread loop mirrors the functionality of the void draw() processing funtion, and is also basically Java and only executes if it’s connected to the IOIO. The function declaration is also followed by throws ConnectionLostException before the opening curly bracket.

7a. The try block checks our light1On boolean.

If it returns true, the light will turn on.

7b. The catch block is an exception handler.

It throws an error if the phone is not connected to the IOIO.

Run on device

Now you should be good to go. Before you run the application be sure to go to the Android menu in the Processing IDE and select “Sketch Permissions” and check off BLUETOOTH, BLUETOOTH_ADMIN, and INTERNET.

Conclusion

And that’s it! Run the sketch and you should now be able to touch anywhere on the screen and the screen color will toggle between being black and white, and your LED should be turning on and off at the same time.

For more information, checkout our books in Safari Books Online that cover IOIO.

Not a subscriber? Sign up for a free trial.

Safari Books Online has the content you need

Making Android Accessories with IOIO helps you create your own electronic devices with the popular IOIO (“yoyo”) board, and control them with your Android phone or tablet. With this concise guide, you’ll get started by building four example projects—after that, the possibilities for making your own fun and creative accessories with Android and IOIO are endless. This book provides the source code and step-by-step instructions you need to build the example projects. All you have to supply is the hardware.
Beginning Android ADK with Arduino shows how the ADK works and how it can be used with a variety of Arduino boards to create a variety of fun projects that showcase the abilities of the ADK.The author walks you through several projects, including making sounds, driving motors, and creating alarm systems, all while explaining how to use the ADK and how standard Arduino boards may differ from Google-branded Arduinos.
Professional Android Sensor Programming shows Android developers how to exploit the rich set of device sensors—locational, physical (temperature, pressure, light, acceleration, etc.), cameras, microphones, and speech recognition—in order to build fully human-interactive Android applications. Whether providing hands-free directions or checking your blood pressure, this book shows how to turn possibility into reality.

Tags: android, Arduino, IOIO, java, LED, PinkHatSpike, Processing, Simple Digital Output,

Comments are closed.