Android Bluetooth joystick

Hi hulkpeppe, welcome to the Forum as a new contributor

What does Arduino pin #2 and #3 means?

Digital I/O #2 to be connected to TX pin from Bluetooth module Digital I/O #3 to be connected to RX pin from Bluetooth module

...but it doesn't work. Any ideas what to do?

I need more information to make a clever guess ;)

I understand you are already seasoned with V3.1 and AndroLEDV10_3.ino

[u]Android side/u: 1) go to Option/About and confirm that installed version is V4.0 (or higher) 2) can you move the joystick and display position at bottom left 3) are you connected ("connected to: XXX", green text)

[u]Arduino side:[/u] 4) are you using AndroTest V1.3.ino (or higher) 5) are RX/TX connected as per above 6) is your Bluetooth card already configured @57600 bps (most are 9600bps by default) 7) is your Bluetooth card LED solid red (blinking red = not connected) 8 ) is your Serial monitor configured @57600 bps

[u]Finally:[/u] 9) your serial monitor should anycase display: AndroTest V1.3 - @kas2014 demo for V4.X (6 button version) does it ??

  • any additional information ??

Please advise

@all Please confirm V4.0 works OK for you

I'm using Itead BT shield and here are not Arduino pin #2 and #3 routed to BT TX and RX. I made a strap from Arduino pin #2 to BT shield TX and Pin #3 to RX. But still no sucess. :~

Yes, I got the V3.1 and AndroLEDV10_3.ino working with serial monitor and was able to visulize button #1 ON/OFF.

All checkpoints (1-7) in your answer are fulfilled.

In the Androind BT commander I observed some information under Data1 and Data2 (not under Data3). When I push any buttons they only got activated (green) a very short time. I don't see that LED at Arduino pin#13 got active when pushing button 1.

In the Androind BT commander I observed some information under Data1 and Data2 (not under Data3)

Interesting

IF - Data1 cycles from -3000 to 0 with 111 increment (see GetdataInt1) - Data2 cycles from 50 to -50 with -0.8 increment (see GetdataFloat2) - Data3 shows "xxxx"

THEN Arduino to Android communication is OK Android to Arduino communication is not wired correctly

When I push any buttons they only got activated (green) a very short time

Makes sense, Arduino will always feed back its own real buttons position and overwrite the Android display

Assuming you have the "Master/Slave" model: - make sure the switch is in DAT mode - place the TX jumper on pin 4 and the RX on Pin 5 - change "SoftwareSerial mySerial(2,3);" to "SoftwareSerial mySerial(4,5);"

Should work ;)

Hi Kas

Can you please post the communication protocol for Total RC commander V1.1 ? The communication from the android device to the arduino and between the arduino and the android device.

Total RC commander is the "video aware" version of Joystick BT Commander V4.x They both share the same Communication Protocol with Arduino

[u]Communication from Android to Arduino/u

  • Joystick information: Joystick data is transmitted as an integer = 2 Bytes (Most Significant Byte + Less Significant Byte) according to this data frame:

This is the relevant Android (Java) code snippet from Joystick Bluetooth Commander:

      final int XvalB = Xval + 200;
      final int YvalB = Yval + 200;
      if((((Xant!=Xval) || (Yant!=Yval)) || (mTimeoutCounter>=mMaxTimeoutCount && mMaxTimeoutCount>-1)))     // joystick position changed, or timeout occurred
          sendMessage( new String(new byte[] {STX, (byte) (XvalB / 128), (byte)(XvalB % 128), (byte)(YvalB / 128), (byte)(YvalB % 128), ETX } ));

So, for Xval=0 Yval=0: XvalB = YvalB = 200 XvalB / 128 = YvalB / 128 = 1 XvalB % 128 = XvalB % 128 = 72 Data frame: <2 1 72 1 72 3>

for Xval=100 Yval=100: XvalB = YvalB = 300 XvalB / 128 = YvalB / 128 = 2 XvalB % 128 = XvalB % 128 = 44 Data frame: <2 2 44 2 44 3>

The data frame is send via BlueTooth to the arduino board and decoded. This is the AndroTest V1.3.ino code snippet:

 void getJoystickState(byte data[5])    {
  int joyX = (data[1]<<7) + data[2];
  int joyY = (data[3]<<7) + data[4];
  joyX = joyX - 200;               // Offset to avoid
  joyY = joyY - 200;               // transmitting negative numbers
 }
  • Button information: Each time an android button is pressed, the following data frame is transmitted

buttonState is transmitted as a Byte value and reflects the state of the pushed button Button #1 ON: 1 Button #1 OFF: 2 Button #2 ON: 3 Button #2 OFF: 4 Button #3 ...

On the Arduino side, data frame is decoded in getButtonState()

void getButtonState(int bStatus)  {
  switch (bStatus) {
// -----------------  BUTTON #1  -----------------------
    case '1':
      buttonStatus |= B000001;        // ON
      Serial.println("\n** Button_1: ON **");
      // your code...      
      displayStatus = "LED ";
      Serial.println(displayStatus);
      digitalWrite(ledPin, HIGH);
      break;
    case '2':
      buttonStatus &= B111110;        // OFF
      Serial.println("\n** Button_1: OFF **");
      // your code...      
      displayStatus = "LED ";
      Serial.println(displayStatus);
      digitalWrite(ledPin, LOW);
      break;

// -----------------  BUTTON #2  -----------------------
    case '3':
      buttonStatus |= B000010;        // ON
      Serial.println("\n** Button_2: ON **");
      // your code...      
      displayStatus = "DEBUG ";
      Serial.println(displayStatus);
      DEBUG = true;
      break;
    case '4':
      buttonStatus &= B111101;        // OFF
      Serial.println("\n** Button_2: OFF **");
      // your code...      
      displayStatus = "DEBUG ";
      Serial.println(displayStatus);
      DEBUG = false;
      break;

    ....
}

[u]Communication back from Arduino to Android:[/u]

Arduino code

void sendBlueToothData()  {

  ....

    mySerial.print((char)0x2);                                             // Start of Transmission
    mySerial.print(getButtonStatusString());  mySerial.print((char)0x1);   // buttons status feedback
    mySerial.print(GetdataInt1());            mySerial.print((char)0x4);   // datafield #1
    mySerial.print(GetdataFloat2());          mySerial.print((char)0x5);   // datafield #2
    mySerial.print(displayStatus);                                         // datafield #3
    mySerial.print((char)0x3);                                             // End of Transmission
  
  ....  
}

Data frame transmitted back from Arduino to Android device: (by default, every 1000ms) < STX Buttons state 0X01 DataField#1 0x04 DataField#2 0x05 DataField#3 ETX >

example: < 0X02 001011 0X01 120.00 0x04 -4500 0x05 Motor enabled 0x03 >

Button state: This is a six character string reflecting the Arduino state for buttons position button #1,#2,#4: ON, all others: OFF >>> 001011 This feedback avoid any discrepancy with the Android device button state An Arduino Reset will reinitialize the Android buttons, Two way communication is mandatory for "clean" control Button state string is created in the getButtonStatusString() function.

String getButtonStatusString()  {
  String bStatus = "";
  for(i=0; i<6; i++)  {
    if(buttonStatus & B100000 >>i)      bStatus += "1";
    else                                bStatus += "0";
  }
  return bStatus;
}

Datafields: Data is transmitted as ASCII characters using Serial.print() Numbers are printed using an ASCII character for each digit 120.00 >>> 0X31, 0X32, 0X30, 0X2E, 0X30, 0X30

So < 0x02 001011 0x01 120.00 0x04 -4500 0x05 Motor enabled 0x03 > is actually transmitted as < 0x02, 0x30,0x30,0x31,0x30,0x31,0x31, 0X01, 0x31,0x32,0x30,0x2E,0x30,0x30, ** ** 0x04, 0x2D,0x34,0x35,0x30,0x30, 0x05, 0x4D,0x6F,0x74,0x6F,0x72,0x20,0x65, ** 0x6E,0x61,0x62,0x6C,0x65,0x64, 0x03 >** without commas and spaces

Should you have additional questions, let me know ;)

Firstly thanks Kas this is a great app/sketch.

I too couldnt get it to work but after some playing around i got this to work with these settings:

// Arduino RX to TX BlueTooth module // Arduino TX to RX BlueTooth module // make sure your BT board is set @9600 bps //Set Serial Monitor rate to 9600 bps

Thanks Kas for the protocol information

@Maudey Thanks for the feedback I edited reply #182 based on your comments Please post an App screenshot, together with your screen size and resolution This will help me optimizing App display layout

What BT brand are you using ?? Most boards default to 9600bps, which is a bit slow I strongly suggest to boost your board @57600bps

@tolisn63 Did you try the on board mini router ?? I hardwired connection between IP camera and router and still have to observe any noticiable lag 8)

Hi Kas

The mini router did not come in yet. I'm waiting to see how it goes.

Hi kas

Thank's for your help! Now it works fine for me :) I really appreciate this app/sketch :D

Hi I have a small problem. I didnt get receive any of datafields.

Hi kas Thanks for your help! Now it works fine for me :) I really appreciate this app/sketch :D

Thanks hulkpeppe for this positive feedback

@n3kx

Hi I have a small problem. I didn't get receive any of datafields.

I bit more information would definitely help ...

I use your basic sketch and your app, but under datafields in android is only xxxx

I use your basic sketch and your app, but under datafields in android is only xxxx

Not a lot of additional info's ;)

Carefully review those 9 questions and answer YES/NO

[u]Android side/u: 1) go to Option/About and confirm that installed version is V4.0 (or higher) 2) can you move the joystick and display position at bottom left 3) are you connected ("connected to: XXX", green text)

[u]Arduino side:[/u] 4) are you using AndroTest V1.3.ino (or higher) 5) are Bluetooth TX connected to D2 and RX connected to D3 6) is your Bluetooth card already configured @57600 bps (most are 9600bps by default) 7) is your Bluetooth card LED solid red or green (blinking = not connected) 8 ) is your Serial monitor configured @57600 bps

[u]Finally:[/u] 9) your serial monitor should anycase display: AndroTest V1.3 - @kas2014 demo for V4.X (6 button version) does it ??

  • any additional information ??

Hi Kas, I think your “Joystick bt commander” is brilliant – just what I want for some projects I am looking at. However, I am having intermittent problems connecting from Android to Arduino via HC-05 module and very rarely get data from Arduino to Android. Is there a correct boot up sequence between Arduino and Anroid or doesn’t it matter? When I changed the baud rate in AT commands I used AT+UART=57600,0,0 – is that correct? Also I think I may have a mismatch of Wiring diagram/ino code/Android app. Can you email me details of where I can get the latest drawings/codes etc. Again – brilliant application Many thanks Bob

YES I have 4.0 version YES I see changing position on bottom left YES I am conncted

YES I using ArduinoTest V1.3.ino YES I have Connected Bluetooth in right way YES I have solid led YES Is my serial monitor configured @57200 bps

YES My serial monitor display that information

Hi Bob1943

I think your “Joystick bt commander” is brilliant

Thanks 8)

I am having intermittent problems connecting from Android to Arduino via HC-05 module and very rarely get data from Arduino to Android

Can you be more specific, I would expect the App to always work or always fail... :roll_eyes: :roll_eyes:

Is there a correct boot up sequence between Arduino and Anroid or doesn’t it matter?

No, doesn't matter

When I changed the baud rate in AT commands I used AT+UART=57600,0,0 – is that correct?

Yes, make sure you have entered the Command mode I just prepared a sketch for my new HC-05 board. Will display - device name - firmware version - serial parameters

// Iteaduino BT board configurator - BASIC version

// Utility will display actual Baud rate

// TX BT board pin to Arduino D2
// RX BT board pin to Arduino D3
// 5V BT board pin to Arduino 5V
// GND BT board pin to Arduino GND

// set Serial Monitor @57600bps

#include "SoftwareSerial.h"

SoftwareSerial mySerial(2, 3);          // BlueTooth module TX RX
char response[30];                      // responses from module
int rv = 0;                             // return value

void setup()  {
  Serial.begin(57600);
  mySerial.begin(38400);                  //  config mode bps
  mySerial.setTimeout(500); 
  delay(300);
  Serial.print("\n\nIteaduino BT board configurator ");
  Serial.print("\nSearching ...");        
  mySerial.write("at\r\n");               // BT card bug ???
  delay(20);
  mySerial.readBytes(response, 15);
  mySerial.write("at\r\n");               // "AT" will respond with "OK" if it is working
  delay(20);
  response[0] = 0;
  rv = mySerial.readBytes(response, 15);
  response[rv] = 0;
  if (strncmp(response, "OK", 2)==0)    Serial.println("\n\nBluetooth Module found");
  else  {
    Serial.println("\nERROR: Bluetooth Module not responding!");
    Serial.println("Make sure there are no active bluetooth connection and try again");
    while(1);
  }
  
  delay(100);
  mySerial.write("at+name?\r\n");               // Check device name
  while(mySerial.available() == 0);
  rv = mySerial.readBytes(response, 30);
  response[rv] = 0;
  if (strncmp(response, "+NAME:", 6)==0)  {
    Serial.print("Name: ");
    Serial.println(response);
  }  else  {  
    Serial.println("Error checking name");
    while(1);
  }

  delay(100);
  mySerial.write("at+version?\r\n");           // Check firmware version
  while(mySerial.available() == 0);
  response[0] = 0;
  rv = mySerial.readBytes(response, 30);
  response[rv] = 0;
  Serial.print("Version: ");
  Serial.println(response);

  delay(100);
  mySerial.write("at+uart?\r\n");           // Check serial parameters
  while(mySerial.available() == 0);
  response[0] = 0;
  rv = mySerial.readBytes(response, 30);
  response[rv] = 0;
  Serial.print("Serial: ");
  Serial.println(response);
}

