Android Robot Control using Bluetooth and Accelerometer

Last semester I just finished a  part of my final year project. The main objective was to control robot using Bluetooth. It was very easy and interesting task. There are some great tutorial about Bluetooth but its difficult for someone who really don’t know that how all android stuff works especially Electrical Engineers. I got help from different sources to complete this project. So lets talk about this Android Robot Control using Bluetooth and Accelerometer part. I will also share Android Controlled Robot using Bluetooth and Voice later.
There is too much code. I am unable explain all but I will help you via comments and feedback and will try to Improve and cleanup if I got time and your response.

This is the only code where you have to set your logic. You can find at the end of file ‘BurraqBlutooth.java’

public void onSensorChanged(SensorEvent event) {
		if (BurraqBlutooth.chk == 1) {
			if (event.sensor.getType() != Sensor.TYPE_ACCELEROMETER)
				return;

			float X = event.values[1];
			float Z = event.values[2];

			// Reverse
			if (Z < -2) {
				BurraqBlutooth.sendMessage("M");
			}
			// Stop Robot
			else if (Z > -1 && Z <= 0) {
				BurraqBlutooth.sendMessage("M");
			}
			// Normal Speed
			else if (Z > 0 && Z < 4) {
				BurraqBlutooth.sendMessage("N");
				if (X > -2 && X < 2) {
					// straight normal
					BurraqBlutooth.sendMessage("A");

				}
				// Right
				else if (X >= 2 && X < 4) {
					BurraqBlutooth.sendMessage("B");
				}
				// Right ++
				else if (X >= 4 && X < 7) {
					BurraqBlutooth.sendMessage("C");
				}
				// Right ++ ++
				else if (X >= 7 && X < 10) {
					BurraqBlutooth.sendMessage("X");
				}

				// LEFT
				else if (X < -2 && X > -4) {
					BurraqBlutooth.sendMessage("D");
				}
				// LEFT ++
				else if (X <= -4 && X > -7) {
					BurraqBlutooth.sendMessage("E");
				}
				// LEFT ++ ++
				else if (X <= -7 && X > -10) {
					BurraqBlutooth.sendMessage("Y");
				}
			}

			// FAST Speed
			else if (Z > 4) {
				BurraqBlutooth.sendMessage("F");
				if (X > -2 && X < 2) {
					// straight normal
					BurraqBlutooth.sendMessage("G");

				}
				// Right
				else if (X > 2 && X < 5) {
					BurraqBlutooth.sendMessage("H");
				}
				// Right ++
				else if (X > 5 && X < 10) {
					BurraqBlutooth.sendMessage("I");
				}

				// Right ++ ++
				else if (X >= 7 && X < 10) {
					BurraqBlutooth.sendMessage("Z");
				}

				// LEFT
				else if (X < -2 && X > -5) {
					BurraqBlutooth.sendMessage("J");
				}
				// Left ++
				else if (X < -6 && X > -10) {
					BurraqBlutooth.sendMessage("K");
				}

				// LEFT ++ ++
				else if (X <= -7 && X > -10) {
					BurraqBlutooth.sendMessage("L");
				}
			}

		} // if chk end
	}

 

We are sending a character to micro controller using sendMessage function.

BurraqBlutooth.sendMessage("Z")

A the receiving end at controller we have set our logic how motor should behave if it get character ‘Z’. I hope you know how to code controller. You may modify this code according to your needs.

There are still some bugs and unwanted code because I had only one week to complete this part of my big bull project. There are some exception handling error but works fine If Bluetooth is already enable then you will not get any error.

Demo  Android Robot Control using Bluetooth and Accelerometer

Download Android Robot Control using Bluetooth and Accelerometer Files

Let's Discuss

  • haris

    whats the baudrate ?

    • http://mycomsats.com/ Nauman

      Baudrate should be same for your bluetooth module and microcontroller. In this application there is no need for baudrate. For some reason I set baudrate 4800 for both module and controller.

      • haris

        my controller and bluetooth module is set at 9600 baud rate
        i hope this program would work for it…

        • http://mycomsats.com/ Nauman

          Yes! it will,

          • haris

            Thank You :)