Go Down

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

kas

#90
Aug 28, 2013, 07:16 pm Last Edit: Sep 01, 2013, 08:04 am by kas Reason: 1
This is the complete Arduino demo sketch for V3.0

Code: [Select]
// AndroLED V9

// Arduino test sketch for Joystick BT commander
// V9.0: ** Release for Android BT Commander V3.0 AND HIGHER ONLY **

//  Arduino pin#2 to TX BlueTooth module
//  Arduino pin#3 to RX BlueTooth module

#include "SoftwareSerial.h"

#define    DEBUG        false
#define    STX          0x02
#define    ETX          0x03

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
byte dataByte = 0;                    // second Byte sent to Android device
long previousMillis = 0;              // will store last Buttons status was updated

long interval = 1000;                 // interval to transmit Buttons status (milliseconds)

void setup()  {
 Serial.begin(57600);
 mySerial.begin(57600);              // 57600 = max value for softserial
 Serial.println("AndroLED V9 - demo for Joystick BT commander V3.0");
 delay(300);
}

void loop() {
 if(mySerial.available())  {            // received from smartphone
   delay(5);
   cmd[0] =  mySerial.read();  
   if(DEBUG)  Serial.println(cmd[0]);   // to serial monitor
   if(cmd[0] == STX)  {  
     i=1;      
     while(mySerial.available() && ((cmd[i]=mySerial.read()) != ETX)) {
       if(i>5)  break;
       if(DEBUG)    {Serial.print(i); Serial.print(": "); Serial.println(cmd[i]);}
       i++;
     }
   }
   if(i==2)    setLED(cmd[1]);
   else if(i==5)    setJoystick_Int(cmd);  // 6 Bytes
   else Serial.println("****");
 }  else  {
   unsigned long currentMillis = millis();
   if(currentMillis - previousMillis > interval) {
     dataByte = GetdataByte();
     sendData(buttonStatus, dataByte);
     previousMillis = currentMillis;  
   }
 }
 delay(5);
}

void setLED(int LEDstatus)  {
 switch (LEDstatus) {
   case '1':
     buttonStatus |= B0001;    // Button_1: ON
     Serial.println("Button_1: ON");
     // your code...      
     break;
   case '2':
     buttonStatus &= B1110;    // Button_1: OFF
     Serial.println("Button_1: OFF");
     // your code...      
     break;
   case '3':
     buttonStatus |= B0010;    // Button_2: ON
     Serial.println("Button_2: ON");
     // your code...      
     break;
   case '4':
     buttonStatus &= B1101;    // Button_2: OFF
     Serial.println("Button_2: OFF");
     // your code...      
     break;
   case '5':                    // configured as momentary button
//      buttonStatus |= B0100;     // Button_3: ON
     Serial.println("Button_3: ON");
     // your code...      
     break;
//    case '6':
//      buttonStatus &= B1011;     // Button_3: OFF
//      Serial.println("Button_3: OFF");
//      // your code...      
//      break;
   case '7':                    // configured as momentary button
//      buttonStatus |= B1000;     // Button_4: ON
     Serial.println("Button_4: ON");
     // your code...      
     break;
//    case '8':
//      buttonStatus &= B0111;    // Button_4: OFF
//      Serial.println("Button_4: OFF");
//      // your code...      
//      break;
 }
 sendData(buttonStatus, dataByte);
}

void setJoystick_Int(byte data[5])    {
 // Demo
 int joyX = (data[1]<<7) + data[2];
 int joyY = (data[3]<<7) + data[4];
 joyX = joyX - 200;               // Transmission offset = 110
 joyY = joyY - 200;               // to avoid negative numbers

 Serial.print("Joystick data:  ");
 Serial.print(joyX);  
 Serial.print(", ");  
 Serial.println(joyY);

 // Your code here ...
}

byte GetdataByte()  {
 // Demo
 static byte i=0;
 i +=5;
 if(i >100)    i = 0;
 return i;  

 // Your code ...
}

void sendData(byte button, byte data)    {
 mySerial.write(STX); mySerial.write((button<<2)+4); mySerial.write(data+4); mySerial.write(ETX); // +4: avoid 0x2 & 0x3
}


Buttons 1 & 2 are latch buttons
Buttons 3 & 4 are momentary buttons

kas

I got an interesting comment/suggestion
Quote
What about developing the same type of application, but using the android device built in accelerometers
Control would be performed just by tilting the smartphone/tablet.
The joystick would be a "read only" device, reflecting the device inclination.


This sister application could be called Acc Bluetooth Commander
Any interest ???

balaclava07

Hey

First I want to thank you for this great app and sharing it for free. Second I would like to ask you, why you don't make joystick ball to turn in circle instead of square like it's now. I hope you understand what I mean.

Thank you for the answer


kas

#93
Sep 05, 2013, 07:46 am Last Edit: Sep 05, 2013, 07:49 am by kas Reason: 1
Quote
First I want to thank you for this great app and sharing it for free

Thanks  ;)

Quote
why you don't make joystick ball to turn in circle instead of square like it's now

To get full range at 45°, 135°, 225° and 315°, the joystick moving area HAS to be square.
With a circle, at 45° max values would be 71 (100/1.414) assuming a -100/+100 data range.

Please let me have your phone/tablet model

transfinite

Quote
What about developing the same type of application, but using the android device built in accelerometers
Control would be performed just by tilting the smartphone/tablet.
The joystick would be a "read only" device, reflecting the device inclination.

      This sister application could be called Acc Bluetooth Commander
       Any interest ???



I think that's a great idea.  Or just add an option to the current app to toggle using the accelerometer instead of your finger to control the joystick.

-transfinite

kas

#95
Sep 06, 2013, 08:57 pm Last Edit: Sep 06, 2013, 08:59 pm by kas Reason: 1
Quote
Quote

What about developing the same type of application, but using the android device built in accelerometers
Control would be performed just by tilting the smartphone/tablet.
The joystick would be a "read only" device, reflecting the device inclination.

     This sister application could be called Acc Bluetooth Commander
      Any interest ???


I think that's a great idea.  Or just add an option to the current app to toggle using the accelerometer instead of your finger to control the joystick.

-transfinite


That's another great idea, thanks transfinite
We could call it  "Total BT Commander"   ;)

BTW, any "unexpected behavior" in V3.0 ???

balaclava07



To get full range at 45°, 135°, 225° and 315°, the joystick moving area HAS to be square.
With a circle, at 45° max values would be 71 (100/1.414) assuming a -100/+100 data range.



Yes that's true, but for my Arduino program I'm using for position calculating Pythagorean theorem and because the moving area is square I'm not getting the right numbers. Numbers are too big at 45°, 135°, 225° and 315°.


kas

#97
Sep 09, 2013, 03:59 pm Last Edit: Sep 09, 2013, 04:01 pm by kas Reason: 1
OK...
This feature will be included, as an option, in V3.1

kas

#98
Sep 13, 2013, 06:46 pm Last Edit: Sep 25, 2013, 08:17 am by kas Reason: 1
With range -100/+100, some joystick positions do interfere with ETX (0x3):

X=59, Y=24  is transmitted as  <2  2  3  1  96  3>
X=47, Y=59  is transmitted as  <2  1  119  2  3  3>

In these two examples, a transmission error is generated as ETX is detected earlier than expected
I have modified the Arduino demo sketch to definitely address this situation

AndroLED V10 is attached to this message
I suggest to use the same decoding algorithm in your own program  

EDIT:
replaced   previousMillis = currentMillis - interval*0.9;  
by            previousMillis = currentMillis - interval*0.5;
to avoid possible racing conditions  

please download again (V10.1)

kas

#99
Sep 25, 2013, 08:11 am Last Edit: Jan 17, 2014, 09:06 am by kas Reason: 1
I just realized that the Arduino sketch is not attached to previous post, sorry guys, here it is
EDIT: You must be logged to see the file  :smiley-roll-blue:

some functions have been renamed to more meaningfull names

for this demo
- Button #1:  built in LED control
- Button #2:  enable Debug mode
- Button #3:  configured as momentary
- Button #4:  configured as momentary

EDIT: Please  discard V10.2 and download V10.3 (see reply #129)


kas

#100
Oct 11, 2013, 09:25 pm Last Edit: Oct 11, 2013, 09:28 pm by kas Reason: 1
V 3.1  maintenance version

- Joystick constraint: box or circle option (as per balaclava07 request)
- better algorithm for data reception from Arduino
- various minor fixes

To be uploaded here later today


jooheepark

Hi, kas.

Thank you for sharing this awesome app.
I've tested it with Bluetooth Mate Silver board.
It works perfectly. However you need to change a baud rate of Bluetooth Mate Silver board.
you can find out how to change a baud rate http://www.buildcircuit.com/how-to-change-baud-rate-of-bluetooth-modem-bluesmirf-gold-using-arduino-terminal/.

Thank you!

kas

#102
Oct 29, 2013, 11:05 am Last Edit: Oct 29, 2013, 11:41 am by kas Reason: 1
Quote
Thank you for sharing this awesome app


Thanks jooheepark,
I appreciate the time you spent for registering and sharing your experience
Matching baud rate is indeed a key point for success (together with RX/TX proper wiring)

@all
please report
- bugs (if any ;))
- requested new features

and...  info's regarding your new Bluetooth RC toy

I personally use this App for controlling my balancing robot, using now the MPU-6050 gyro/acc combo


o_lampe

#103
Oct 31, 2013, 08:07 pm Last Edit: Oct 31, 2013, 08:30 pm by o_lampe Reason: 1
Hi kas,
thanks a lot for your time and work on this APP.

I tried to use your BT-scanner and BAUD-rate setting program, but with soft_serial I had no luck on my mega2560 board. I tried several pin combinations and reversed TX/RX , but nothing.
Then I rewrote the program and replaced the soft_serial 'myserial' with 'Serial1' commands, and it worked like a charm.

BTW, if you get bored one time, pleeease write a version for stepper motors as well  ;)

Groeten
Olaf

<edit> It seems, that soft_serial only works up to 57600 BAUD. My BT-module was setup to 115200 BAUD, that's why it didn't work

@all, does anybody know the AT-command to change the PIN-code for the BT module?

kas

#104
Nov 02, 2013, 08:40 am Last Edit: Nov 02, 2013, 10:00 am by kas Reason: 1
Hi o_lampe

Quote
BTW, if you get bored one time, pleeease write a version for stepper
motors as well  smiley-wink


What is your specific application that make you choose steppers over servo's ??


Quote
does anybody know the AT-command to change the PIN-code for the BT
module?


Assuming you have a JY-MCU or compatible board, see here and scroll down to mid page:

AT+PINxxxx  Sets the pairing password of the device
Any 4 digit number can be used, the default pincode is 1234
Response: OKsetPIN


Quote
It seems, that soft_serial only works up to 57600 BAUD. My BT-module was
setup to 115200 BAUD, that's why it didn't work


Using hardware serial, mega2560 board should be OK @115200 baud (and higher with
a third party Serial Monitor)

Go Up