Advertisement
Ogomegbunam

Brooder DHT22

Mar 14th, 2024 (edited)
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.34 KB | None | 0 0
  1. #include <SimpleDHT.h>
  2.  
  3. int pinDHT22 = 2;
  4. SimpleDHT22 dht22(pinDHT22);
  5.  
  6. # define cooler 3
  7. # define heater 4
  8.  
  9. int T1;
  10. int T2;
  11.  
  12. int Ti;
  13. int Hi;
  14.  
  15. int H1 = 60;
  16. int H2 = 70;
  17.  
  18. #include <SoftwareSerial.h>
  19. SoftwareSerial B(8,9);
  20.  
  21. void setup() {
  22. // put your setup code here, to run once:
  23. Serial.begin(115200);
  24. B.begin(9600);
  25. pinMode(cooler,OUTPUT);
  26. pinMode(heater,OUTPUT);
  27. }
  28.  
  29. void loop() {
  30. // put your main code here, to run repeatedly:
  31. float temperature = 0;
  32. float humidity = 0;
  33. int err = SimpleDHTErrSuccess;
  34. if ((err = dht22.read2(&temperature, &humidity, NULL)) != SimpleDHTErrSuccess) {
  35. Serial.print("Read DHT22 failed, err="); Serial.print(SimpleDHTErrCode(err));
  36. Serial.print(","); Serial.println(SimpleDHTErrDuration(err)); delay(2000);
  37. return;
  38. }
  39.  
  40. Serial.print("Sample OK: ");
  41. Serial.print((float)temperature); Serial.print(" *C, "); Serial.print(Ti);
  42. Serial.print((float)humidity); Serial.println(" RH%"); Serial.print(Hi);
  43.  
  44. //Temperature and humidity Match
  45. int Ti = ((float)temperature - 1);
  46. int Hi = ((float)humidity - 10);
  47.  
  48. // read the input on analog pin 0:
  49. int sensorValue = analogRead(A0);
  50. // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
  51. int voltage = sensorValue * (5.0 / 1023.0);
  52. // print out the value you read:
  53. Serial.println(voltage);
  54.  
  55. if (voltage == 0){
  56. T1 = 33;
  57. T2 = 35;
  58. }
  59.  
  60. if (voltage == 1){
  61. T1 = 31;
  62. T2 = 33;
  63. }
  64.  
  65. if (voltage == 2){
  66. T1 = 29;
  67. T2 = 31;
  68. }
  69.  
  70. if (voltage == 3){
  71. T1 = 27;
  72. T2 = 29;
  73. }
  74.  
  75. if (voltage == 4){
  76. T1 = 25;
  77. T2 = 27;
  78. }
  79.  
  80. if (voltage == 5){
  81. T1 = 25;
  82. T2 = 27;
  83. }
  84.  
  85. Serial.print("New temperature range");
  86. Serial.print(T2);
  87. Serial.println(T1);
  88. B.print(Ti);
  89. B.print(",");
  90. B.print(Hi);
  91. B.print(",");
  92. B.print(T2);
  93. B.print(",");
  94. B.print(T1);
  95. B.print(",");
  96. B.print(voltage);
  97. B.print(",");
  98. B.print((millis() / 1000));
  99. B.print(";");
  100.  
  101. // TEMPERATURE CONTROL
  102. if (Ti > T2){
  103. digitalWrite (cooler,LOW);
  104. digitalWrite (heater,HIGH);
  105. }
  106.  
  107. if (Ti < T1){
  108. digitalWrite (heater,LOW);
  109. digitalWrite (cooler,HIGH);
  110. }
  111. if (Hi < H1){
  112. // digitalWrite (heater,HIGH);
  113. digitalWrite (cooler,HIGH);
  114. }
  115. if (Hi > H2){
  116. // digitalWrite (heater,HIGH);
  117. digitalWrite (cooler,LOW);
  118. }
  119.  
  120. delay(2000);
  121. }
Tags: DHT22
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement