3679| 4
|
用Arduino做温湿度显示器是代码出现问题 |
[mw_shl_code=xml,true]// 128x64 I2C OLED #include "DHT.h" // DHT22数字传感器库 #include "U8glib.h" // OLED库 U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_NONE); // I2C #define DHTPIN 2 #define DHTTYPE DHT22 // DHT 22 (AM2302) DHT dht(DHTPIN, DHTTYPE); void draw(void) { u8g.setFont(u8g_font_8x13); //使用8x13大小的字符 u8g.setPrintPos(0, 20); // 对应 x,y 轴值 u8g.print("HUMI(%) :"); u8g.setPrintPos(73, 20); u8g.print((float)DHT22.humidity); u8g.setPrintPos(0, 40); u8g.print("TEMP(oC):"); u8g.setPrintPos(73, 40); u8g.print((float)DHT22.temperature); } void setup() { Serial.begin(9600);//串口波特率 Serial.println("DHT22 TEST PROGRAM "); Serial.print("LIBRARY VERSION: "); Serial.println(DHT22LIB_VERSION); Serial.println(); } void loop() { Serial.println("\n"); int chk = DHT22.read(DHT22PIN); Serial.print("Read sensor: "); switch (chk) { case DHTLIB_OK: Serial.println("OK"); break; case DHTLIB_ERROR_CHECKSUM: Serial.println("Checksum error"); break; case DHTLIB_ERROR_TIMEOUT: Serial.println("Time out error"); break; default: Serial.println("Unknown error"); break; } Serial.print("Humidity (%): "); Serial.println((float)DHT22.humidity,2); Serial.print("Temperature (oC): "); Serial.println((float)DHT22.temperature,2); u8g.firstPage(); do { draw(); } while( u8g.nextPage() ); delay(10000); }[/mw_shl_code] |
© 2013-2024 Comsenz Inc. Powered by Discuz! X3.4 Licensed