Advertisement
pleasedontcode

"Temperature Control" rev_07

Jan 21st, 2025
41
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: "Temperature Control"
  13.     - Source Code NOT compiled for: ESP32 DevKit V1
  14.     - Source Code created on: 2025-01-21 15:38:36
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* The thermostat maintains a temperature of 21 */
  21.     /* degrees for 100 seconds using RTOS */
  22. /****** END SYSTEM REQUIREMENTS *****/
  23.  
  24. /* START CODE */
  25.  
  26. /****** DEFINITION OF LIBRARIES *****/
  27. #include <DS18B20.h>    //https://github.com/matmunk/DS18B20
  28. #include <OneWire.h>   // Required for OneWire operations
  29. #include <freertos/FreeRTOS.h> // Include FreeRTOS library
  30. #include <freertos/task.h> // Include FreeRTOS task related functions
  31.  
  32. /****** FUNCTION PROTOTYPES *****/
  33. void setup(void);
  34. void loop(void);
  35. void temperatureControlTask(void * parameter);
  36.  
  37. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  38. const uint8_t Temp_Sensor_DS18B20_DQ_PIN_D4 = 4;
  39. DS18B20 sensor(new OneWire(Temp_Sensor_DS18B20_DQ_PIN_D4)); // Create an instance of DS18B20 with OneWire
  40.  
  41. /****** FUNCTION IMPLEMENTATIONS *****/
  42. void setup(void)
  43. {
  44.     Serial.begin(115200);
  45.     serial.println("Thermostat Starting");
  46.    
  47.     sensor.begin();
  48.    
  49.     // Create a FreeRTOS task for temperature control
  50.     xTaskCreate(
  51.         temperatureControlTask,     // Function to implement the task
  52.         "Temperature Control",       // Name of the task
  53.         1024,                        // Stack size in words
  54.         NULL,                        // Task input parameter
  55.         1,                           // Priority of the task
  56.         NULL                         // Task handle
  57.     );
  58. }
  59.  
  60. void loop(void)
  61. {
  62.     // Loop remains empty as tasks run in the background
  63. }
  64.  
  65. // Temperature control task
  66. void temperatureControlTask(void * parameter)
  67. {
  68.     const float targetTemperature = 21.0; // Target temperature
  69.     const int duration = 100000; // Run time in milliseconds (100 seconds)
  70.    
  71.     uint32_t startTime = xTaskGetTickCount();
  72.     while ((xTaskGetTickCount() - startTime) * portTICK_PERIOD_MS < duration)
  73.     {
  74.         sensor.requestTemperatures();
  75.         float currentTemperature = sensor.getTempC(); // Request and retrieve current temperature
  76.         Serial.print("Current Temperature: ");
  77.         Serial.println(currentTemperature);
  78.        
  79.         // Additional code to maintain temperature can be added here
  80.         vTaskDelay(1000 / portTICK_PERIOD_MS); // Delay for 1 second to prevent flooding the output
  81.     }
  82.    
  83.     Serial.println("Temperature Control Finished");
  84.     vTaskDelete(NULL); // Delete the task when done
  85. }
  86.  
  87. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement