Advertisement
pleasedontcode

"Temperature Control" rev_11

Jan 21st, 2025
69
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:49:47
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* The thermostat turns the heater on and off to */
  21.     /* maintain a temperature of 21 degrees for 100 */
  22.     /* seconds. Use FreeRTOS */
  23. /****** END SYSTEM REQUIREMENTS *****/
  24.  
  25. /* START CODE */
  26.  
  27. /****** DEFINITION OF LIBRARIES *****/
  28. #include <DS18B20.h>    // https://github.com/matmunk/DS18B20
  29. #include <OneWire.h>
  30. #include <freertos/FreeRTOS.h>
  31. #include <freertos/task.h>
  32.  
  33. /****** FUNCTION PROTOTYPES *****/
  34. void setup(void);
  35. void loop(void);
  36. void heaterTask(void *pvParameters);
  37. void temperatureTask(void *pvParameters);
  38.  
  39. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  40. const uint8_t Temp_Sensor_DS18B20_DQ_PIN_D4 = 4;
  41.  
  42. /***** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  43. OneWire oneWire(Temp_Sensor_DS18B20_DQ_PIN_D4);
  44. DS18B20 sensor(&oneWire);
  45.  
  46. /***** TARGET TEMPERATURE *****/
  47. const float TARGET_TEMPERATURE = 21.0;
  48. const int HEATER_ON_TIME = 10000; // Heater on time in milliseconds
  49.  
  50. void setup(void)
  51. {
  52.     // Initialize serial communication
  53.     Serial.begin(115200);
  54.     Serial.println("Thermostat System Starting...");
  55.  
  56.     // Initialize the temperature sensor
  57.     sensor.begin();
  58.  
  59.     // Create FreeRTOS tasks
  60.     xTaskCreate(heaterTask, "Heater Task", 2048, NULL, 1, NULL);
  61.     xTaskCreate(temperatureTask, "Temperature Task", 2048, NULL, 1, NULL);
  62. }
  63.  
  64. void loop(void)
  65. {
  66.     // Empty loop as tasks are handled by FreeRTOS
  67. }
  68.  
  69. void heaterTask(void *pvParameters)
  70. {
  71.     while (true)
  72.     {
  73.         // Request temperature from the sensor
  74.         sensor.requestTemperatures();
  75.         float currentTemperature = sensor.getTempC();
  76.  
  77.         // Check if the current temperature is below the target
  78.         if (currentTemperature < TARGET_TEMPERATURE)
  79.         {
  80.             Serial.println("Heater ON");
  81.             // Simulate heater ON
  82.             vTaskDelay(HEATER_ON_TIME / portTICK_PERIOD_MS);
  83.             Serial.println("Heater OFF");
  84.         }
  85.         else
  86.         {
  87.             Serial.println("Temperature is sufficient, Heater OFF");
  88.         }
  89.  
  90.         // Delay before the next temperature check
  91.         vTaskDelay(1000 / portTICK_PERIOD_MS); // Check every second
  92.     }
  93. }
  94.  
  95. void temperatureTask(void *pvParameters)
  96. {
  97.     while (true)
  98.     {
  99.         // Request temperature from the sensor
  100.         sensor.requestTemperatures();
  101.         float currentTemperature = sensor.getTempC();
  102.         Serial.print("Current Temperature: ");
  103.         Serial.println(currentTemperature);
  104.  
  105.         // Delay before the next temperature read
  106.         vTaskDelay(2000 / portTICK_PERIOD_MS); // Read every 2 seconds
  107.     }
  108. }
  109.  
  110. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement