Advertisement
pleasedontcode

"Distance-controlled Fan" rev_01

Apr 5th, 2024
99
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-controlled Fan"
  13.     - Source Code compiled for: Arduino Uno
  14.     - Source Code created on: 2024-04-05 18:22:50
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* Turn on the fan of the command is 1 and turn off */
  21.     /* the fan of the command is 0 and also turn off the */
  22.     /* util any object near the ultrasonic sensor and */
  23.     /* again turn on the fan when the object moves away */
  24. /****** END SYSTEM REQUIREMENTS *****/
  25.  
  26. /****** DEFINITION OF LIBRARIES *****/
  27. #include <Ultrasonic.h>    // Include the Ultrasonic library
  28. #include <DHT.h>    // Include the DHT library
  29.  
  30. /****** SYSTEM REQUIREMENTS *****/
  31. /****** SYSTEM REQUIREMENT 1 *****/
  32. /* Turn on the fan if the command is 1 and turn off */
  33. /* the fan if the command is 0. Also, turn off the */
  34. /* fan if any object is near the ultrasonic sensor */
  35. /* and turn on the fan when the object moves away */
  36. /****** END SYSTEM REQUIREMENTS *****/
  37.  
  38. /****** FUNCTION PROTOTYPES *****/
  39. void setup(void);
  40. void loop(void);
  41. void updateOutputs(void);  // Add function prototype for updateOutputs function
  42.  
  43. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  44. const uint8_t Temp_DHT11_DOUT_PIN_D2 = 2;
  45. const uint8_t Ult_HC_SR04_Echo_PIN_D4 = 4;
  46.  
  47. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  48. const uint8_t Ult_HC_SR04_Trigger_PIN_D3 = 3;
  49.  
  50. /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
  51. /***** used to store raw data *****/
  52. bool Ult_HC_SR04_Trigger_PIN_D3_rawData = 0;
  53.  
  54. /***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
  55. /***** used to store data after characteristic curve transformation *****/
  56. float Ult_HC_SR04_Trigger_PIN_D3_phyData = 0.0;
  57.  
  58. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  59. Ultrasonic ultrasonic(Ult_HC_SR04_Trigger_PIN_D3, Ult_HC_SR04_Echo_PIN_D4);  // Create an instance of the Ultrasonic class
  60.  
  61. DHT dht(Temp_DHT11_DOUT_PIN_D2, DHT11);  // Create an instance of the DHT class with DHT11 sensor type
  62.  
  63. bool fanStatus = false;  // Variable to store the current status of the fan
  64.  
  65. void setup(void)
  66. {
  67.     // put your setup code here, to run once:
  68.  
  69.     pinMode(Temp_DHT11_DOUT_PIN_D2, INPUT_PULLUP);
  70.     pinMode(Ult_HC_SR04_Echo_PIN_D4, INPUT);
  71.     pinMode(Ult_HC_SR04_Trigger_PIN_D3, OUTPUT);
  72.  
  73.     dht.begin();  // Initialize the DHT sensor
  74.  
  75. }
  76.  
  77. void loop(void)
  78. {
  79.     // put your main code here, to run repeatedly:
  80.  
  81.     updateOutputs(); // Refresh output data
  82.  
  83.     // Read the distance from the ultrasonic sensor
  84.     unsigned int distance = ultrasonic.read();
  85.  
  86.     // Check if an object is detected within a certain distance
  87.     if (distance < 20 && !fanStatus) {
  88.         // Turn on the fan
  89.         Ult_HC_SR04_Trigger_PIN_D3_rawData = 1;
  90.         fanStatus = true;
  91.     } else if (distance >= 20 && fanStatus) {
  92.         // Turn off the fan
  93.         Ult_HC_SR04_Trigger_PIN_D3_rawData = 0;
  94.         fanStatus = false;
  95.     }
  96.  
  97.     // Add your code here to read other sensor data and update variables
  98.  
  99.     delay(1000);  // Wait for 1 second before checking again
  100. }
  101.  
  102. void updateOutputs()
  103. {
  104.     digitalWrite(Ult_HC_SR04_Trigger_PIN_D3, Ult_HC_SR04_Trigger_PIN_D3_rawData);
  105. }
  106.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement