Das senden der Daten von arduino über bluetooth serial

Ich versuche momentan das design eines controller, dass würde die Kommunikation über Bluetooth an ein android-Handy (Galaxy Nexus). Ich bin vor ein paar Herausforderungen. Auch ich habe nicht viel praktische Programmiererfahrung.

Den Kern des Controllers befindet sich ein Arduino-mikrocontroller, die scans der Zustand der 8 digital-pins und sechs analoge pins (10 bit) und sendet die Daten über einen seriellen, einem HC-05 Bluetooth-chip.
Das android-Handy sollten dann Lesen Sie die serielle Informationen gesendet über Bluetooth und entweder übertragen Sie das Paket an ein anderes Telefon - Das wird mich eine Weile zu implementieren, wie ich weiß, sehr wenig, wie das internet funktioniert - oder analysieren Sie und interpretieren Sie für das weitere Vorgehen getroffen werden

Was ich verlange sind die Einsicht, der beste Weg zu gehen über das tun dies. Was nun am besten zu bedeuten???
Wir werden ich möchte es wirklich mal so, wenn ich eine Taste drücken oder eine Kombination von Böden, boom, android-Telefon nimmt die Handlung schnell genug für einen Menschen nicht wahrzunehmen, eine Verzögerung.

Dann möchte ich in der Lage zu assoziieren die Informationen, die das Telefon liest den seriellen Puffer auf die entsprechende Schaltfläche oder Analog-pin. In Fall gibt es einen Fehler oder um zu vermeiden, dass Sie fallen out of sync

Hier ist was ich habe, so weit. Ich habe nicht getestet code noch, teilweise, weil ich bin immer noch lernen, wie die Programmierung der android-Teil dieses Projekts, zum Teil, weil ich möchte etwas feedback, ob ich bescheuert bin oder ist dies tatsächlich eine effektive Weg, dies zu tun:

//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

Ist es sinnlos für mich zu tun, bit-Verschiebungen wie diese? Letztlich, wenn ich mich nicht Irre, werden alle Daten gesendet werden als bytes ( 8 bit-Pakete), so wird die Bluetooth.write(analogRead(pin)+testByte) tatsächlich senden zwei bytes oder wird es abgeschnitten den int Daten? wie werden Sie abgebaut und wie kann ich mich erholen, es auf dem android Ende?

Wie würden Sie gehen über die Umsetzung dieser? Irgendwelche Erkenntnisse oder Hinweise?

InformationsquelleAutor Ethienne | 2014-02-06

Schreibe einen Kommentar