Concept proof of reading hTC Desire Android phone Accelerometer data into Arduino Duemilanove (Android version: 2.2 - code should be working on any 2+ Android phone).
This is a demo of how to control Arduino-powered robot using HTC Desire Android phone. HTc Desire has Android 2.2 Froyo, and the Amarino 2.0 is installed to capture Orientation sensor data and send them to the robot via bluetooth. The robot is using a Serial Bluetooth Module to receive the phone signals.

NOTE: YOUMUST HAVE INSTALLED AMARINO 2.0 SOFTWARE WITH ALL NECESSARY PLUG-INS IN THE FIRST PLACE! Look into link below for instructions on how to do this.

http://www.amarino-toolkit.net/

Also see below for me info.

http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1276034564

You can see my demo video clip here:

http://youtu.be/1FJJsXIi-wQ

Source code is below. Note that robot steering is also potentially coded and included in the code, though it is disabled in at the moment because needs bug fixing.

Android 2.2 hTC Desire running Amarino 2.0 Controls Arduino Duemilanove robot (demo)'

/*
  written by: Shahin Moghimi
  E-mail: shahinjet3@yahoo.com
  15 Oct 2010
  This program controls a ground robot forward and backward motions using hTC Desire phone running Android 2.2 (UK Orange locked version)
  Amarino 2.0 installed
  Phone should be held in landscape
*/

#include <MeetAndroid.h>

#define IDLE 0
#define RINGING 1
#define OFFHOOK 2

MeetAndroid meetAndroid;



int EN1 = 6; 
int EN2 = 5;  //Roboduino Motor shield uses Pin 9 
int IN1 = 7; 
int IN2 = 4; //Latest version use pin 4 instead of pin 8 

void Motor1(int pwm, boolean reverse) 
{ 
analogWrite(EN1,pwm); //set pwm control, 0 for stop, and 255 for maximum speed 
if(reverse) 
{ 
digitalWrite(IN1,HIGH);
} 
else 
{ 
digitalWrite(IN1,LOW); 
} 
} 


void Motor2(int pwm, boolean reverse) 
{ 
analogWrite(EN2,pwm); 
if(reverse) 
{
  digitalWrite(IN2,HIGH); 
} 
else 
{ 
digitalWrite(IN2,LOW); 
} 
} 

void Forward(int speed)
{
  Motor1(speed,true); //You can change the speed, such as Motor(50,true) 
  Motor2(speed,true); 
}

void Backward(int speed)
{
  Motor1(speed,false); //You can change the speed, such as Motor(50,true) 
  Motor2(speed,false); 
}

void Right(int speed)
{
  Motor1(speed,false); 
  Motor2(speed,true); 
}

void Left(int speed)
{
  Motor1(speed,true); 
  Motor2(speed,false); 
}

void Stop()
{
  Motor1(0,false);
  Motor2(0,false);
}







void setup()  
{
  Serial.begin(57600); 

  meetAndroid.registerFunction(phoneorient, 'A');
    int input=0;
    int i; 
  for(i=5;i<=8;i++)     //For Arduino Motor Shield 
  pinMode(i, OUTPUT);    //set pin 4,5,6,7 to output mode
}

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

void phoneorient(byte flag, byte numOfValues)
{
  //Phone Orientation Controller
  int values[]={0,0,0};
  meetAndroid.getIntValues(values);

  // You must hold phone in LANDSCAPE for following orientation.
  int Steer=values[1];   //Steering   , Roll
  int Delta=values[2];   //Throttle  ,  Pitch
  int Heading=values[3];    //Heading  , Yaw

  if (Steer>=90) {
    Steer=90;
  }
  else if (Steer<=-90) {
    Steer = -90;
  }

  if (Delta>=0) {   //Forward

      Motor1(min(150,Delta+Steer),true);
      Motor2(min(150,Delta-Steer),true);

  }
  else if (Delta <0) {

    //Motor1(min(150,-Delta+Steer),false);
    //Motor2(min(150,-Delta-Steer),false);
    Motor1(60,false);
    Motor2(60,false);

  }


  /*
  if (0<=values[1]) {
   // float rate = min(values[2]*3.55+25,254);      
    //Forward(rate);
      Left(values[1]);
      delay(10);

   // Serial.println(rate);
  }
  else if (0>values[1]) {
    //float rate = min(-values[2]*3.55+25,254);
    //Backward(rate);
    Right(-values[1]);
    delay(10);

   // Serial.println(rate);
   }  
  else if (values[1] ==0) {
    Stop();
  }

  */

  Serial.println(values[1]);  //Throttle  , Pitch
  Serial.println(values[2]);  //Steering, Roll
  Serial.println(values[3]);  // Heading, degree
}


Share