Bluetooth Amarino

The project Amarino provides the tools to use an ardiod device to control an arduino via Bluetooth. What they are not so good at, is to provide documentation for a newbee to use the tool.

Installing Amarino

To install amarino, you need more than the android, arduino and amarino ...Here are all the steps you need:

  • First you have to switch the android device into development mode (Settings->Applications->Development->USB debuggin switched to on).
  • Next you have to download the Android ADK to your computer and install it. Locate the tool adb, you will need it later
  • download the amarino application from the download page
  • install the app using "adb install Amarino_2.apk"
  • download the default events called "Amarino Plug-in Bundle" and install it using adb
  • download the "MeetAndroid" library and extract it into the subdirectory "libraries" where your android sketches reside (create if not present)

After a restart of the arduino IDE, you should see the library in the example list.

Preparing the arduino

The bluetooth shield I use interferes with the arduino USB communication. So to download the sketch, disconnect the bluetooth shield from the arduino. I downloaded the following sketch:

/*
  Receives Orientation Events from your phone.
 */
#include <Servo.h>  
#include <MeetAndroid.h>

Servo myservo;
MeetAndroid meetAndroid;
int servoPIN = 9;
int LastPos=0;

void setup()  
{
  // use the baud rate your bluetooth module is configured to
  // the ITead BT shield by default uses 9600 baud
  Serial.begin(9600); //57600

  // register callback functions, which will be called when an associated event occurs.
  // - the first parameter is the name of your function (see below)
  // - match the second parameter ('A', 'B', 'a', etc...)
  //   with the flag on your Android application
  meetAndroid.registerFunction(SetServo, 'A');  

  // move the servoce once from left to right and back
  myservo.attach(servoPIN);
  for(int pos = 0; pos < 180; pos += 1)  
  {                                  
    myservo.write(pos);               
    delay(15);                       
  }
  for(int pos = 180; pos>=1; pos-=1)      
  {                                
    myservo.write(pos);              
    delay(15);                        
  }
}

void loop()
{
  // you need to keep this in your loop() to receive events
  meetAndroid.receive();
}

void SetServo(byte flag, byte numOfValues)
{
  // create an array where all event values should be stored
  // the number of values attached to this event is given by
  // a parameter(numOfValues)
  float data[numOfValues];
  int servoPos;
  int deltaPos;

  // call the library function to fill the array with values
  meetAndroid.getFloatValues(data);
  // map the +/- 90 degrees from the android to 0-180
  servoPos = map(data[2], -90,90,0,180);
  deltaPos = abs(servoPos - LastPos);
  // ignore jitter on the device
  if (deltaPos>3)
  {
    myservo.write(servoPos);
    LastPos = servoPos;
  }
}

The sketch is based on the servo example and merged with the amarino example to readout an array. Upload the sketch to the android and insert the bluetooth shield.

Preparing the Amarino App

Launch the amarino app and add your bluetooth device. Then add the event "Orientation". Make sure, the event is the first in the list of events. This is a poorly documented feature of amarino: The events get a unique identifyer based on the order they are added to the device. The first event gets "A", the second "B" and so on. In the skecth, you register the function that is called according to the event (above: meetAndroid.registerFunction(SetServo, 'A'); ). If the event order changes, you have to match that change in the sketch!

The event the needs to be started by pressing on the event. When the menu appreares, select "force enable". In theory, connecting and disconnectin the bluetooth device should start and stop the events - however, it does not work that way. You will have to start and stop the events manually.

I then connected a servo to the shield and watched it moving according to my movement of the android phone

Enjoy!