Advertisement
pleasedontcode

**Non-Blocking Control** rev_08

Jan 26th, 2025
48
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: **Non-Blocking Control**
  13.     - Source Code NOT compiled for: ESP32 DevKit V1
  14.     - Source Code created on: 2025-01-26 06:35:56
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* 温度到达 60led亮灯 */
  21. /****** END SYSTEM REQUIREMENTS *****/
  22.  
  23.  
  24. /********* User code review feedback **********
  25. #### Feedback 1 ####
  26. - 温度到达30伺服转动90度3秒回转回 不要阻塞
  27. ********* User code review feedback **********/
  28.  
  29. /* START CODE */
  30.  
  31. /****** DEFINITION OF LIBRARIES *****/
  32. #include <DHT.h>    //https://github.com/adafruit/DHT-sensor-library
  33. #include <Deneyap_Servo.h>  //https://github.com/deneyapkart/deneyap-servo-arduino-library
  34.  
  35. /****** FUNCTION PROTOTYPES *****/
  36. void setup(void);
  37. void loop(void);
  38. void updateOutputs(void);
  39. void readTemperature(void);
  40. void rotateServoNonBlocking(); // New function for non-blocking servo rotation
  41.  
  42. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  43. const uint8_t DHT22_DOUT_PIN_D4 = 4;
  44.  
  45. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  46. const uint8_t LED_PIN_D14 = 14;
  47.  
  48. /***** DEFINITION OF PWM OUTPUT PINS *****/
  49. const uint8_t Servomotor_PWMSignal_PIN_D13 = 13;
  50.  
  51. /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
  52. bool LED_PIN_D14_rawData = 0;
  53. uint8_t Servomotor_PWMSignal_PIN_D13_rawData = 0;
  54.  
  55. /***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
  56. float LED_PIN_D14_phyData = 0.0;
  57. float Servomotor_PWMSignal_PIN_D13_phyData = 0.0;
  58.  
  59. // Variables for non-blocking servo rotation
  60. unsigned long previousMillis = 0;
  61. const long interval = 3000; // 3 seconds
  62. bool servoRotated = false;
  63.  
  64. // Create an instance of the DHT sensor
  65. DHT dht(DHT22_DOUT_PIN_D4, DHT22);
  66.  
  67. // Create an instance of the Servo
  68. Servo servo;
  69.  
  70. void setup(void)
  71. {
  72.     // Initialize the DHT sensor
  73.     dht.begin();
  74.  
  75.     // Set pin modes
  76.     pinMode(LED_PIN_D14, OUTPUT);
  77.     pinMode(Servomotor_PWMSignal_PIN_D13, OUTPUT);
  78. }
  79.  
  80. void loop(void)
  81. {
  82.     // Read temperature and update outputs
  83.     readTemperature();
  84.     updateOutputs(); // Refresh output data
  85.     rotateServoNonBlocking(); // Handle non-blocking servo rotation
  86. }
  87.  
  88. void readTemperature()
  89. {
  90.     // Read temperature from the DHT sensor
  91.     float temperature = dht.readTemperature();
  92.  
  93.     // Check if the reading is valid
  94.     if (isnan(temperature)) {
  95.         Serial.println("Failed to read from DHT sensor!");
  96.         return;
  97.     }
  98.  
  99.     // Check if temperature reaches 60 degrees
  100.     if (temperature >= 60) {
  101.         LED_PIN_D14_rawData = true; // Turn on LED
  102.     } else {
  103.         LED_PIN_D14_rawData = false; // Turn off LED
  104.     }
  105.  
  106.     // Check if temperature reaches 30 degrees for servo rotation
  107.     if (temperature >= 30 && !servoRotated) {
  108.         servo.write(90); // Rotate servo to 90 degrees
  109.         previousMillis = millis(); // Reset the timer
  110.         servoRotated = true; // Set the flag to indicate servo has rotated
  111.     }
  112. }
  113.  
  114. void updateOutputs()
  115. {
  116.     digitalWrite(LED_PIN_D14, LED_PIN_D14_rawData);
  117.     analogWrite(Servomotor_PWMSignal_PIN_D13, Servomotor_PWMSignal_PIN_D13_rawData);
  118. }
  119.  
  120. void rotateServoNonBlocking()
  121. {
  122.     // Check if the servo has rotated
  123.     if (servoRotated) {
  124.         unsigned long currentMillis = millis();
  125.         // Check if 3 seconds have passed
  126.         if (currentMillis - previousMillis >= interval) {
  127.             servo.write(0); // Rotate back to 0 degrees
  128.             servoRotated = false; // Reset the flag
  129.         }
  130.     }
  131. }
  132.  
  133. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement