This is a four part tutorial which will take you through step-by-step on how to create Android apps on your Mobile device that will allow you to communicate with your Arduino over Bluetooth. This tutorial is based upon the Windows environment and an Android device like the Samsung Galaxy S2 Phone. I will take you through setting up your computer and phone, and will move through in stages so that you understand what each part of the bluetooth code is actually doing. Obviously you will need to ensure that you have a Bluetooth Shield on your Arduino to be able to walk through this tutorial with me. If you are not interested in the step-by-step instructions, you can jump straight to the end (Part 4) which will have the complete Arduino and Android/Processing code that was used in the following video:
Setting up Processing for Android applications: For the latest and most up to date version, please follow the instructions on this website: http://wiki.processing.org/w/Android
Step One: Download the Android SDK from this website: http://developer.android.com/sdk/index.html Android SDK Download: Make sure to select the "Use and Existing IDE" link, as per the picture below.
When you select the "Use an Existing IDE" link, it will then show you the appropriate download to use. This is what it should look like. Select the "Download the SDK Tools for Windows" link. Once you have downloaded the Android SDK, go ahead and install it as per the instructions below. These instruction can also be found here.
Installing the necessary packages in the Android SDK Manager program Make the following 3 selections:
Tools: Android SDK Platform-tools
API 10: SDK Platform
Extras: Google USB Driver
Then select the button on the bottom-right to install your selections.
Here is a picture of the Android SDK Manager selections:
While you may decide to download other packages, you MUST download API 10: SDK Platform . Do not leave this one out !!
Step Two: Processing Download Download the latest Processing IDE(version 2.0 Beta 8) from this website: http://processing.org/download/
I am using Windows 7, and have chosen to download the Windows 32 bit version as shown below.
Load Processing, and switch to Android mode, as per the image below.
You should now have an empty sketch window which looks something like this.
Turn on USB debugging on your Android Phone: To find out what Android Version you are on, have a look at Settings > About Phone : look for heading "Android Version".
My Android version is 2.3.4 on my Samsung Galaxy S2.
To Enable USB Debugging:
Settings > Applications > Development >Select (or Enable) USB debugging
For those of you who have a different Android version, have a look below:
Downloading the USB driver for your Android Phone(Windows Users) If you are developing on Windows and would like to connect an Android-powered device to test your applications, then you need to install the appropriate USB driver. Have a look at this site for more information on how to download the USB driver for your phone: http://developer.android.com/tools/extras/oem-usb.html
You will need the model number of your phone: On the Samsung Galaxy S2, you can go into Settings > About Phone => Model number. Otherwise, it is located behind the battery.
My Phone's Model Number is: GT-I9100
See the image below for the link to press if you have a non-US phone.
We will now test our our current setup and make sure that we can run a simple Processing Sketch on the Phone. Bluetooth functionality will be tested later on, so all we need for this step, is our computer, our Android phone, and a USB cable. While it is possible to run this sketch without an Android phone (by using the emulator), I personally do not have the patience to wait an eternity while the emulator boots up... (yes, it takes an eternity)... In this tutorial, we are going to test it on the device (phone). This sketch has an orange background and a black circle which you can move around the screen with your finger (that's it) - I did say it was going to be a simple sketch.
Copy and paste the following Android-Processing sketch into the IDE, and then press the (Run on Device) button, which is the triangle button or press Ctrl-R.
Android/Processing Sketch 1: Circle Dragger
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*Circle Dragger: Simple Android-Processing Sketch written by ScottC on 13/03/2013. Visit: http://arduinobasics.blogspot.com/ */ int circleWidth = 150; void setup(){ orientation(LANDSCAPE); } void draw(){ background(255,100,0); fill(0); ellipse(mouseX,mouseY,circleWidth,circleWidth); }
You should see an orange screen appear on your phone. Move your finger across the screen and watch as the black circle follows your finger. No, not rocket science, but hopefully everything worked as planned. If you want to change the colour of the background or circle, this is a good site: http://www.colorpicker.com/
Step Five: Bluetooth testing:
We are now going to walk through Bluetooth connectivity. While we could just use a library to do all the heavy lifting for us, I decided to explore Bluetooth functionality from scratch. This will hopefully provide greater returns in the long run. Ok, lets create a new Android/Processing Sketch which changes its behaviour depending on whether Bluetooth is enabled or disabled when the sketch is run. We will display a red screen when Bluetooth is switched off, and green when Bluetooth is switched on.
To enable Bluetooth on my Samsung Galaxy SII phone:
Settings >Wireless and Network > Bluetooth Settings > Bluetooth (Turn on Bluetooth) - check the box
To disable Bluetooth on my Samsung Galaxy SII phone:
Settings >Wireless and Network > Bluetooth Settings > Bluetooth - Uncheck the box
In the processing/android IDE, you need to make sure that you update the AndroidManifest.xml file to grant specific permissions. You can either edit the file manually in the sketch folder, however, it is much easier and safer to do the following. In the processing/android IDE, select:
Android > Sketch permissions (as per the picture below)
Make sure that BLUETOOTH and BLUETOOTH_ADMIN are selected (as per the picture below). Then press the OK button.
Then copy and paste the following sketch into the processing/android IDE:
Android/Processing Sketch 2: BluetoothChecker1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*BluetoothChecker1: Written by ScottC on 17 March 2013 This will show a red screen if Bluetooth is off, and a green screen when Bluetooth is switched on */
When you run the BluetoothChecker1 sketch on the device, you will either see a red screen or a green screen depending on whether you had Bluetooth enabled or disabled at the time. Ok, pretty boring, but it is a start. What if we wanted to ask the USER if they would like to enable Bluetooth at the beginning? We could then change the appearance of the screen depending on their selected answer. Before we add this functionality, I would recommend that you read about the following concepts introduced in the next sketch.
startActivityForResult : used to create a new Activity. We create a new Activity to ask the user for permission to enable Bluetooth.
onActivityResult: called when the Activity exits. We use this to find out whether the user granted permission to turn on Bluetooth or not.
While it is actually possible to turn bluetooth on without asking for permission, I thought I would retain my manners for the following sketch: Android/Processing Sketch 3: BluetoothChecker2
import android.bluetooth.BluetoothAdapter; import android.content.Intent; int BACKGND=0; //Set the background to BLUE //Get the default Bluetooth adapter BluetoothAdapter bluetooth = BluetoothAdapter.getDefaultAdapter();
/*The startActivityForResult() launches an Activity which is used to request the user to turn Bluetooth on. The following onActivityResult() method is called when the Activity exits. */ @Overrideprotectedvoid onActivityResult(int requestCode, int resultCode, Intent data){ if(requestCode==0){ if(resultCode == RESULT_OK){ BACKGND=2; //Set the background to GREEN } else { BACKGND=1; //Set the background to RED } } } void setup(){ orientation(LANDSCAPE);
/*IF Bluetooth is NOT enabled, then ask user permission to enable it */ if (!bluetooth.isEnabled()) { Intent requestBluetooth = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); startActivityForResult(requestBluetooth, 0); } } void draw(){ if(BACKGND==0){ background(10,10,255); //Set background to BLUE } elseif(BACKGND==1) { background(255,10,10); //Set background to RED } else { background(10,255,10); //Set background to GREEN } }
I tried my best to explain the code via the comments within. I hope it made sense.
Have you ever wondered if there was a way to store and retrieve data from a USB stick with an Arduino UNO? Most people choose SD cards to store their project data, but you may be surprised there IS a way! IC Station have a nice little module which allows you store and retrieve your Arduino (or other MCU) project data to a USB stick. I am not too sure why USB storage is not widely used in Arduino projects? These modules are not expensive, they have been around for quite a while, and are relatively simple to use. You do not need any libraries to get them to work, however, I must say that documentation for this module is not that easy to find. This site and this document proved to be very useful in my endevour to get this module working, and I hope my tutorial below will help you get started and bridge some of the information gaps. The...
Guest Post Disclaimer This is a guest post by the EasyEDA team. I would like to thank EasyEDA for providing this tutorial for everyone to enjoy. All information within this post was provided by EasyEDA. Description None of us could deny the fact that we would love with to play with LED’s and lighting stuff. I love to play with LED’s and create attractive lighting effects. This project was a result of such an attempt where I created a stunning RGB light effect using the popular development platform Arduino Nano. Let’s see the circuit, code and instruction on building this project: Parts Required: An EasyEDA account (free) Arduino Nano (or compatible board) LED strip Dupont wire connectors Custom control board Image source: EasyEDA Ardui...
FIVE MINUTE TUTORIAL Project Description: Sending Hex values to an Arduino UNO This simple tutorial will show you how to send Hexadecimal values from a computer to an Arduino Uno. The "Processing" programming language will be used to send the HEX values from the computer when a mouse button is pressed. The Arduino will use these values to adjust the brightness of an LED. Learning Objectives To Send Hexadecimal (Hex) values from a computer to the Arduino Trigger an action based on the press of a mouse button Learn to create a simple Computer to Arduino interface Use Arduino's PWM capabilities to adjust brightness of an LED Learn to use Arduino's analogWrite() function Create a simple LED circuit...
Comments
Post a Comment