title: Sending data from arduino through bluetooth serial
body: <p>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.</p>
  
  <p>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</p>
  
  <p>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.</p>
  
  <p>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</p>
  
  <p>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: </p>
  
  <pre><code>//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 --&gt; Bluetooth 
  
  */
  #include &lt;SoftwareSerial.h&gt;// 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 &lt; 10; pin++, b = b &lt;&lt; 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 &lt; 6 ; pin++, testByte = testByte &gt;&gt; 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 &amp;&amp; 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
  </code></pre>
  
  <p>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 <code>Bluetooth.write(analogRead(pin)+testByte)</code> actually send two bytes or will it truncate the <code>int</code> data? how will it be broken down and how can I recuperate it on the android end? </p>
  
  <p>How would you go about implementing this? Any insights or words of advice?</p>