Advertisement
pleasedontcode

**Sensor Control** rev_01

Nov 3rd, 2024
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: **Sensor Control**
  13.     - Source Code NOT compiled for: Arduino Uno
  14.     - Source Code created on: 2024-11-03 23:33:46
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* The project aims to develop an interactive Arduino */
  21.     /* application leveraging connected sensors and */
  22.     /* actuators for real-time data acquisition and */
  23.     /* control, ensuring seamless integration of modular */
  24.     /* libraries for enhanced functionality and user */
  25.     /* engagement. */
  26. /****** END SYSTEM REQUIREMENTS *****/
  27.  
  28. /* START CODE */
  29.  
  30. /****** DEFINITION OF LIBRARIES *****/
  31. #include <Wire.h>   // I2C communication
  32. #include <SPI.h>    // SPI communication
  33. #include <Servo.h>  // Control servo motors
  34. #include <DHT.h>    // DHT sensor library
  35.  
  36. /****** FUNCTION PROTOTYPES *****/
  37. void setup(void);
  38. void loop(void);
  39.  
  40. /****** GLOBAL VARIABLES *****/
  41. DHT dht(2, DHT11); // Initialize DHT sensor on pin 2, using DHT11
  42. Servo myServo;     // Create a Servo object
  43.  
  44. void setup(void)
  45. {
  46.     // Initialize serial communication for debugging
  47.     Serial.begin(9600);
  48.    
  49.     // Initialize DHT sensor
  50.     dht.begin();
  51.    
  52.     // Attach the servo on pin 9
  53.     myServo.attach(9);
  54. }
  55.  
  56. void loop(void)
  57. {
  58.     // Read temperature and humidity from DHT sensor
  59.     float humidity = dht.readHumidity();
  60.     float temperature = dht.readTemperature();
  61.  
  62.     // Check if any reads failed and exit early (to try again).
  63.     if (isnan(humidity) || isnan(temperature)) {
  64.         Serial.println("Failed to read from DHT sensor!");
  65.         return;
  66.     }
  67.  
  68.     // Print the results to the serial monitor
  69.     Serial.print("Humidity: ");
  70.     Serial.print(humidity);
  71.     Serial.print(" %\t");
  72.     Serial.print("Temperature: ");
  73.     Serial.print(temperature);
  74.     Serial.println(" *C");
  75.  
  76.     // Control the servo based on temperature
  77.     int servoPosition = map(temperature, 0, 40, 0, 180); // Map temperature to servo position
  78.     myServo.write(servoPosition); // Set servo position
  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