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.

If you haven’t read it already, take a look at the Processing & IOIO Simple Digital Output post, where I used Processing to build an Android application that turns an LED on and off every time you touch the screen using the Android IOIO (“yoyo”) board. For this project we’re going to use Processing to read values from an accelerometer, and to then use those variables to control the color of a screen, and that color will be mirrored by an RGB LED via the IOIO board.

The IOIO is a microcontroller that has 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 pulse-width-modulated digital output, which is basically a way of faking analog output. We can also use the IOIO for standard 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_accelRGB.

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 a little complicated but not too hard!

  • Connect first lead (#1) of the RGB LED to a 150Ω resistor and then to pin 12 on the IOIO board
  • Connect the longest lead (#2) (negative, cathode) of the LED to ground (GND) on the IOIO board
  • Connect third lead (#3) of the RGB LED to a 100Ω resistor and then to pin 13 on the IOIO board
  • Connect fourth lead (#4) of the RGB LED to a 100Ω resistor and then to pin 14 on the IOIO board

LED

Step 1: Importing Libraries / Android

First we have to add the appropriate IOIO libraries, the Ketai library and parts of the Android API. Before you can import the PIOIO and Ketai libraries to your Processing sketch, you have to download the libraries from PinkHatSpike here and Google here, and put them in your libraries folder. After the libraries are in the libraries folder you can bring them in to your project. This happens before the setup loop.

Step 2: Adding global variables

Also before the setup loop, we name our LED leads, create variables for the accelerometer and the colors, and set up the Ketai sensor library.

2a. Instantiate the Ketai sensor to read the accelerometer.

2b. Create float variables for the accelerometer axes and their corresponding colors.

2c. Name the three LED variables.

Be sure to specify that it’s a pulse-width-modulated (PWM) digital output.

Step 3: Processing setup

The setup chunk is ran once at the beginning of the sketch, and it is in the void setup() funtion. Here we start the PIOIO communication and declare the size of the sketch, as well as choose the orientation of the sketch.

3a. 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.

3b. Instantiate Ketai sensor.

3c. Instantiate pIOIO.

3c. Set the orientation of the Android application.

Here the orientation is 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.

Set it to be left-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 at ~60 times per second default and is in the void draw() function. In the draw loop, we’re setting what draws to the screen and adjusting the color variables.

4a. Update the color variables based upon the accelerometer input.

The accelerometer on my phone has a range of about -11 to +11. Let’s re-map that input to a range between zero and one using Processing’s map() functionality.

4b. Change the background color of the Android application.

You do this based upon the color variables. The color variables are multiplied by 255 for 24-bit RGB colors.

4c. Draw text to the screen.

Writing the Red, Green, and Blue values (using round() to keep them whole numbers. And displaying the corresponding X, Y, and Z accelerometer inputs using nfp() to dynamically adjust the + or - signifier.

Step 5: Reading the accelerometer

Now let’s write a function to read the accelerometer data from the Android device. This is a standard Ketai function, void onAccelerometerEvent(), with the variables float x, float y, and float z.

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, link the ledR, ledG and ledB variables to pins 12 (R), 14 (G), and 13 (B) on the board. Be sure you declare that it is to be used for pulse-width-modulated (PWM) digital output. The 1000 after the pin number sets the number of cycles per second.

WARNING: Most RGB LEDs are set up so that the Blue anode pin is on the outside, but mine happened to have the Green anode pin on the outside. Check the documentation for your specific RGB LED to be sure you’re connecting the right pins.

Step 7: IOIO thread loop

The IOIO thread loop mirrors the functionality of the void draw() processing function. It is also 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.

7a. The try block is empty.

7b. The catch block is an exception handler.

It 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 you should now be able to move your device around and see the color and accelerometer readouts as well as the screen and RGB LED changing color!

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, Bluetooth, Color, faking analog output, IOIO, LED, pulse-width-modulated digital output, sparkfun,

Comments are closed.