Android 通过蓝牙&x2B;从Arduino接收大整数;安卓&x2B;appinventor

Android 通过蓝牙&x2B;从Arduino接收大整数;安卓&x2B;appinventor,android,bluetooth,arduino,Android,Bluetooth,Arduino,我正在使用arduino和appinventor开发一个简单的RPM感应。我面临着一个问题,Android无法从Arduino BT接收大于255的整数值或大量垃圾 arduino的逻辑已经非常有效了。它在16x2 LCD上正确显示RPM,精度很高。 我还想用appinventor界面通过蓝牙向android发送RPM。它可以工作,但最多只显示255个值。即使当LCD显示1000+转/分时,android上的显示也总是在0到255之间 我尝试过使用类型转换(将其转换为字符,并将其打印为文本,但没

我正在使用arduino和appinventor开发一个简单的RPM感应。我面临着一个问题,Android无法从Arduino BT接收大于255的整数值或大量垃圾

arduino的逻辑已经非常有效了。它在16x2 LCD上正确显示RPM,精度很高。 我还想用appinventor界面通过蓝牙向android发送RPM。它可以工作,但最多只显示255个值。即使当LCD显示1000+转/分时,android上的显示也总是在0到255之间

我尝试过使用类型转换(将其转换为字符,并将其打印为文本,但没有成功)。还更改了appinventor上接收的字节(从1字节更改为4字节)

Arduino的代码:

// include libraries
#include <LiquidCrystal.h>

const int resolution = 500;
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

//hardware global constants;
const int inputPin = 7;
const int ledPin = 13;
//const int bluetoothTx = 9;
//const int bluetoothRx = 8;

//logical variables
int inputState = 0;
int lastPinState;
int counter = 0;
int timeI = 0;
int timeF = 0;
int revolutions;
int cilinderRatio = 2;
int rpm = 0;
char * data = 0;
//---------------------------------------
//BLUETOOTH RESERVED

void setup() {
  //set up input Pin and integrated led (pin13)
  pinMode(inputPin, INPUT);
  pinMode(ledPin, OUTPUT);
  // set up the LCD's number of columns and rows: 
  lcd.begin(16, 2);
  // Print a message to the LCD.
  lcd.print("RPM:");
  //Setup Bluetooth Serial Connection to Android
  //pinMode(bluetoothTx, OUTPUT);
  //pinMode(bluetoothRx, INPUT);
  Serial.begin(9600);

}

void loop() {
  rpm = 0;
  revolutions = 0;
  counter = 0;
  // set the cursor to column 0, line 1
  // (note: line 1 is the second row, since counting begins with 0):
  lcd.setCursor(0, 1);
  timeI = millis(); //get current running time
  do{
    //get current state
    inputState = digitalRead(inputPin);
    //if a pulse occurs, continues
    if(inputState == HIGH) {
    //if last state is not equal to last state, count it increasing 1 
    if(lastPinState != inputState)
    {
      counter += 1;
    }
  } 
  lastPinState = inputState; // takes current input State as last state
  timeF = millis(); //get final execution time
 }while((timeF - timeI) < resolution); //Repeat until reach resolution sample
 //lcd.print(counter); //Print how much pulses was counted
 // Revolutions is how much resolutions occured in one minute
 //since resolution is a seconds fraction, 1000mS * 60S must be
 //considered for RPMs 
 revolutions = counter * (60000 / resolution);
 //lcd.print(revolutions); //Print how much turns was counted, in fact
 //Revolutions divided by cilinderRatio is equal to RPM
 rpm = revolutions / cilinderRatio;
 rpm = rpm / 2;
 lcd.print(rpm); //Print Revolutions per minute
 lcd.print("                ");
 Serial.write(rpm);
 rpm = 0;
 counter = 0;
 digitalWrite(ledPin, LOW);
}
//包含库
#包括
常数int分辨率=500;
//使用接口引脚的编号初始化库
液晶显示器(12,11,5,4,3,2);
//硬件全局常数;
常量int inputPin=7;
常数int ledPin=13;
//常数int bluetoothTx=9;
//常数int bluetoothRx=8;
//逻辑变量
int inputState=0;
int-lastPinState;
int计数器=0;
int-timeI=0;
int-timeF=0;
整数转;
int-cilinderRatio=2;
int rpm=0;
char*data=0;
//---------------------------------------
//保留蓝牙
无效设置(){
//设置输入引脚和集成led(引脚13)
pinMode(输入pin,输入);
引脚模式(LED引脚,输出);
//设置LCD的列数和行数:
lcd.begin(16,2);
//将消息打印到LCD。
lcd.打印(“RPM:”);
//设置与Android的蓝牙串行连接
//pinMode(蓝牙TX,输出);
//pinMode(蓝牙接收,输入);
Serial.begin(9600);
}
void循环(){
rpm=0;
转数=0;
计数器=0;
//将光标设置为第0列第1行
//(注意:第1行是第二行,因为计数从0开始):
lcd.setCursor(0,1);
timeI=millis();//获取当前运行时间
做{
//获取当前状态
inputState=数字读取(inputPin);
//如果出现脉冲,则继续
如果(输入状态==高){
//如果最后一个状态不等于最后一个状态,则将其按1递增
如果(lastPinState!=inputState)
{
计数器+=1;
}
} 
lastPinState=inputState;//将当前输入状态作为最后一个状态
timeF=millis();//获取最终执行时间
}while((timeF-timeI)<分辨率);//重复,直到达到分辨率样本
//lcd.print(计数器);//打印计数的脉冲数
//转数是一分钟内发生的分辨率
//由于分辨率是秒的分数,因此必须为1000mS*60S
//考虑使用RPM
转数=计数器*(60000/分辨率);
//lcd.print(转数);//实际上,打印转数是多少
//转数除以传动比等于RPM
rpm=转数/转速比;
rpm=rpm/2;
lcd.print(rpm);//每分钟打印转数
lcd.打印(“”);
串行写入(rpm);
rpm=0;
计数器=0;
数字写入(ledPin,低电平);
}

注意:我已将接收字节更改为2,以管理比255大的更好的整数。然而,它很容易显示超过255的大量垃圾。

根据Arduino doc关于串行写入的说明。Serial.Write一次发送一个字节或一系列字节。因此,您需要将整数RPM转换为数字字符串 使用itoa或类似的东西


您也可以使用Serial.print()直接将整数作为数字发送。

我通过分隔每个字节解决了这个问题

首先,定义了一个2字节类型的数据数组大小:

byte data[2];
然后,下面的表达式将rpm的每个字节放在一个字节中:

data[0] = (byte) rpm; //typecast of rpm integer to byte
data[0] = ((byte) rpm >> 8); //typecast and 8-bit shift

itoa在Crystal Display library中的使用效果不太好。我认为您的答案中有输入错误,第二行应该是数据[1]=((字节)rpm>>8);