【问题标题】:Arduino Serial.read() from xbee if statement来自 xbee if 语句的 Arduino Serial.read()
【发布时间】:2014-11-03 01:37:09
【问题描述】:

我正在尝试使用连接到 Fio V3 的湿度传感器来完成一个小项目。 我还在 Fio 的插座上连接了一个 Xbee S1 模块。

我已将以下代码上传到 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-CTU 上接收到正确的数据。 当传感器在一杯水之外时为零 (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 功能,而传感器在水外。液晶显示器显示“干燥环境”。

但是,一旦我将传感器放入水中,LCD 就无法按要求工作。 即使我将传感器留在水中,LCD 仍会显示“干燥环境”。

我尝试将传感器直接连接到带有 LCD 的 Uno,它可以工作! 我想 UNO 上的 serial.read() 和/或我的 If / loop 语句有问题。

有什么建议或意见吗?

【问题讨论】:

  • 您的串行读数到底是什么?您有几行在那里打印调试类型信息。当你把它放在水里时,会发生什么变化吗?
  • 或者(如果上面的输出总是一样的),你直接连接的时候用了什么代码?

标签: arduino xbee


【解决方案1】:

当您传输数据时,您将其作为String "1""0" 发送。

在接收器上,您正在测试字符 '1''0'Strings 以空字符 (/u0000) 结尾,而字符则不是。因此条件总是失败。您可以尝试仅传输和测试字符。

【讨论】:

  • 将 0/1 替换为 a/b 即可。但是,不适用于 IF else 语句。我尝试了 2 个 IF,效果很好!对您的专业水平印象深刻!
猜你喜欢
  • 1970-01-01
  • 2021-10-17
  • 1970-01-01
  • 2017-01-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-04
  • 1970-01-01
相关资源
最近更新 更多