android-通过蓝牙串行从arduino发送数据
作者:互联网
我目前正在尝试设计一种控制器,该控制器可以通过蓝牙与Android手机(Galaxy Nexus)进行通讯.我面临一些挑战.另外,我没有太多实际的编程经验.
控制器的核心是一个Arduino微控制器,该微控制器扫描8个数字引脚和6个模拟引脚(10位)的状态,然后通过串行将数据发送到HC-05蓝牙芯片.
然后,Android手机应读取通过蓝牙发送的串行信息,然后将数据包传输到另一部手机-这将花一些时间来实施,因为我对互联网的工作原理了解甚少-或对其进行分析和解释以采取进一步的措施已采取
我要问的是如何做到这一点的最佳见识.现在最好是什么意思?
我们希望它是实时的,因此当我按下按钮或底部,围栏组合时,Android手机会采取足够快的动作,以至于人类不会察觉到延迟.
然后,我希望能够将手机在串行缓冲区中读取的信息关联到相应的按钮或模拟引脚.如果出现错误或避免它们不同步
这是我到目前为止所拥有的.我尚未测试此代码,部分是因为我仍在学习如何对该项目的android部分进行编程,部分是因为我想获得有关我是否很傻还是这实际上是一种行之有效的方法的反馈这个:
//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
对我来说,像这样进行位移是没有意义的吗?最终,如果我没记错的话,所有数据都以字节(8位数据包)的形式发送,那么Bluetooth.write(analogRead(pin)testByte)实际上会发送两个字节还是会截断int数据?如何将其分解,如何在Android端进行恢复?
您将如何实施呢?有什么见解或建议吗?
解决方法:
您正在学习这真是太好了!一些建议:
如果您将代码隔开一些空间,尤其是通过添加换行符,则代码将更易于阅读,理解和维护.
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);
}
}
此外,您可以将移位运算符与非1值一起使用.因此,您只需使用一个简单变量即可,而不是保留随后添加的移位遮罩.一种更经典的方式来表达您在第一个循环中所表达的内容将是这样的:
#define PIN_OFFSET 2
for ( int i=0; i < 8; i++) {
if (digitalRead( i+PIN_OFFSET)== HIGH)
state += (1 << i);
}
第二个循环可以类似地完成:
for( int pin = 0; pin < 6; pin++) {
short testByte = 0x8000 >> pin;
short result = analogRead(pin) + testByte;
Bluetooth.write( result >> 8);
Bluetooth.write( result & 0xFF);
}
请注意,Bluetooth.write()调用发送一个字节,因此此代码首先发送最高有效字节,然后发送最低字节.
最后,您可能希望在loop()开始时将状态变量清零-否则,一旦将其置位就永远不会清除.
您可能需要考虑发送到手机的内容.这将是二进制数据,并且可能很难处理-您如何知道开始和结束,通常一个值将被误解为控制字符,使您浪费大量时间.考虑将其更改为人类可读的格式化字符串,并在末尾添加换行符.
希望对您有所帮助.
标签:arduino,arduino-ide,bluetooth,microcontroller,android 来源: https://codeday.me/bug/20191122/2057410.html