2011年6月17日星期五

Amarino- How to access your Android's compass and accelerator data

What is amarino? a maxican food? Nah!, Amarino mean "Android meets Arduino"

here is the cite from Amarino official site

Normally smartphone events are tightly coupled to your phone device itself. When your cell phone is ringing, your phone speaker plays a ringtone. When you get a new text message, your phone displays it on its screen. Wouldn't it be thrilling to make thoses phone events visible somewhere else, on your wearable, in your living room, on your robot, in your office or where ever you want it to occur? Or would you like to use your smartphone sensors, like the accelerometer, light sensor, compass or your touchscreen to control other devices? 'android meets arduino' is a toolkit, basically consisting of an Android application and an Arduino library which will help you to interface with your phone in a new dimension. You can build your own interfaces almost without any programming experience.


The basic concept for this tutorial is simple, Amarino API in your android phone will access your phone sensor and transmit the sensor data through Bluetooth over the air and received by Bluetooth module attached at Arduino's UART (pin 1 & 2), so the amarino API in Arduino will interprets the data and return the proper value at your end.

It is relatively easy to get this tutorial done.
But first of all, you needs some basic stuff before you start

1) Arduino board
2) Bluetooth module (get it from ebay for 8 USD)
3) Android phone with Bluetooth
4) Amarino library (http://www.amarino-toolkit.net/index.php/download)
5) Arduino IDE
6) LCD display (ebay for 5 USD)



You should follow these steps and get the magic done.

1) go to Amarino site

2) Download and install the Amarino_2_v0_55.apk into your phone

2) Extract MeetAndroid_3.zip into your IDE library path,
the path will looks like this arduino_IDE\libraries\MeetAndroid

3) Copy this code into your Arduino IDE
------------------------------------------------
/*
Receives compass and accelerator sensor events from your phone.
By Willer June 2011
*/
#include
#include

#define MAX_OUT_CHARS 16 //max nbr of characters to be sent on any one serial command
char buffer1[MAX_OUT_CHARS + 1]; //buffer used to format a line (+1 is for trailing 0)
char buffer2[MAX_OUT_CHARS + 1];

//Error capturing function
MeetAndroid meetAndroid(error);
void error(uint8_t flag, uint8_t values){
Serial.print("ERROR: ");
Serial.print(flag);
}

int onboardLed = 13;

//I use LCD to display debug message
extern LiquidCrystal lcd(7, 6, 5, 4, 3, 2);

//buffer to store compasas and accelerator
extern int compass = 0;
extern float accelerator[3] = {0,0,0};

void setup()
{
lcd.begin(16, 2);
lcdDebug("We-Android-Freaks.com","Amarino Demo 01 by IChing");
// use the baud rate your bluetooth module is configured to
// not all baud rates are working well, i.e. ATMEGA168 works best with 57600
Serial.begin(57600);
// register callback functions, which will be called when an associated event occurs.
//naviCore is the function will be call for every incoming event,
//A & B is the events ID that you can get it from your amarino apps in your phone.
meetAndroid.registerFunction(naviCore, 'A');
meetAndroid.registerFunction(naviCore, 'B');

pinMode(onboardLed, OUTPUT);
digitalWrite(onboardLed, HIGH);
delay(2000);

}

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

void naviCore(byte flag, byte numOfValues)
{

if(flag=='A'){

compass = meetAndroid.getInt(); //0-360
sprintf(buffer1,"Compass: %d",int(compass));
lcdDebug(buffer1, buffer2);
}else if(flag=='B'){
meetAndroid.getFloatValues(accelerator);
int mappedX = map(accelerator[0]*10, -50, 50, -1000, 1000);
int mappedY = map(accelerator[1]*10, -50, 50, -1000, 1000);
int mappedZ = map(accelerator[2]*10, -150, 150, -1000, 1000);
sprintf(buffer2,"X:%d,Y:%d,Z:%d",mappedX, mappedY, mappedZ);
lcdDebug(buffer1, buffer2);
}

}

void lcdDebug(String line1, String line2){
lcd.clear();
lcd.setCursor(0,0);
lcd.print(line1);
lcd.setCursor(0,1);
lcd.print(line2);
}
------------------------------------------------

4) Compile the code into your Arduino board, plug the LCD and bluetooth module into correct pin, power it up. be aware that Bluetooth is working on 3.3V

5) Run your Amarino on your Android phone, press the green cross (Add BT device) to search for your Bluetooth module, Add it into your list, do not connect it.

6) Press a little bit longer on your newly added Bluetooth device, an option screen will be pop out and click "Show Events", then Add your sensor event to monitor by pressing "Add Event"

7) Add the Compass Sensor (ID:A) then follow by Accelerometer sensor (ID:B)

8) Hit the back button and go to the Amarino main menu, and connect to your Bluetooth device.

9) done, you should see the LCD screen displaying your sensor data.