Android 和 Arduino 都通过蓝牙接收奇怪的值 [英] Android and Arduino both receiving strange values via Bluetooth

查看:30
本文介绍了Android 和 Arduino 都通过蓝牙接收奇怪的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Android 端使用 BluetoothChat 代码通过聊天框向 Arduino UNO 发送开/关信号(我正在发送 110、119 或其他定义如下的情况),但是当我显示接收到的值时,它在 Arduino 串行监视器中显示奇怪的值,即 255、254、250 或 245,并且在 Android 端也用奇怪的值回复.

I am using BluetoothChat code on the Android side to send an ON/OFF signal (I am sending 110, 119 or other cases as defined below) to the Arduino UNO using the chat box, but when I display the received value, it shows strange values in the Arduino Serial monitor, that is, 255, 254, 250 or 245 and replies with strange values on the Android side too.

但令人惊讶的是,当我从 android 发送这些命令并且没有其他指令/LED 正确执行/打开/关闭时,110、119 等中只有一个条件成立.

but suprisingly only single condition among 110, 119, etc gets true when i send these commands from android and no other instruction/LED gets properly executed/turned ON/off.

谁能帮我找出问题所在?下面是我的arduino代码.

Can any one help me figure out the problem? Below is my arduino code.

int data; 
String message; 

void setup () 
{ 
Serial.begin (57600); 

pinMode (6, OUTPUT); 
pinMode (7, OUTPUT); 
pinMode (8, OUTPUT); 
pinMode (9, OUTPUT); 
pinMode (10, OUTPUT); 

digitalWrite(6, LOW); // off LED 1 at pin6 
digitalWrite(7, LOW); // off LED 2 at pin7 
digitalWrite(8, LOW); // off LED 3 at pin8 
digitalWrite(9, LOW); // off LED 4 at pin9 
digitalWrite(10, LOW); // off LED 5 at pin10 
}

void loop () { 
int i=0; 
if (Serial.available()>0) 
{ 
int data= Serial.read(); 

Serial.println (" Received data: "); 
Serial.println (data); 

if (data==110) //q 
{ 
digitalWrite(6, HIGH); // turn on LED 1 at pin6 
message = "LED 1 On"; 
Serial.println (message); 

} 
if (data==102) // a 
{ 
digitalWrite(6, LOW); // off LED 1 at pin6 
message = "LED 1 Off"; 
Serial.println (message); 
} 

if (data==96) //c 
{ 
digitalWrite(7, HIGH); //on LED 2 
message = "LED 2 On"; 
Serial.println (message); 
} 

if (data==115) //d 
{ 
digitalWrite(7, LOW); // off LED 2 
message = "LED 2 Off"; 
Serial.println (message); 
} 

if (data==111) //e 
{ 
digitalWrite(8, HIGH); //on LED 3 
message = "LED 3 On"; 
Serial.println (message); 
} 

if (data==97)// f 
{ 
digitalWrite(8, LOW); //sets the LED 3 
message = "LED 3 Off"; 
Serial.println (message); 
} 

if (data==107) //g 
{ 
digitalWrite(9, HIGH); //turn on LED 4 
message = "LED 4 On"; 
Serial.println (message); 
} 

if (data==120)//h 
{ 
digitalWrite(9, LOW); //sets the LED 4 
message = "LED 4 Off"; 
Serial.println (message); 
} 


if (data==99) //i 
{ 
digitalWrite(10, HIGH); //sets the LED 5 
message = "L5 high"; 
Serial.println (message); 
} 

if (data==104) //j 
{ 
digitalWrite(10, LOW); //sets the LED 5 
message = "LED 5 Off"; 
Serial.println (message); 
} 

if (data==112) //y 
{ 
digitalWrite(6, HIGH); // turn on ALL LEDs 
digitalWrite(7, HIGH); 
digitalWrite(8, HIGH); 
digitalWrite(9, HIGH); 
digitalWrite(10, HIGH); 
message = "ALL LEDs ON"; 
Serial.println (message); 
} 


if (data==122)//z 
{ 
digitalWrite(6, LOW); //Turn off all LEDs 
digitalWrite(7, LOW); 
digitalWrite(8, LOW); 
digitalWrite(9, LOW); 
digitalWrite(10, LOW); 
Serial.println ("ALL LEDs OFF"); 
} 

else 
{ 
digitalWrite(13, HIGH); //sets the LED 5 
delay(3000); //3sec 
digitalWrite(13, LOW); 
delay(3000); 
Serial.println (" else case executed "); 

} ////end of else 

}
data=0;
}

推荐答案

Try char data = Serial.read();

并显示适用于 Android 的 Java 代码.

And show Java code for Android.

查看 蓝牙 Arduino&Android

这篇关于Android 和 Arduino 都通过蓝牙接收奇怪的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