void loop() { }

Please confirm your BT board baud rate

Also I think I may have a mismatch of Wiring diagram/ino code/Android app. Can you email me details of where I can get the latest drawings/codes etc.

Bluetooth TX connected to D2 and RX connected to D3 Joystick bluetooth Commander V4.0 is here AndroTest V1.3.ino is there

Please answer the 9 questions as per above

@n3kx

Base on your responses, problem is the BT card to Arduino board serial connection Is your BT card HC-05 or HC-06 ?? let me have a link to your specific model You may reverse TX and RX cables just to make sure, this will not harm anything Did you actuallly check you BT card baud rate ?? they usually default to 9600bps Have succesfully used this card for other projects ??

Can you see the scrolling Joystick values in the Serial Monitor ??

My model is HC-05 and it work @9600bps. I use your older version and it works, even on version 4.0 it works Arduino receive joystick data as button data no problem. But datafields doesnt change there are always showing XXXX.

I have this type of Bluetooth: http://www.ebay.com/itm/Wireless-Serial-6-Pin-Bluetooth-RF-Transceiver-Module-HC-05-RS232-Master-Slave-D-/400709013564?pt=LH_DefaultDomain_2&hash=item5d4c1e503c

Hi, thanks for your rapid response. Your assistance in helping is excellent. Hope I have answered your questions so I can get a response from Arduino UNO back to Android

I used the AT Command setup at this URL http://arduinofy.blogspot.co.uk/2013/10/tutorial-programming-hc-05-at-mode-with.html

when completed AT+UART? Returned: 57600,0,0

Your sketch headed: // Iteaduino BT board configurator - BASIC version Returned the following error message:

Iteaduino BT board configurator Searching ... ERROR: Bluetooth Module not responding! Make sure there are no active bluetooth connection and try again I couldn’t find anything paired with the HC-05 The red BT LED flashed on/off about every half second

Your sketch headed: AndroTest V1.3 - @kas2014\ndemo for V4.X (6 button version) Only Connections: .Arduino pin#2 to TX BlueTooth module . Arduino pin#3 to RX BlueTooth module ..Arduino 3.3v to 3.3 BlueTooth module ..Arduino GND to GND NOTE: Key and 5v not connected

Paired and connected with Android (v4.0.4) OK. Still only xxx showing bottom left. Interesting point is that button B5 stays locked the same as other buttons until pressed again to release. (I did notice at one time it did act like a push button – maybe that was when data got fed back). Just a thought I think the connection problems may be down to me as that problem appears to have gone away. My only problem now (apart from error in BT board configurator mentioned above) is the failure to get a signal from Arduino to Adoid.

Answers to 9 question in reply #197 1..Version v4.0.4 2. Yes 3. Yes 4. Yes 5. Yes 6. Yes 7. NO – Initially Red LED flashes continuously but when connected it flashes twice every couple of seconds (not solid red as in question) 8. Yes 9. Yes

Observations: I am not experienced with serial coms., but understand there are parity and stop bits. When I changed the BT module to 57600 baud I sent AT+UART=57600,0,0 and the AT command AT+UART? returned: 57600,0,0 Is that OK?

BT Red LED flashes rapldly before pairing. Then when paired changes to on/off flash every 2 seconds then but when connected it flashes twice every couple of seconds.

UNO TX LED flashes about every 2 seconds and whenever a button is pressed. When the joystick is moved it constantly flickers, then after about 10 seconds of continuous joystick movement the TX LED locks on. Opening the serial monitor window seems to return it back to normal. UNO RX LED only ever flashes when uploading code

Hope some of this makes sense – thanks again for your help Bob