Go Down

Topic: Android Bluetooth joystick (Read 70 times) previous topic - next topic

tolisn63

Great, thanks

just a few ideas that maybe can implemented in future releases.
1. Text overlay on the video feed, like a HUD. Instead of printing the adruino sent messages under the video screen  overlay them on the video feed.
2. Have an option to switch between the joystick style control and a four directional arrow control.

Any idea when Total RC commander will be available with the new protocol and a test sketch ?

kas

#256
Jun 15, 2014, 06:56 pm Last Edit: Jun 24, 2014, 06:30 pm by kas Reason: 1
This is AndroTest V2.0 demo sketch for Joystick BT Commander V5.x

modified variables
- STX
- ETX
- cmd[8]

modified functions:
- loop()
- getJoystickState()
- getButtonState()

Code: [Select]
#define VERSION     "\n\nAndroTest V2.0 - @kas2014\ndemo for V5.x App"

// V2.0  changed to pure ASCII Communication Protocol ** not backward compatible **
// 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 pin #13 LED
// Button #4 toggle datafield display rate
// Button #5 configured as "push" button (momentary)
// Other buttons display demo message

// 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          0x02
#define    ETX          0x03
#define    ledPin       13
#define    SLOW         750                            // Datafields refresh rate (ms)
#define    FAST         250                             // Datafields refresh rate (ms)

SoftwareSerial mySerial(2,3);                           // BlueTooth module: pin#2=TX pin#3=RX
byte cmd[8] = {0, 0, 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
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
}

void loop() {
 if(mySerial.available())  {                           // data received from smartphone
   delay(2);
   cmd[0] =  mySerial.read();  
   if(cmd[0] == STX)  {
     int i=1;      
     while(mySerial.available())  {
       delay(1);
       cmd[i] = mySerial.read();
       if(cmd[i]>127 || i>7)                 break;     // Communication error
       if((cmd[i]==ETX) && (i==2 || i==7))   break;     // Button or Joystick data
       i++;
     }
     if     (i==2)          getButtonState(cmd[1]);    // 3 Bytes  ex: < STX "C" ETX >
     else if(i==7)          getJoystickState(cmd);     // 6 Bytes  ex: < STX "200" "180" ETX >
   }
 }
 sendBlueToothData();
}

void sendBlueToothData()  {
 static long previousMillis = 0;                            
 long currentMillis = millis();
 if(currentMillis - previousMillis > sendInterval) {   // send data back 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)STX);                                             // 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)ETX);                                             // 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 GetdataInt1()  {              // Data dummy values sent to Android device for demo purpose
 static int i= -30;              // Replace with your own code
 i ++;
 if(i >0)    i = -30;
 return i;  
}

float GetdataFloat2()  {           // 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[8])    {
 int joyX = (data[1]-48)*100 + (data[2]-48)*10 + (data[3]-48);       // obtain the Int from the ASCII representation
 int joyY = (data[4]-48)*100 + (data[5]-48)*10 + (data[6]-48);
 joyX = joyX - 200;                                                  // Offset to avoid
 joyY = joyY - 200;                                                  // transmitting negative numbers

 if(joyX<-100 || joyX>100 || joyY<-100 || joyY>100)     return;      // commmunication error
 
// Your code here ...
   Serial.print("Joystick position:  ");
   Serial.print(joyX);  
   Serial.print(", ");  
   Serial.println(joyY);
}

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

// -----------------  BUTTON #2  -----------------------
   case 'C':
     buttonStatus |= B000010;        // ON
     Serial.println("\n** Button_2: ON **");
     // your code...      
     displayStatus = "Button2 <ON>";
     Serial.println(displayStatus);
     break;
   case 'D':
     buttonStatus &= B111101;        // OFF
     Serial.println("\n** Button_2: OFF **");
     // your code...      
     displayStatus = "Button2 <OFF>";
     Serial.println(displayStatus);
     break;

// -----------------  BUTTON #3  -----------------------
   case 'E':
     buttonStatus |= B000100;        // ON
     Serial.println("\n** Button_3: ON **");
     // your code...      
     displayStatus = "Motor #1 enabled"; // Demo text message
     Serial.println(displayStatus);
     break;
   case 'F':
     buttonStatus &= B111011;      // OFF
     Serial.println("\n** Button_3: OFF **");
     // your code...      
     displayStatus = "Motor #1 stopped";
     Serial.println(displayStatus);
     break;

// -----------------  BUTTON #4  -----------------------
   case 'G':
     buttonStatus |= B001000;       // ON
     Serial.println("\n** Button_4: ON **");
     // your code...      
     displayStatus = "Datafield update <FAST>";
     Serial.println(displayStatus);
     sendInterval = FAST;
     break;
   case 'H':
     buttonStatus &= B110111;    // OFF
     Serial.println("\n** Button_4: OFF **");
     // your code...      
     displayStatus = "Datafield update <SLOW>";
     Serial.println(displayStatus);
     sendInterval = SLOW;
    break;

// -----------------  BUTTON #5  -----------------------
   case 'I':           // configured as momentary button
//      buttonStatus |= B010000;        // ON
     Serial.println("\n** Button_5: ++ pushed ++ **");
     // your code...      
     displayStatus = "Button5: <pushed>";
     break;
//   case 'J':
//     buttonStatus &= B101111;        // OFF
//     // your code...      
//     break;

// -----------------  BUTTON #6  -----------------------
   case 'K':
     buttonStatus |= B100000;        // ON
     Serial.println("\n** Button_6: ON **");
     // your code...      
      displayStatus = "Button6 <ON>"; // Demo text message
    break;
   case 'L':
     buttonStatus &= B011111;        // OFF
     Serial.println("\n** Button_6: OFF **");
     // your code...      
     displayStatus = "Button6 <OFF>";
     break;
 }
// ---------------------------------------------------------------
}




kas

@tolisn63
Quote
Great, thanks
just a few ideas that maybe can implemented in future releases.
1. Text overlay on the video feed, like a HUD. Instead of printing the arduino sent messages under the video screen  overlay them on the video feed.

Well... to keep it simple, I have Bluetooth and WiFi modules well apart
This is easier for maintenance and upgrade
Text overlay would create modules intricacy

Quote
2. Have an option to switch between the joystick style control and a four directional arrow control.
Nice suggestion
I will first implement the long awaited accelerometer control (tilting the Android device)

Quote
Any idea when Total RC commander will be available with the new protocol and a test sketch ?

Will wait for Joystick BT commander V5.0 feedback
Would say two or three weeks

thornlv

Hi Kas

Have tried the updated Andro_Pan_Tilt sketch but it did not work.
What happens is the servos centre and when I use the joystick I get a few lines of positions in serial monitor and then 'communication error' repeating lines. MyservoX changes to full 180 position  and stays there (motor whirring) whilst MyservoY does nothing.

I played with the delay settings in the void loop to no avail (same behavior) except delay of 1 stops the communication error but the servos do not respond.

My Phone is Sony experia, am using an arduino Uno and a HC-06 bluetooth module (Set to 57600 with your AT sketch)
BT TX to D0, Servos to 9 and 10 (BT RX not used ?)

Hope this helps

kas

#259
Jun 16, 2014, 06:46 pm Last Edit: Jun 16, 2014, 07:17 pm by kas Reason: 1
Joystick Bluetooth Commander Version 5.0  has just been published on Google Play

Change log:
- improved communication protocol  see reply #252
- smoother Joystick behavior  (thanks Bob1943)
- automatically flip if the user turns device upside down  (thanks joepro)
- optimized layout  (thanks billcat)

Demo sketch: AndroTest V2.0       see reply #256

Please let me have feedback, including screenshots

Enjoy   ;)

kas

#260
Jun 16, 2014, 07:14 pm Last Edit: Jun 16, 2014, 07:19 pm by kas Reason: 1
@thornlv
Quote
Have tried the updated Andro_Pan_Tilt sketch but it did not work.
What happens is the servos centre and when I use the joystick I get a few lines of positions in serial monitor and then 'communication error' repeating lines. MyservoX changes to full 180 position  and stays there (motor whirring) whilst MyservoY does nothing.

Go to settings/About and confirm your actual App version
What is your Arduino board model ??
You don't need to connect motors before you get sensible readings on your Serial Monitor

Quote
BT TX to D0, Servos to 9 and 10 (BT RX not used ?)

Yes, no need to send data back to your phone

thornlv

Hi Kas
App is V4 and the board is a Uno v3

Hope this helps
Cheers, Vic

kas

@thornlv
Sorry guy, I was too self confident in modifying the code and not testing it  :smiley-red:
I just modified my code (reply #354)
Now fully tested on Serial Monitor ;)

Please upgrade and use Joystick BT Commander V5.0

thornlv

Hi Kas.

Have briefly tried it and it works ok, will give a thorough test when I have finished my domestic duties today by order of her indoors  ;)

Cheers mate

Vic

kas

