Go Down

Topic: Acceleration sensor from the smartphone is used to drive the robot forward backw (Read 2034 times) previous topic - next topic

valim

Acceleration sensor from the smartphone is used to drive the robot forward backwords left and right

http://youtu.be/1uLr0o4bpm4

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

   1.1 http://code.google.com/p/amarino/downloads/detail?name=Amarino_2_v0_55.apk&can=2&q=

   1.2 http://code.google.com/p/amarino/downloads/detail?name=AmarinoPluginBundle.apk&can=2&q=


Code: [Select]
//Acceleration sensor from the smartphone is used to drive the robot forward backwords left and right

//inspired from http://www.arduino.cc/playground/Code/HTCDesire
//arduino + amarino + HTC Wildfire

#include <MeetAndroid.h>

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

MeetAndroid meetAndroid;



int EN1 = 6;
int EN2 = 5;  //
int IN1 = 7;
int IN2 = 4; //

 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(115200);

   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);

 int Steer=values[1];   //
 int Delta=values[2];   //
 int Heading=values[3];    //

 if (Steer >=5)

   {   //left

     Motor1(220,true); // M1 forward when accelerometer is set to 0 -10...0 0
     Motor2(220,false); // M2 forward when accelerometer is set to 0 10 0
       
 }

 else if (Steer <=-5)
 { //right

   Motor1(220,false);
   Motor2(220,true);

 }

  else if (Delta >=5) { //forward
   
   Motor1(220,true);
   Motor2(220,true);

 }
    else if (Delta <=-5) { //back

   Motor1(220,false);
   Motor2(220,false);

 }

 else
 
 {
   Motor1(0,false);
   Motor2(0,false);

 }

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

PeterH

I only provide help via the forum - please do not contact me for private consultancy.



ygobi

I just need to know how you connected the bluetooth module to the arduino and where to connected.. I'm a total noob when it comes to this but I really want to make this project.

Go Up