Advertisement
pleasedontcode

Distance rev_01

Dec 2nd, 2023
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /********* Pleasedontcode.com **********
  2.  
  3.     Pleasedontcode thanks you for automatic code generation! Enjoy your code!
  4.  
  5.     - Terms and Conditions:
  6.     You have a non-exclusive, revocable, worldwide, royalty-free license
  7.     for personal and commercial use. Attribution is optional; modifications
  8.     are allowed, but you're responsible for code maintenance. We're not
  9.     liable for any loss or damage. For full terms,
  10.     please visit pleasedontcode.com/termsandconditions.
  11.  
  12.     - Project: Distance
  13.     - Source Code compiled for: Arduino Uno
  14.     - Source Code created on: 2023-12-02 18:56:42
  15.     - Source Code generated by: Swetha
  16.  
  17. ********* Pleasedontcode.com **********/
  18. /****** DEFINITION OF LIBRARIES *****/
  19. #include <DHT.h>
  20.  
  21. /****** SYSTEM REQUIREMENT 1 *****/
  22. const int LED_PIN = 13; // LED pin
  23.  
  24. /****** FUNCTION PROTOTYPES *****/
  25. void setup(void);
  26. void loop(void);
  27.  
  28. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  29. const uint8_t SENSOR_DHT11_DOUT_PIN = 2; // DHT11 sensor pin
  30.  
  31. /****** DEFINITION OF LIBRARIES CLASS INSTANCES *****/
  32. DHT dht(SENSOR_DHT11_DOUT_PIN, DHT11); // DHT sensor instance
  33.  
  34. void setup(void)
  35. {
  36.     pinMode(LED_PIN, OUTPUT); // Set LED pin as output
  37.  
  38.     dht.begin(); // Initialize DHT sensor
  39.     Serial.begin(9600); // Initialize serial communication
  40. }
  41.  
  42. void loop(void)
  43. {
  44.     digitalWrite(LED_PIN, HIGH); // Turn on LED
  45.  
  46.     // Read humidity and temperature from the DHT sensor
  47.     float humidity = dht.readHumidity();
  48.     float temperature = dht.readTemperature();
  49.  
  50.     // Check if the readings are valid
  51.     if (isnan(humidity) || isnan(temperature))
  52.     {
  53.         Serial.println(F("Failed to read from DHT sensor!"));
  54.     }
  55.     else
  56.     {
  57.         Serial.print(F("Humidity: "));
  58.         Serial.print(humidity);
  59.         Serial.print(F("% Temperature: "));
  60.         Serial.print(temperature);
  61.         Serial.println(F("°C"));
  62.     }
  63.  
  64.     digitalWrite(LED_PIN, LOW); // Turn off LED
  65.  
  66.     delay(2000); // Wait for 2 seconds before taking another reading
  67. }
  68.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement