Using the accelerometer
Most Android devices have an accelerometer. This chip is used to detect the orientation of your device. It will return different values if you hold your device in portrait, landscape, or upside down. In this recipe, we'll take a look at how we can access this chip, and use those values to move a ball across the screen.
Getting ready
Create a new sketch and save it as accelerometer.pde
. Go to the File | Examples menu, search for the Sensors folder on this panel, and open the accelerometer example. You'll notice that there is a second tab in this sketch, the AccelerometerManager.java
class. Copy this file into your new sketch. We'll need it to make our example work.
How to do it...
We'll start by declaring some variables and assigning values to them in the setup()
function:
AccelerometerManager acc; float ax; float ay; float az; float r; PVector loc; color bgcolor; void setup() { acc = new AccelerometerManager( this ); size( displayWidth, displayHeight ); orientation...