Advertisement
pleasedontcode

"Sensor Control" rev_01

Nov 3rd, 2024
86
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: "Sensor Control"
  13.     - Source Code NOT compiled for: Arduino Uno
  14.     - Source Code created on: 2024-11-04 02:01:26
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* This project focuses on integrating various */
  21.     /* sensors and actuators with Arduino to create a */
  22.     /* responsive environment, enhancing interaction and */
  23.     /* functionality in real-time applications. */
  24. /****** END SYSTEM REQUIREMENTS *****/
  25.  
  26. /* START CODE */
  27.  
  28. /****** DEFINITION OF LIBRARIES *****/
  29. #include <Wire.h>   // Include Wire library for I2C communication
  30. #include <Servo.h>  // Include Servo library for controlling servo motors
  31. #include <DHT.h>    // Include DHT library for temperature and humidity sensor
  32.  
  33. /****** FUNCTION PROTOTYPES *****/
  34. void setup(void);
  35. void loop(void);
  36.  
  37. /****** GLOBAL VARIABLES *****/
  38. DHT dht(2, DHT11); // Initialize DHT sensor on pin 2 with DHT11 type
  39. Servo myServo;     // Create a Servo object
  40.  
  41. void setup(void)
  42. {
  43.     // Initialize serial communication for debugging
  44.     Serial.begin(9600);
  45.    
  46.     // Initialize DHT sensor
  47.     dht.begin();
  48.    
  49.     // Attach the servo on pin 9
  50.     myServo.attach(9);
  51. }
  52.  
  53. void loop(void)
  54. {
  55.     // Read temperature and humidity from DHT sensor
  56.     float humidity = dht.readHumidity();
  57.     float temperature = dht.readTemperature();
  58.  
  59.     // Check if any reads failed and exit early (to try again).
  60.     if (isnan(humidity) || isnan(temperature)) {
  61.         Serial.println("Failed to read from DHT sensor!");
  62.         return;
  63.     }
  64.  
  65.     // Print the values to the Serial Monitor
  66.     Serial.print("Humidity: ");
  67.     Serial.print(humidity);
  68.     Serial.print(" %\t");
  69.     Serial.print("Temperature: ");
  70.     Serial.print(temperature);
  71.     Serial.println(" *C");
  72.  
  73.     // Control the servo based on temperature
  74.     if (temperature > 25) {
  75.         myServo.write(180); // Move servo to 180 degrees if temperature is above 25°C
  76.     } else {
  77.         myServo.write(0);   // Move servo to 0 degrees if temperature is below or equal to 25°C
  78.     }
  79.  
  80.     // Wait a few seconds between measurements
  81.     delay(2000);
  82. }
  83.  
  84. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement