- // 这个例子用来驱动各款DHT温湿度传感器
- // Written by ladyada, public domain
-
- #include "DHT.h"
- #include <Wire.h>
- #include "Adafruit_LEDBackpack.h"
- #include "Adafruit_GFX.h"
-
- Adafruit_BicolorMatrix matrix = Adafruit_BicolorMatrix();
- #define DHTPIN 2 // 定义DHT温湿度传感器连接到的引脚
-
- // Uncomment whatever type you're using!
- #define DHTTYPE DHT11 // DHT 11
- //#define DHTTYPE DHT22 // DHT 22 (AM2302)
- //#define DHTTYPE DHT21 // DHT 21 (AM2301)
-
- // 传感器的1号引脚(在最左边)连接到+5V
- // 传感器的2号引脚连接到DHTPIN所定义的引脚
- // 传感器的4号引脚(在最右边)连接到GND
- // 在传感器的2号引脚(data)和1号引脚(vcc)之间连接一个10K电阻
-
- DHT dht(DHTPIN, DHTTYPE);
-
- void setup() {
- Serial.begin(9600);
- Serial.println("DHTxx test!");
- Serial.println("8x8 LED Matrix Test");
- dht.begin();
- matrix.begin(0x70);
- }
- static const uint8_t PROGMEM
- smile_bmp[] =
- {
- B00111100,
- B01000010,
- B10100101,
- B10000001,
- B10100101,
- B10011001,
- B01000010,
- B00111100 }
- ,
- neutral_bmp[] =
- {
- B00111100,
- B01000010,
- B10100101,
- B10000001,
- B10111101,
- B10000001,
- B01000010,
- B00111100 }
- ,
- frown_bmp[] =
- {
- B00111100,
- B01000010,
- B10100101,
- B10000001,
- B10011001,
- B10100101,
- B01000010,
- B00111100 };
- void loop() {
- // 读取温度或者湿度大约需要250毫秒时间
- // 传感器读取数据也可能超过2秒(它是一个反应速度很慢的传感器)
- float h = dht.readHumidity();
- float t = dht.readTemperature();
- //char
- // 检查返回的数据是否是幼小的,如果是非数字数据,那么就说明出错了。
- if (isnan(t) || isnan(h)) {
- Serial.println("Failed to read from DHT");
- } else {
- Serial.print("Humidity: ");
- Serial.print(h);
- Serial.print(" %\t");
- Serial.print("Temperature: ");
- Serial.print(t);
- Serial.println(" *C");
- }
- matrix.setTextWrap(false); // we dont want text to wrap so it scrolls nicely
- matrix.setTextSize(1);
- matrix.setTextColor(LED_RED);
- matrix.setRotation(3);
- for (int8_t x=7; x>=-72; x--) {
- matrix.clear();
- matrix.setCursor(x,0);
- matrix.print("Temp:");
- matrix.print(t);
- matrix.writeDisplay();
- delay(100);
- }
- }
复制代码
|