Quote
Hi Kas.
Have briefly tried it and it works ok, will give a thorough test when I have finished my domestic duties today by order of her indoors   ;)

Cheers mate
Vic


Great  :)
Remember, you can adjust Zero position, max move and motors direction
When you have time, please post a video showing both App screen and moving gimball


thornlv

Hi Kas

Gave it a good going over, everything works ok. going to work out how to adjust maximum movement as I dont need full 180 deg field - at around 90deg for pan and 45 for tilt. Next will be to include an old HTC phone to act as a 'monitor' with V5 - because live view with a 300/600mm lens is a narrow field which makes it difficult to adjust position for the subject, I see the option of a 'wider angle' view on the smart phone will aid that much more quickly - I will try to show you what I mean when record a video of the intended set up.

Many thanks.#

Vic

kas

Quote
hi kas , i saw your nice app and i thought telling you a problem that  i have with my app.. well my hc-06 disconnects in really random times.. it might work for 10 minutes or more with no problem while on the other hand it might start disconnecting again and again without being able to understand why..
Any ideas why this might happening?

Thank you  :) :)


@zaxarias

Some shots in the dark
- Your board is resetting due to high load, use a separate power supply for Arduino
- intermittent contact within breadboard, move and secure components
- defective BT board, not an expensive item, try a spare one

Let us know the outcome

jaapdemaat

Hi All,
Ive been using this app (v5) for my remote controlled filling cabinet project. https://vimeo.com/98759560
Kas has been helping intensively both with customizing the app and the Arduino code.
Attached is the final code I used, incl distance sensor feedback & Battery level and lots more.

Hardware: Arduino mega, DC Motor Driver 2x 15A - Lite,  BT2S Bluetooth to Serial Slave, HC-SR04 Distance Sensors, Pride electric wheel chair, blood sweat and tears. You can find more making of videos on: https://vimeo.com/jaapdemaat/videos.


john_lenfr

Bonjour Kas, juste pour confirmer que l'appli fonctionne avec le programme exemple:
Quote
AndroTest V2.0 - @kas2014
demo for V5.x App


Et avec :
Quote
Samsung Galaxy Note 3
Android 4.4.2


Quote

HC 05 bluetooth module


Acheté ici:
http://www.dx.com/p/hc-05-wireless-bluetooth-serial-pass-through-module-w-backboard-green-blue-298911#.U6a0QPl_sjE


Je n'ai pas encore testé en détail (j'ai remarqué qu'il y a pleins d'options)mais je voulais te féliciter pour ton travail qui a l'air énorme!

Les seuls points négatifs que j'aurais à redire sont sur la communication de ton programme, mais je te rassure ce sont des points de détails comparé au reste de ton travail:
- Tu devrais éditer ton premier post pour mettre ton versionning en sens inverse: le plus récent en haut de la liste (on a plus l'habitude que ce soit dans ce sens et ça perturbe pour trouver la bonne info)
- Pour les renvois vers les posts des version etc fais juste un lien hypertext sur le post en question par exemple si tu parles de la réponse #256 tu la transforme en #256 c'est de suite plus pratique.
- Pour le coté GooglePlay met un lien direct vers le programme exemple (fichier .ino) car sinon il faut venir sur ce post, chercher la dernière version, et ensuiite se rendre à la réponse #256 pour enfin faire un copier/coller du code: le nombre de manip est rebuttant et cela peut causer problème pour une personne qui s'y connait pas trop mais qui voudrait utiliser ton appli.

Encore une fois ce sont des critiques constructives pour que tu puisse faire partager cet excellente application à encore plus de monde!

;)

kas

#269
Jun 22, 2014, 01:00 pm Last Edit: Jun 29, 2014, 05:05 pm by kas Reason: 1
Quote
Hi All,
Ive been using this app (v5) for my remote controlled filling cabinet project. https://vimeo.com/98759560
Kas has been helping intensively both with customizing the app and the Arduino code.
Attached is the final code I used, incl distance sensor feedback & Battery level and lots more.

Hardware: Arduino mega, DC Motor Driver 2x 15A - Lite,  BT2S Bluetooth to Serial Slave, HC-SR04 Distance Sensors, Pride electric wheel chair, blood sweat and tears. You can find more making of videos on: https://vimeo.com/jaapdemaat/videos.



For info
Jaap is a senior graphic designer, based in Rotterdam and London
The Cabinet ("I Know What You Did Last Summer") is currently exhibited at the Royal College of Art, in London, up to June 27th


@Jaap,
Thanks again for your hospitality in London, I really enjoyed entering your universe for a while



Go Up