Animum

TempHumidityMonitor

Oct 19th, 2020
852
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //Autor : Animum
  2. //Version : Beta 1
  3. //platform : Arduino Nano
  4. #include <math.h>
  5. #include <Wire.h>
  6. #include <BH1750.h>
  7. #include <DHT.h>
  8. #include <DallasTemperature.h>
  9. #include <OneWire.h>
  10. #include <LiquidCrystal.h>
  11.  
  12. //define LCD pins
  13. LiquidCrystal lcd(6, 7, 9, 10, 11, 12);
  14.  
  15. //define DS18B20 pin - use D4
  16. OneWire oneWire(4);
  17. DallasTemperature sensors(&oneWire);
  18.  
  19. //define AM2302(DHT22) pin - use D5
  20. DHT SenDHT(5, DHT22);
  21.  
  22. //define PH1750 sensors
  23. BH1750 lightMeter;
  24.  
  25. //declaration of a LCD String
  26. String Row1, Row2;
  27.  
  28. //declaration of a sensors variables
  29. float Temp1 = 0;
  30. float Hum = 0;
  31. float Temp2 = 0;
  32. word Lux = 0;
  33.  
  34. //declaration of a mathematical variable
  35. double DewPo = 0;
  36. double Temppom = 0;
  37. double Mathpom = 0;
  38.  
  39. //declare Menu position byte
  40. byte Menu = 0;
  41.  
  42. void setup() {
  43.   // put your setup code here, to run once:
  44.   // lcd init
  45.   lcd.begin(16, 2);
  46.   sensors.begin();
  47.   //LCD backlight pin
  48.   pinMode(8, OUTPUT);
  49.   digitalWrite(8, HIGH);
  50.   //DHT sensor init
  51.   SenDHT.begin();
  52.   //BH1750 init
  53.   lightMeter.begin();
  54.   //button pin
  55.   pinMode(2,INPUT_PULLUP);  //button 1 - Incrase screen roll
  56.   pinMode(3,INPUT_PULLUP);  //button 2 - Decrase screen roll
  57.   //default Led pin
  58.   pinMode(LED_BUILTIN, OUTPUT);
  59. }
  60.  
  61. void loop() {
  62.   // put your main code here, to run repeatedly:
  63.   //Incrase Menu position
  64.   if (digitalRead(2) == false)
  65.   {
  66.     digitalWrite(LED_BUILTIN, HIGH);
  67.     while (digitalRead(2) == false)
  68.     {
  69.       delay(1);    
  70.     }
  71.     if (Menu < 2)
  72.     {
  73.       ++Menu;
  74.     }
  75.     else
  76.     {
  77.       Menu = 0;    
  78.     }
  79.   }
  80.   //Decrase Menu position
  81.   if (digitalRead(3) == false)
  82.   {
  83.     digitalWrite(LED_BUILTIN, HIGH);
  84.     while (digitalRead(3) == false)
  85.     {
  86.       delay(1);    
  87.     }
  88.     if (Menu > 0)
  89.     {
  90.       --Menu;
  91.     }
  92.     else
  93.     {
  94.       Menu = 2;    
  95.     }
  96.   }
  97.   digitalWrite(LED_BUILTIN, LOW);
  98.   //reading sensors
  99.   Temp1 = SenDHT.readTemperature();
  100.   Hum = SenDHT.readHumidity();
  101.   sensors.requestTemperatures();
  102.   Temp2 = sensors.getTempCByIndex(0);
  103.   Lux = lightMeter.readLightLevel();
  104.   //clearing Row1  and Row2
  105.   Row1, Row2 = " ";
  106.   //LCD screen create
  107.   if (Menu == 0)
  108.   {
  109.     Row1 = "Temp : " + String(Temp1, 2) + char(223) + "C";
  110.     Row2 = "Hum : " + String(Hum, 2) + "%";
  111.   }
  112.   else if (Menu == 1)
  113.   {
  114.     Row1 = "Temp : " + String(Temp2, 2) + char(223) + "C";
  115.     Row2 = "Lux : " + String(Lux);
  116.   }
  117.   else if (Menu == 2)
  118.   {
  119.     Temppom = (17.67 * Temp1) / (243.5 + Temp1);
  120.     Mathpom = (Hum/100) * (exp(Temppom));
  121.     Mathpom = log(Mathpom);
  122.     DewPo = (243.5 * Mathpom) / (17.67 - Mathpom);
  123.     Row1 = "Dew point :";
  124.     Row2 = String(DewPo,2) + char(223) + "C";
  125.   }
  126.   else
  127.   {
  128.     Row1 = "Error";
  129.     Row2 = "Error";
  130.   }
  131.   //view LCD screen
  132.   lcd.clear();
  133.   lcd.print(Row1);
  134.   lcd.setCursor(0, 1);
  135.   lcd.print(Row2);
  136.   //waiting 2s
  137.   delay(2000);
  138.  }
RAW Paste Data