Advertisement
keenan-v1

arduino-lcd-dht11.ino

Oct 6th, 2017
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.29 KB | None | 0 0
  1. #include <TimerOne.h>
  2. #include <DHT_U.h>
  3. #include <DHT.h>
  4. #include <LiquidCrystal.h>
  5.  
  6. LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
  7. DHT dht(2, DHT11);
  8. bool fahrenheit = true;
  9. long duration = 2L * 1000L; // millis
  10. float t = 0.0;
  11. float h = 0.0;
  12.  
  13. void initLCD() {
  14.     lcd.clear();
  15.     lcd.setCursor(0, 0);
  16.     if (fahrenheit)
  17.         lcd.print("Temp        0 *F");
  18.     else
  19.         lcd.print("Temp        0 *C");
  20.     lcd.setCursor(0, 1);
  21.     lcd.print("Humidity    0 %");
  22. }
  23.  
  24. void updateTemp() {
  25.     float newT = dht.readTemperature(false);
  26.     if(!isnan(newT))
  27.         t = newT;
  28.     float newH = dht.readHumidity();
  29.     if(!isnan(newH))
  30.         h = newH;
  31. }
  32.  
  33. void toggleUnits() {
  34.     fahrenheit = !fahrenheit;
  35.     initLCD();
  36.     refreshDisplay();
  37. }
  38.  
  39. void refreshDisplay() {
  40.     printData(fahrenheit ? dht.convertCtoF(t) : t, 0, 11);
  41.     printData(h, 1, 11);
  42. }
  43.  
  44. void setup() {
  45.     lcd.begin(16, 2);
  46.     initLCD();
  47.     dht.begin();
  48.     attachInterrupt(digitalPinToInterrupt(3), toggleUnits, RISING);
  49.     Timer1.initialize(duration * 1000L); // micros
  50.     Timer1.attachInterrupt(refreshDisplay);
  51. }
  52.  
  53. void printData(float d, int r, int o) {
  54.     if(d > 100.0)
  55.         o--;
  56.     lcd.setCursor(o, r);
  57.     lcd.print((int)d);
  58. }
  59.  
  60. void loop() {
  61.     delay(duration);
  62.     updateTemp();
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement