我正在使用我朋友的代码建立一个气象站。然而,他是Arduino的老版本,我无法弄清楚原因。我是编程新手,所以我不知道''dht'没有命名类型“是什么意思?我正在使用Arduino 1.04和我的朋友在Arudino 0022上编码他的气象站。我的问题是,为什么我的验证不能编译?我做错了什么?
这是我的代码:
#include <dht.h>
dht DHT;
#define DHT22_PIN 2
#include <Wire.h>
#define BMP085_ADDRESS 0x77 // I2C address of BMP085
const unsigned char OSS = 3; // Oversampling Setting
// Calibration values
int ac1;
int ac2;
int ac3;
unsigned int ac4;
unsigned int ac5;
unsigned int ac6;
int b1;
int b2;
int mb;
int mc;
int md;
// b5 is calculated in bmp085GetTemperature(...), this variable is also used in bmp085GetPressure(...)
// so ...Temperature(...) must be called before ...Pressure(...).
long b5;
short temperature;
long pressure;
void setup()
{
pinMode(2, INPUT);
Wire.begin();
bmp085Calibration();
Serial.begin(115200);
analogReference(INTERNAL);
}
void loop() {
// READ DATA
float bat_read=analogRead(6);
//bat_read = analogRead(BAT_voltage);
float chk = DHT.read22(DHT22_PIN);
// DISPLAY DATA
Serial.print(bat_read, DEC);
Serial.print(", ");
Serial.print(DHT.humidity, 2);
Serial.print(", ");
temperature = bmp085GetTemperature(bmp085ReadUT());
pressure = bmp085GetPressure(bmp085ReadUP());
Serial.print(temperature, DEC);
Serial.print(", ");
Serial.println(pressure, DEC);
delay(1000);
}
错误是:
error: 'dht' does not name a type
sketch_jul05a.ino: In function 'void loop()': error: 'DHT' was not declared in this scope
答案 0 :(得分:1)
关于您应该
的特定错误'dht'未命名类型
这意味着您的单词dht
不在编译器可以理解您的意思的地方。
如何将其更改为某种类型?
您不想将其更改为某种类型,您希望解决您所遇到的编译问题。
类型是什么意思?
您应该阅读K&R上的the subject。类型是一种限制您制作连贯代码的规则。
你应该在你的问题中复制你得到的完整错误,以便我们更好地帮助你。
我们假设错误在以下一行:
#include <dht.h>
dht DHT;
编译器不知道dht
是什么,因为它从未见过以前定义过。我假设,dht.h
应该是要包含的标头,需要包含dht
类型定义。您应该查看文件系统中是否有dht.h
,并将其推送到草图的目录中。最后将#include <dht.h>
更改为#include "dht.h"
,以便在本地目录中查找dht.h
。
答案 1 :(得分:1)
昨天我在使用用Arduino 0022编写的DHT22传感器更新草图时遇到了同样的问题。我试图在Arduino 1.0.5 IDE中编辑它。多个编译错误,特别是函数millis()。我认为如果我刚刚打开0022 IDE,它可能会有效,但我认为是时候将草图带入这十年了;)
我从Github下载了Adafruit DHT22库并稍微重写了草图以使用库的方法。这几乎是昙花一现。库中包含一个示例草图,您可以将其导出以使一切适合。
在我的草图中,我知道了:
#include "DHT.h"
#define DHTPIN 7 // what pin we're connected to on the Arduino UNO
#define DHTTYPE DHT22 // DHT 22 (AM2302)
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(9600);
Serial.println("DHTxx test!");
dht.begin();
}
旧版本和更新版本之间的差异是'dht.begin();'设置块中的语句。
Adafruit库不像我之前使用的库那样强大,特别是在处理错误时,但是在经过45分钟的摆弄之后只是工作。
如果XBee库在未来的Arduino IDE版本中停止运行,可能会帮助我!
答案 2 :(得分:0)
'type'描述了你的变量。像整数(5),字符串(“Hello world”),字符(“a”),布尔值(true或false)或浮点数(3.1415)。
看起来'dht'应该是一种类型,但是arduino IDE不能识别它,因为它不是普通类型。这应该通过导入创建该类型的库来解决。然后arduino可以识别它。
这就是你的“#include”的用武之地。我建议把它改成“#include”dht.h“。
另外,请仔细检查以确保dht.h确实是arduino可以访问的文件。