Android Bluetooth joystick

Data flow from Arduino to Android problem SOLVED

I am using Joystick BT Commander on an Android Tablet. This allows Apps to be closed by swiping the minimised app icon off the screen. This was the problem.

When I closed the App by using back button (hairpin arrow) and confirm “Total RC Commander - close this controller” and then re-open the App it works OK. Data received back from Arduino and Button 5 is in push mode. Apologies for inconvenience and thanks for your time. It is great to see the care and level of attention that you provide to the users of your App. Again many thanks - Brilliant App Bob

Data flow from Arduino to Android problem SOLVED ..... It is great to see the care and level of attention that you provide to the users of your App. Again many thanks - Brilliant App Bob

Thanks Bob for the good new and the kind words 8)

I am investing time because this App should bring a lot to the Arduino community It is a universal remote for most projects, using wide spread (Android) technologies. I told my friend to download the App and within a minute he took control of my RC tank He was rather impressed :astonished: :astonished: :astonished:

During my support session, I did some interesting stress tests yesterday. I tried various data refresh interval at different baud rates and found some minor glitches. Please use AndroTest_V14.ino

#define VERSION     "\n\nAndroTest V1.4 - @kas2014\ndemo for V4.X  (6 button version)"

// V1.4  improved communication errors handling
// V1.3  renamed for publishing, posted on 09/05/2014
// V1.2  Text display   ** not backward compatible **
// V1.1  Integer display
// V1.0  6 buttons + 4 data char implemented

    **  The message exceeds the maximum allowed length (9500 characters)  **
See next message

This updated sketch features optimized timing and better transmission error management Again, make sure your BT board is set @57600bps Avoid using 25ms refresh interval and continuous timeout count, that's too much data I am also preparing V5.0, with a pure ASCII communication protocol

Some thoughts

  1. Where is AndroTest_V14.ino – only some lines showing?

  2. In the Android app can you recognise when the app has been closed ‘illegally’ and act upon that action next time the app is opened. Might make it idiot proof.

  3. When you first touch the yellow joystick dot, it immediately moves to the centre of the touch point. This changes the xy numbers – most noticeable when you touch the edge. (I want to control two servos but don’t want them to move until the joystick is moved intentionally). Can the touch point be recognised without moving the yellow dot?

  4. What do I need to change to alter the data fields fed back to the Android and can it be coded in “Your code here” areas in the Arduino sketch. I am doing number crunching on Arduino from Joystick positions and would like to send three values back to android.

Here it is

#define VERSION     "\n\nAndroTest V1.41 - @kas2014\ndemo for V4.X  (6 button version)"

// V1.41 minor modifications
// V1.4  improved communication errors handling
// V1.3  renamed for publishing, posted on 09/05/2014
// V1.2  Text display   ** not backward compatible **
// V1.1  Integer display
// V1.0  6 buttons + 4 data char implemented

// Demo setup:
// Button #1 controls pin13 LED
// Button #2 starts DEBUG
// Button #3 displays demo message
// Button #4 toggle datafield display rate
// Button #5 configured as "push" button (momentary)

// Arduino pin#2 to TX BlueTooth module
// Arduino pin#3 to RX BlueTooth module
// make sure your BT board is set @57600 bps
// better remove SoftSerial for PWM based projects

// For Mega 2560:
// remove   #include "SoftwareSerial.h", SoftwareSerial mySerial(2,3);
// search/replace  mySerial  >> Serial1
// pin#18 to RX bluetooth module, pin#19 to TX bluetooth module

#include "SoftwareSerial.h"

#define    STX          0x01
#define    ETX          0x00
#define    ledPin       13
#define    SLOW         1000                           // Datafields refresh rate (ms)
#define    FAST         250                            // Datafields refresh rate (ms)

SoftwareSerial mySerial(2,3);                           // BlueTooth module: pin#2=TX pin#3=RX
int i=0;
byte cmd[6] = {0, 0, 0, 0, 0, 0};                       // bytes received
byte buttonStatus = 0;                                  // first Byte sent to Android device
long previousMillis = 0;                                // will store last time Buttons status was updated
boolean setButtonFeedback = false;                      // momentary buttons feedback to Android device
long sendInterval = SLOW;                               // interval between Buttons status transmission (milliseconds)
String displayStatus = "xxxx";                          // message to Android device

void setup()  {
  Serial.begin(57600);
  mySerial.begin(57600);                                // 57600 = max value for softserial
  pinMode(ledPin, OUTPUT);     
  Serial.println(VERSION);
  while(mySerial.available())  mySerial.read();         // empty RX buffer    //  <127 || i>5)                               break;   // Communication error << XXX Mod
        if((cmd[i]==ETX) && ((i==2 && cmd[1]>2) || i==5))   break;   // Button or Joystick data
        i++;
      }
      if     (i==2 && cmd[1]>48 && cmd[1]<68)     getButtonState(cmd[1]);                         // 3 Bytes
      else if(i==5 && cmd[1]<3  && cmd[3]<3 )     getJoystickState(cmd);                          // 6 Bytes
    }
  }
  sendBlueToothData();                                                       //  < sendInterval) {   // send data to smartphone
    previousMillis = currentMillis; 

// Data frame transmitted back from Arduino to Android device:
// < 0X02   Buttons state   0X01   DataField#1   0x04   DataField#2   0x05   DataField#3    0x03 >  
// < 0X02       01011       0X01      120.00     0x04      -4500      0x05   Motor enabled  0x03 >    // example

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

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

int GetDataField_1_int()  {            // Data dummy values sent to Android device for demo purpose
  static int i= -30;                   // Replace with your own code
  i ++;
  if(i >0)    i = -3000;
  return i;  
}

float GetDataField_2_float()  {       // Data dummy values sent to Android device for demo purpose
  static float i=50;                  // Replace with your own code
  i-=.5;
  if(i <-50)    i = 50;
  return i;  
}

void getJoystickState(byte data[6])    {                             //  <100 || joyY<-100 || joyY>100)    return; // commmunication error
  
  Serial.print("Joystick position:  ");
  Serial.print(joyX); Serial.print(", "); Serial.println(joyY);
    
// Your code here ...
}

void getButtonState(int bStatus)  {
  switch (bStatus) {
// -----------------  BUTTON #1  -----------------------
    case '1':
      buttonStatus |= B000001;        // ON
      Serial.println("\n** Button_1: ON **");    // your code here...
      displayStatus = "LED ";
      Serial.println(displayStatus);
      digitalWrite(ledPin, HIGH);
      break;
    case '2':
      buttonStatus &= B111110;        // OFF
      Serial.println("\n** Button_1: OFF **");    // your code here...
      displayStatus = "LED ";
      Serial.println(displayStatus);
      digitalWrite(ledPin, LOW);
      break;
// -----------------  BUTTON #2  -----------------------
    case '3':
      buttonStatus |= B000010;        // ON
      Serial.println("\n** Button_2: ON **");    // your code here...
      break;
    case '4':
      buttonStatus &= B111101;        // OFF
      Serial.println("\n** Button_2: OFF **");    // your code here...
      break;
// -----------------  BUTTON #3  -----------------------
    case '5':
      buttonStatus |= B000100;        // ON
     // setButtonFeedback = true;            moved to button #5 (for push buttons)
      Serial.println("\n** Button_3: ON **");    // your code here...
      displayStatus = "Motor #1 enabled"; // Demo text message
      Serial.println(displayStatus);
      break;
    case '6':
      buttonStatus &= B111011;      // OFF
      Serial.println("\n** Button_3: OFF **");    // your code here...
      displayStatus = "Motor #1 stopped";
      Serial.println(displayStatus);
      break;
// -----------------  BUTTON #4  -----------------------
    case '7':
      buttonStatus |= B001000;       // ON
      Serial.println("\n** Button_4: ON **");    // your code here...
      displayStatus = "Datafield update ";
      Serial.println(displayStatus);
      sendInterval = FAST;
      break;
    case '8':
      buttonStatus &= B110111;    // OFF
      Serial.println("\n** Button_4: OFF **");    // your code here...
      displayStatus = "Datafield update ";
      Serial.println(displayStatus);
      sendInterval = SLOW;
     break;
// -----------------  BUTTON #5  -----------------------
    case '9':           // configured as momentary button
//      buttonStatus |= B010000;        // ON
      setButtonFeedback = true;                     // moved from button #3
      Serial.println("\n** Button_5: + pushed + **"); // your code here...
      displayStatus = "Button_5: ++ pushed ++";
      break;
//   case 'A':
//     buttonStatus &= B101111;        // OFF
//     break;
// -----------------  BUTTON #6  -----------------------
    case 'B':
      buttonStatus |= B100000;        // ON
      Serial.println("\n** Button_6: ON **");    // your code here...
       displayStatus = "Button #6 ON"; // Demo text message
     break;
    case 'C':
      buttonStatus &= B011111;        // OFF
      Serial.println("\n** Button_6: OFF **");    // your code here...
      displayStatus = "Button #6 OFF";
      break;
// -----------------------------------------------------
  }
}

EDIT: moved setButtonFeedback from button #3 to button #5 (push button) Added 1ms delay after while(mySerial.available()) Renamed to AndroTest V1.41 see Reply #225

  1. In the Android app can you recognise when the app has been closed ‘illegally’ and act upon that action next time the app is opened. Might make it idiot proof.

Not sure to understand, please clarify "This allows Apps to be closed by swiping the minimized app icon off the screen"

Can the touch point be recognised without moving the yellow dot?

I understand your concern, makes sense, let me think about it...

  1. What do I need to change to alter the data fields fed back to the Android and can it be coded in “Your code here” areas in the Arduino sketch. I am doing number crunching on Arduino from Joystick positions and would like to send three values back to android.

To send back three values to Android (data_1, data_2,data_3,) just modify sendBlueToothData()

void sendBlueToothData()  {

    ......................
    ......................

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

Hi, Thanks for information

The swipe-to-close feature is about 40 seconds into video at this site: http://blog.laptopmag.com/9-awesome-android-ice-cream-sandwich-tips

This method is what was causing problem about no data from Arduino to Android. I wonder if this happens with other users?

The swipe-to-close feature is about 40 seconds into video at this site:
http://blog.laptopmag.com/9-awesome-android-ice-cream-sandwich-tips
This method is what was causing problem about no data from Arduino to Android. I wonder if this happens with other users?

I can’t replicate the “swipe” problem both on Galaxy Tab3 tablet and Motorola G phone (V4.4.2)
Please let me have a step by step procedure

I am using a 7 inch tablet which doesn't have phone but allows multitasking. My Samsung GT-S5570 phone won't recognise the HC-05 so I can't test on mobile phone. However, the link below shows how to swipe on Galaxy 3. Hope this helps. I think all Android tablets have swipe-to-close so others may also have a problem. If you want I can video the actions. Just let me know

https://www.youtube.com/watch?v=I5wRaFSDIPo

(anyone know why my Samsung GT-S5570 phone won't recognise the HC-05)

@ Bob1943

  1. When you first touch the yellow joystick dot, it immediately moves to the centre of the touch point. This changes the xy numbers – most noticeable when you touch the edge. (I want to control two servos but don’t want them to move until the joystick is moved intentionally). Can the touch point be recognised without moving the yellow dot?

I understand your concern, makes sense, let me think about it...

I addressed the situation ;) Please let me have your email via PM, I will send you V4.1 for testing

I addressed the situation ;) Please let me have your email via PM, I will send you V4.1 for testing

Check your mail ...

Hi Kas, This version V4.1 works fine. Wherever you touch on the yellow spot is returned as coordinate 0,0 (x,y) which is exactly what I was looking for. No jumpy servos anymore. - Many thanks

A couple of comments on Joystick BT Commander.

  1. As information can be sent from Arduino to Android, is it possible to use the Android like a Serial monitor to display this information with rolling values - just like the USB to computer screen. You could give the option to change from Joystick to Monitor to make space on the Android screen. An option to rotate the Android to make a deeper window would help, and an added bonus would be to have a button to pause the display if required.

  2. Can the Arduino Bluetooth code be put into a library so the bluetooth device can be connected and used as a monitoring/commander tool. This would make the Arduino coding for the main applications more visible.

Just a thought!!!

This version V4.1 works fine. Wherever you touch on the yellow spot is returned as coordinate 0,0 (x,y) which is exactly what I was looking for. No jumpy servos anymore. - Many thanks

Thanks, I will include this feature in V5.0 (pure ASCII protocol)

  1. As information can be sent from Arduino to Android, is it possible to use the Android like a Serial monitor to display this information with rolling values - just like the USB to computer screen. You could give the option to change from Joystick to Monitor to make space on the Android screen.

Android Bluetooth joystick is a small footprint App (78Ko), its only task is to emulate a joystick ;) There are very good App for serial monitoring, BlueTerm is one of those

My Samsung GT-S5570 phone won't recognise the HC-05 so I can't test on mobile phone

Try sending AT command

AT+CLASS=1

Should work

@Topicja

Hi Kas I really like your excellent android app but after last update i cannot get it to work. I tried to rewrite my program but i don't receive any data. Could you please send me apk file of older version? Version 3.1 was working well for me. My e-mail address is xxxxxxxx@xxx.com

Thank you for your response and your willingness

I could recompile V3.1, but it makes much more sense to have your own program fixed and working Joystick transmission has not been modified and should work, does it?? Please let me have your original Arduino sketch, I will fix it for you

@all Should you encounter same difficulties, please contact me via PM

Sorry for late response. My problem was in bautrate and also you must press 3.rd button to get work … so yust overwrite setbuttonfeedback=false to setbuttonfeedback=true and it work for my.

@Topicja

I could recompile V3.1, but it makes much more sense to have your own program fixed and working Joystick transmission has not been modified and should work, does it?? Please let me have your original Arduino sketch, I will fix it for you

I will send you the modified sketch tomorrow evening There are too many transmission errors at 9600 bps Please, in the meantime, configure your BT card at 57600 bps

Are you able to do it by yourself ?? Otherwise, let me have a link to your specific model

For those who still have a Bluetooth card configured @9600bps, please add a one millisecond delay after the second "while(mySerial.available())"

void loop() {
  if(mySerial.available())  { 
    delay(2);
    cmd[0] =  mySerial.read();  
    if(cmd[0] == STX)  {
      i=1;      
      while(mySerial.available())  {
      delay(1);                     //  <

This will prevent while(mySerial.available()) to return before the complete data frame is received I have modified AndroTest V1.4 to reflect this modification (now Version 1.41)

I only work @57600bps and never bothered checking lower data speeds :blush: Thanks Topicja for bringing this point to my attention

A sweet thought for the Microsoft developers who deal with thousands different hardware configurations and get bashed by users because... "it doesn't work on MY specific setup" ;)

@n3kx

My problem was in bautrate

Glad it finally works :)

and also you must press 3.rd button to get work .... so you overwrite setbuttonfeedback=false to setbuttonfeedback=true and it work for my

:roll_eyes: :roll_eyes: :roll_eyes: setButtonFeedback was created for handling push buttons This is a flag to send an earlier feed back to Android, for a consistent visual effect setButtonFeedback is reset to False with each sendBlueToothData() call:

  if(setButtonFeedback == true)  {                      // allow momentary button visual effect
    previousMillis = currentMillis + 250;   
    setButtonFeedback = false;
  }

This code snippet could be safely omitted (will be removed in next release):

setButtonFeedback is misplaced and should be moved from Button #3 to Button #5 (push button) I edited accordingly AndroTest V1.4 in response #212

Please show me the way to check and configure my bluetooth card
thanks, Christian

Bluetooth cards come in a many flavors HC-05, HC-06, HC-07, HC-08 …

I personally own an HC-06

and a very convenient Arduino/HC-05 combo

Configuration methods are quite different according to board type

For 57600 bps setup:

HC-05:        AT+UART=57600,0,0\r\n        (with linefeed + return)
HC-06:        AT+BAUD7                     (w/o linefeed + return)

The all point is to access the board configuration mode
My HC-05 board has a built in switch + reset button for easy operation (see above)

Arduino member Nick_Pyner has produced a very interesting document, I learned a lot from it
Don’t forget to increase Nick’s Karma by hitting the green “+” sign below his name

Complete AT command set:
HC-05
HC-06

Further readings here

Finally, I attach two sketches for configuring HC-05 and HC-06 boards

Enjoy the fascinating Bluetooth universe :wink:

@all
Feel free to post your personal experience and additional links

BT_Config_HC_05.ino (3.96 KB)

BT_Config_HC_06.ino (3.3 KB)

Hello Kas,

First I would like to say thank you for the great piece of software you have written. It has made implementing this type of controls into my project SO much easier.

I do have one question for you, but it would probably be better to give you a little background first...

I'm currently developing an autonomous mobile robot platform, with the UI to control it on an Android tablet. I've developed most of the UI already for my device, and included a button that opens your Joystick BT Commander app directly from within my UI, for when the end user needs to manually control the robot.

However the Joystick BT Commnader app looks nothing like my UI design/theme, most notably you have a dark theme while my UI uses a "light" theme with a white background.

So my question for you is are you planning on implementing any sort of customization in this area into your app in the near future at all? If not, is there some way that I can change the colors so our software would better compliment eachother? I know I can go back and redesign my UI to match your app, but for my particular application (outdoors and/or in direct sunlight) a dark theme is a lot harder for the end user to see.

Thanks in advance for any assistance you can provide. It is greatly appreciated.

Hi joepro Thanks for the comments Should be included in V5.0, to be posted within 4 weeks