
DHT11 and LCD, humidity, temperature, dew point
Dependencies: Freetronics_16x2_LCD mbed
main.cpp
00001 #include "mbed.h" 00002 #include "freetronicsLCDShield.h" 00003 #include "DHT.h" 00004 00005 freetronicsLCDShield lcd(D8, D9, D4, D5, D6, D7, D3, A0); 00006 DHT sensor(A5, DHT11); 00007 DigitalOut myled(LED1); 00008 Ticker flipper; 00009 bool up=true; 00010 void readBP() 00011 { 00012 float bp=lcd.readButton(); 00013 if (bp<0.25) 00014 up=true; 00015 else if (bp<0.5) 00016 up=false; 00017 } 00018 int main() 00019 { 00020 flipper.attach(&readBP, 0.1); 00021 int error = 0; 00022 float h = 0.0f, c = 0.0f, f = 0.0f, k = 0.0f, dp = 0.0f, dpf = 0.0f; 00023 // turn on the back light (it's off by default) 00024 lcd.setBackLight(true); 00025 lcd.cls(); 00026 // print the first line and wait 3 sec 00027 lcd.printf("mbed DHT11"); 00028 wait(3); 00029 00030 // print the counter prefix; the number will be printed in the while loop 00031 00032 00033 00034 00035 while (1) { 00036 00037 error = sensor.readData(); 00038 if (0 == error) { 00039 c = sensor.ReadTemperature(CELCIUS); 00040 f = sensor.ReadTemperature(FARENHEIT); 00041 k = sensor.ReadTemperature(KELVIN); 00042 h = sensor.ReadHumidity(); 00043 dp = sensor.CalcdewPoint(c, h); 00044 dpf = sensor.CalcdewPointFast(c, h); 00045 if(up==true) { 00046 lcd.setCursorPosition(0, 0); 00047 lcd.printf("Temperature %4.2f ",c); 00048 lcd.setCursorPosition(1, 0); 00049 lcd.printf("Humidity %4.2f ", h); 00050 } 00051 if(up==false) { 00052 00053 lcd.setCursorPosition(0, 0); 00054 lcd.printf("Humidity %4.2f ", h); 00055 lcd.setCursorPosition(1, 0); 00056 lcd.printf("Dew point %4.2f ",dp); 00057 } 00058 } else { 00059 00060 lcd.setCursorPosition(0, 0); 00061 lcd.printf("mbed DHT11 "); 00062 lcd.setCursorPosition(1, 0); 00063 lcd.printf("Error: %d ", error); 00064 } 00065 00066 00067 wait(0.5f); 00068 } 00069 }
Generated on Fri Jul 15 2022 00:55:47 by
