Sending data from arduino through bluetooth serial

Ethienne picture Ethienne · Feb 6, 2014 · Viewed 18.4k times · Source

I’m currently trying to design a controller that would communicate through Bluetooth to an android phone (Galaxy Nexus). I’m facing a few challenges. Also, I don’t have much practical programming experience.

At the core of the controller resides an Arduino microcontroller who scans the state of 8 digital pins and six analogue pins (10 bits) and sends the data through serial, to an HC-05 Bluetooth chip. The android phone should then read the serial information sent through Bluetooth and either transmit the packet to another phone - That will take me a while to implement as I know very little of how the internet works - or analyse and interpret it for further action to be taken

What I’m asking for are insight as to the best way to go about doing this. Now what does best mean? We’ll I want it to be real time so when I press a button or a combination of bottoms, boom, the android phone takes action fast enough for a human not to perceive a delay.

Then I want to be able to associate the information the phone reads in the serial buffer to the corresponding button or analogue pin. In case there is an error or to avoid that they fall out of sync

Here is what I have so far. I have not tested this code yet, partly because I’m still learning how to program the android part of this project, partly because I want some feedback as to whether I’m being silly or this is actually an effective way to go about doing this:

//Initialization
/*

This Program has three functions:
1) Scan 8 digital pins and compile their state in a single byte ( 8 bits)
2) Scans 6 Analogue inputs corresponding to the joysticks and triggers 
3) Send this info as packets through seril --> Bluetooth 

*/
#include <SoftwareSerial.h>// import the serial software library

#define A = 2
#define B = 3
#define X = 4
#define Y = 5
#define LB = 6
#define RB = 7
#define LJ = 8
#define RJ = 9
#define RX = 10
#define TX = 11
//Pin 12 and 13 are unused for now
SoftwareSerial Bluetooth(RX,TX); //Setup software serial using the defined constants
// declare other variables
byte state = B00000000

void setup() {
  //Setup code here, to run once:
  //Setup all digital pin inputs
  pinMode(A,INPUT);
  pinMode(B,INPUT);
  pinMode(X,INPUT);
  pinMode(Y,INPUT);
  pinMode(LB,INPUT);
  pinMode(RB,INPUT);
  pinMode(LJ,INPUT);
  pinMode(RJ,INPUT);
  //Setup all analogue pin inputs
  //setup serial bus and send validation message
  Bluetooth.begin(9600);
  Bluetooth.println("The controller has successfuly connected to the phone")
}

void loop() {
  //Main code here, to run repeatedly: 
  //Loop to sum digital inputs in a byte, left shift the byte every time by 1 and add that if the pin is high
  for(byte pin = 2, b = B00000001;  pin < 10; pin++, b = b << 1){ if (digitalRead(pin)== HIGH) state += b; }
  //Send digital state byte to serial
  Bluetooth.write(state);
  //Loop to read analgue pin 0 to 5 and send it to serial
  for( int pin = 0, testByte = 0x8000; pin < 6 ; pin++, testByte = testByte >> 1) { Bluetooth.write(analogRead(pin)+testByte); }
}

//Adding some validation would be wise. How would I go about doing that?
// Could add a binary value of 1000_0000_0000_0000,  0100_0000_0000_0000,  0010_0000_0000_0000 ... so on and then use a simple AND statement at the other end to veryfy what analogue reading the info came from
// so say the value for analgue is 1023 and came from analgue pin 1 I would have 0100_0011_1111_1111 now using an bitwise && I could check it against 0100_0000_0000_0000 if the result is 0100_0000_0000_0000 then I know this is the analogu reading from pin 1
//could easily implement a test loop with a shiting bit on the android side

Is it pointless for me to be doing bit shifts like this? Ultimately, if I’m not mistaken, all data is sent as bytes ( 8 bits packets) so will the Bluetooth.write(analogRead(pin)+testByte) actually send two bytes or will it truncate the int data? how will it be broken down and how can I recuperate it on the android end?

How would you go about implementing this? Any insights or words of advice?

Answer

bobwki picture bobwki · Feb 7, 2014

It's great that you are learning this! Some suggestions:

Your code will be easier to read, understand and maintain if you space it out a bit, particularly by adding newlines...

void loop() {
  //Main code here, to run repeatedly: 
  //Loop to sum digital inputs in a byte, 
  //left shift the byte every time by 1 and add that  if the pin is high
  for( byte pin = 2, b = B00000001;  pin < 10; pin++, b = b << 1) {
    if (digitalRead(pin)== HIGH)
      state += b;
  }
  //Send digital state byte to serial
  Bluetooth.write(state);
  //Loop to read analgue pin 0 to 5 and send it to serial
  for( int pin = 0, testByte = 0x8000; pin < 6 ; pin++, testByte = testByte >> 1) {
    Bluetooth.write(analogRead(pin)+testByte); 
  }
}

Also, you can use the shift operators with values other than one. so instead of keeping a shift mask that you then add in, you just use the a simple variable. A more classic way to do what you expressing in the first looop would be something like this:

#define PIN_OFFSET 2
  for ( int i=0; i < 8; i++) {
    if (digitalRead( i+PIN_OFFSET)== HIGH)
      state += (1 << i);
  }

The second loop could be done similarly:

  for( int pin = 0; pin < 6; pin++) {
    short testByte = 0x8000 >> pin;
    short result =  analogRead(pin) + testByte;
    Bluetooth.write( result >> 8); 
    Bluetooth.write( result & 0xFF); 
  }

Note that the Bluetooth.write() call sends a byte, so this code sends first the most significant byte, then the least.

Lastly, you probably want to zero your state variable at be beginning of loop() -- otherwise, once a bit is set it will never get cleared.

You may want to think about what you are sending to the phone. It will be binary data, and that can be difficult to deal with -- how do you know the start and end, and often a value will be misinterpretted as a control character messing you up big time. Consider changing it into a formatted, human readable string, with a newline at the end.

I hope that helps.