Advertisement
Vasil_Vasilev

Arduino

Jun 25th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.28 KB | None | 0 0
  1. #include <Adafruit_Sensor.h>
  2. #include <DHT.h>
  3.  
  4. //#include <DHT_U.h>
  5.  
  6. //#include <DHT_U.h>
  7.  
  8. #define DHTPIN 2 // what digital pin we're connected to
  9.  
  10.  
  11. #define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
  12.  
  13.  
  14. // Connect pin 1 (on the left) of the sensor to +5V
  15. // NOTE: If using a board with 3.3V logic like an Arduino Due connect pin 1
  16. // to 3.3V instead of 5V!
  17. // Connect pin 2 of the sensor to whatever your DHTPIN is
  18. // Connect pin 4 (on the right) of the sensor to GROUND
  19. // Connect a 10K resistor from pin 2 (data) to pin 1 (power) of the sensor
  20.  
  21.  
  22. DHT dht(DHTPIN, DHTTYPE);
  23.  
  24.  
  25. void setup() {
  26. Serial.begin(9600);
  27. Serial.println("DHTxx test!");
  28.  
  29. dht.begin();
  30. }
  31.  
  32.  
  33. int tempsensorValue=0;int compareValue=1022;
  34. int p=0;int result;
  35.  
  36. void loop() {
  37. // Wait a few seconds between measurements.
  38. delay(2000);
  39.  
  40. // Reading temperature or humidity takes about 250 milliseconds!
  41. // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  42. float h = dht.readHumidity();
  43. // Read temperature as Celsius (the default)
  44. float t = dht.readTemperature();
  45.  
  46.  
  47. // Check if any reads failed and exit early (to try again).
  48. if (isnan(h) || isnan(t) ) {
  49. Serial.println("Failed to read from DHT sensor!");
  50. return;
  51. }
  52.  
  53.  
  54. // Compute heat index in Celsius (isFahreheit = false)
  55. float hic = dht.computeHeatIndex(t, h, false);
  56. int sensorValue = analogRead(A0);
  57. Serial.print(sensorValue);
  58. if(p!=5){
  59. result=(result+sensorValue);
  60. Serial.print(" %\t");
  61. Serial.print(result);
  62. }
  63. if(p==5)
  64. {
  65. result=result/p;
  66. Serial.print(" %\t");
  67. Serial.print(result);
  68. if(compareValue-200>result){
  69. Serial.print(" %\t");
  70. Serial.print("Rain");
  71. compareValue=result;
  72. }
  73. if(compareValue+50<result){
  74. Serial.print(" %\t");
  75. Serial.print("Drying");
  76. compareValue=result;
  77. }
  78. result=1022;
  79. tempsensorValue=0;
  80. p=0;
  81. }
  82. p++;
  83. Serial.print(" %\t");
  84. Serial.print(p);
  85. Serial.print("Humidity: ");
  86. Serial.print(h);
  87. Serial.print(" %\t");
  88. Serial.print("Temperature: ");
  89. Serial.print(t);
  90. Serial.print(" *C ");
  91. Serial.print(" %\t");
  92. Serial.print("Heat index: ");
  93. Serial.print(hic);
  94. Serial.print(" *C ");
  95. Serial.print(" %\n");
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement