skizziks_53

reddit_dht11_attempt

Aug 17th, 2018
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.90 KB | None | 0 0
  1. /*
  2.    August 17, 2018
  3.    DHT library from:
  4.    https://github.com/RobTillaart/Arduino/tree/master/libraries/DHTlib
  5.  
  6.    If you use non-standard Arduino libraries, you should ALWAYS note what library you used, and where you got it from!
  7.    There are many examples of different libraries from different sources, that use the same names.
  8.  
  9.    Also--I don't know if this sketch really works, as I don't have one of these sensors on hand to use.
  10. */
  11.  
  12. #include <dht.h>
  13.  
  14. //dht DHT; // I prefer to differentiate the instance from the class, by more than just capitalization.
  15. dht my_dht; // Like so.
  16.  
  17. int DHT11_PIN = 7;
  18. int FAN = 6;
  19. int GREENLED = 5;
  20. int REDLED = 4;
  21.  
  22. void setup() {
  23.   Serial.begin(9600);
  24.   pinMode(6, OUTPUT);
  25.   pinMode(5, OUTPUT);
  26.   pinMode(4, OUTPUT);
  27. }
  28.  
  29. void loop() {
  30.  
  31.   int chk = my_dht.read11(DHT11_PIN);
  32.   Serial.print("Temperature = ");
  33.   Serial.println(my_dht.temperature);
  34.   Serial.print("Humidity = ");
  35.   Serial.println(my_dht.humidity);
  36.  
  37.   delay(5000);
  38.  
  39.   if (my_dht.temperature >= 24) {
  40.     digitalWrite(FAN, HIGH);
  41.   }
  42.  
  43.   if (my_dht.temperature < 24) {
  44.     digitalWrite(FAN, LOW);
  45.   }
  46.  
  47.   if (digitalRead(FAN) == HIGH) { // This was one error.
  48.     // You were checking the value of FAN (which is set to 6, because it's pin #6) but what you wanted was the pin state of pin #6.
  49.     // Also you were using the (=) when you need to be using (==) to do comparisons.
  50.     digitalWrite(GREENLED, HIGH);
  51.     //digitalWrite(REDLED, LOW); // this could be combined from the below if() statement
  52.   }
  53.   else {
  54.     digitalWrite(GREENLED, LOW);
  55.     //digitalWrite(REDLED, HIGH); // this could be combined from the below if() statement
  56.   }
  57.  
  58.   if (digitalRead(FAN) == LOW) { // Same error as above. And also,,,, you don't really need these as two separate if() statements.
  59.     digitalWrite(REDLED, HIGH);
  60.   }
  61.   else {
  62.     digitalWrite(REDLED, LOW);
  63.   }
  64.  
  65. }
  66.  
  67. // ~~~~~~~~ end ~~~~~~~~~~
Add Comment
Please, Sign In to add comment