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 we’re going to use Processing, which is a language built on Java used to create an Android application that will be connected to a passive infrared motion sensor via the IOIO board. Every time the sensor detects movement, it will display “Moving” on the screen of the Android device.

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, you’ll be using the IOIO board for simple digital output, which is the most basic IOIO capability.

Here’s what this project will look like:

Hardware used

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

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

  • Connect the GND pin to ground (GND) on the IOIO board.
  • Connect the VCC pin to 3.3v on the IOIO board.
  • Connect the OUT pin to pin #1 on the IOIO board.

Step 1: Importing Libraries / Android

First you have to add appropriate IOIO libraries and parts of the Android API. Before you can import the PIOIO library to your Processing sketch, you have to download the library from here and put it in your libraries folder. After the library is in the libraries folder you bring it in to your project, this happens before the setup loop.

Step 2: Adding global variables

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

2a. Name the sensor input variable.

And specify that it’s a digital input.

2b. Make a boolean to show if there’s movement detected by the sensor.

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 you start the PIOIO communication and declare the size of your sketch as well as choose the orientation of your 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’s portrait but you could also choose orientation(LANDSCAPE) or not include this if you don’t want to lock the orientation.

3d. Set the text that will be displayed on the screen.

Make it to be center-aligned and centered vertically.

3e. Set the size of the text to be 36 pixels.

Step 4: Processing draw loop

The draw loop is run ~60 times per second default and is in the void draw() function.

4a. Draw the background color of the sketch.

4b. If-else statement.

Where if movement is detected, the background color of the application turns white, the text color turns black and the text displays “Moving” in the center of the screen. If there is no movement, the background color of the application turns black, the text color turns white and the text displays “Not Moving”.

Step 5: IOIO thread setup

The IOIO functionality resides in a separate parallel 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 you link the PIRmove variable to pin #1 on the board and declare that it is to be used for digital input.

Step 6: IOIO thread loop

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

6a. The try block checks our pin.

To see if it’s high or low. If it’s high, the isMoving boolean returns true.

6b. The catch block is an exception handler.

And throws an error if there is an interruption.

Run on the 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.

final

Conclusion

And that’s it! Run the sketch and should see the application reacting to movement detected from the motion sensor!

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, IOIO, Motion sensor, Sensor,

Comments are closed.