我正试图完成一个小项目,一个水分传感器连接到一个Fio V3。我还将Xbee S1模块附加到Fio的套接字上。
我已经将以下代码上传到Fio:
int igrasia = 7;
void setup()
{
Serial1.begin(9600);
pinMode(igrasia, INPUT_PULLUP);
}
void loop(){
int sensorVal = digitalRead(igrasia);
if (sensorVal == HIGH) {
Serial1.println("0"); // Send OK to xbee
}
else {
Serial1.println("1"); // Send NOT OK to xbee
}
delay(5000);
}在我的电脑上,使用Xbee USB资源管理器,我每5秒收到一次X上的正确数据。零(0)当传感器在一杯水和一(1)当传感器在玻璃杯水。
我想把这些字节读入一个带有LCD屏幕和Xbee屏蔽的Arduino Uno。为此,我已将以下代码上载到Uno:
#include <SPI.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x38,16,2); // set the LCD address to 0x20 for a 16 chars
void setup(){
Serial.begin(9600);
//configure pin2 as an input and enable the internal pull-up resistor
// pinMode(8, INPUT_PULLUP);
pinMode(13, OUTPUT);
digitalWrite(13, LOW);
lcd.init(); // initialize the lcd
}
void loop(){
if(Serial.available())
{
char getData = Serial.read();
if (getData == '1')
{
Serial.print(getData);
digitalWrite(13, HIGH);
lcd.clear();
lcd.setCursor (0,0); // go to start of 1st line
lcd.print("ATTENTION !!!!");
lcd.setCursor (0,1); // go to start of 1st line
lcd.print("WET environment");
}
else {
Serial.print(getData);
digitalWrite(13, LOW);
lcd.clear();
lcd.setCursor (0,0); // go to start of 1st line
lcd.print("dry environment");
lcd.setCursor (0,1); // go to start of 1st line
lcd.print("all looks good!");
}
}
}它不能正常工作:- (我对0有正确的功能,而传感器在水外面。液晶显示器显示“干燥环境”。
但是一旦我把传感器放在水里,液晶显示器就不能按要求工作了。即使我把感应器留在水中,液晶显示器仍然显示“干燥的环境”。
我尝试了传感器直接连接到Uno与液晶显示器连接,它工作!我认为UNO上的serial.read()和/或If /循环语句有问题。
有什么建议或建议吗?
发布于 2014-11-03 01:51:56
当您传输数据时,您是以String "1","0"的形式发送数据。
在接收器上,您正在测试字符'1','0'。Strings以空字符(/u0000)结束,而字符则不是。因此,条件总是失败的。您可以尝试只发送和测试字符。
https://stackoverflow.com/questions/26706583
复制相似问题