【发布时间】: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 语句有问题。
有什么建议或意见吗?
【问题讨论】:
-
您的串行读数到底是什么?您有几行在那里打印调试类型信息。当你把它放在水里时,会发生什么变化吗?
-
或者(如果上面的输出总是一样的),你直接连接的时候用了什么代码?