Arduino ADK Board: Blink an LED With Your Phone, Minimum Code Required
I recently bought an Arduino ADK board, after downloading the sample
code I was overwhelmed with the amount of C and Java code I saw.
Furthermore the sample code requires that you have all these sensors
that are sold separately on top of the already expensive board, and this
is supposed to be a beginners board?
But enough with the rant, I’ve managed to trimmed the C and JAVA down to what is only necessary to blink the built in LED in the Arduino, the full code and explanation is here, please spread it around.
The ToggleButton xml tag has several properties including dimensions (width and height), position, text size and the ID and onClick attributes. The ID attribute is used to identify inputs (buttons, check boxes etc) in our Java code, the onClick attribute is the function which will executed upon a click.
In the code above, our button’s ID is toggleButtonLED and the function to execute when it’s clicked is blinkLED.
Be sure to change the minSdkVersion to the Android SDK version your project is using.
Whenever you connect your board the Android device (phone or table) will need to know which app the board uses, we do that with this file. You may change the values in the attributes but they will have to same as in the Arduino file.
To make our button be recognized by Java we need to store it in an object. So we declared our object as a ToggleButton object:
What we are doing here is we are making a byte array which will hold the value 0 if the button is checked and the value 1 if it’s not checked, read the comments in the code. The byte is sent to the Arduino ADK board by the write function.
We’ll continue in the same file on the next section.
We only need ONE java file, not 100′s like the "demo" version and here it is.
But enough with the rant, I’ve managed to trimmed the C and JAVA down to what is only necessary to blink the built in LED in the Arduino, the full code and explanation is here, please spread it around.
The App’s Interface
The Android app consists of a toggle button which we’ll use to toggle the state, on or off, of the LED. In Android user interfaces are called layouts, so let’s make a layout with a toggle button.The ToggleButton xml tag has several properties including dimensions (width and height), position, text size and the ID and onClick attributes. The ID attribute is used to identify inputs (buttons, check boxes etc) in our Java code, the onClick attribute is the function which will executed upon a click.
In the code above, our button’s ID is toggleButtonLED and the function to execute when it’s clicked is blinkLED.
Asking For Permission
Whenever you download an app, if you don’t just click OK on everything without reading, you’ll notice that it lists some permissions that it requires from the device. All these permissions are listed in the Android manifest file ("manifest"? LOL sounds like a peace treaty doesn’t it?), we need to list our USB permissions in this file.Be sure to change the minSdkVersion to the Android SDK version your project is using.
How To Route Your USB Device To The Correct Application
Whenever you connect your board the Android device (phone or table) will need to know which app the board uses, we do that with this file. You may change the values in the attributes but they will have to same as in the Arduino file.
How To Handle The Clicks, Send Data To The Board
The following code is part of the ArduinoBlinkLED.java file, don’t worry about where it goes for now, I will tell you that later, for now just understand the following explanations.To make our button be recognized by Java we need to store it in an object. So we declared our object as a ToggleButton object:
Then we store our button in the buttonLED object:
- private ToggleButton buttonLED;
We also need to load the layout into our app.
- // toggleButtonLED is the name we used in the layout/main.xml code above.
- buttonLED = (ToggleButton) findViewById(R.id.toggleButtonLED);
To handle the button clicks we’ll need to make the function blinkLED, just as we promised in the layout/main.xml code above. This is the function’s code:
- // main comes from our interface's file name: main.xml
- setContentView(R.layout.main);
What we are doing here is we are making a byte array which will hold the value 0 if the button is checked and the value 1 if it’s not checked, read the comments in the code. The byte is sent to the Arduino ADK board by the write function.
We’ll continue in the same file on the next section.
Android Arduino Communication
The code that handles USB communication is standard code you’ll need for all your apps, you can either not worry about it or simply check out each function and by the variable names am confident you’ll understand what it’s doing, also in the code to handle clicks is included here. Am sorry I can’t explain each piecebut as you’ll see it could take a while, but the important part is what I explained above.We only need ONE java file, not 100′s like the "demo" version and here it is.
Thanks It was a great HelpfulCode
ReplyDeleteLove it! Thanks!
ReplyDeleteBut to make it work today:
The imports for USB must be changed from com.android.future.usb.... to android.hardware.usb....
In line 128 and 139 change:
UsbAccessory accessory = UsbManager.getAccessory(intent);
to:
UsbAccessory accessory =(UsbAccessory)intent.getParcelableExtra(UsbManager.EXTRA_ACCESSORY);
Line 152 must go from:
mUsbManager = UsbManager.getInstance(this);
to:
mUsbManager = (UsbManager) getSystemService(Context.USB_SERVICE);
Now I just have to wait for my USB host board to come in so that I can play with some stuff....
http://emt-scientist.blogspot.nl/2011/05/adk-compiled-for-android-31.html
Or atleast I had to do that in order to get everything to work.
Thanks again though!
XOXO
Do you have this code on github?
ReplyDelete