Advertisement
pleasedontcode

Temperature Control rev_03

Jan 21st, 2025
46
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 14:58:13
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* a thermostat with  offset on and offset off logic. */
  21.     /* Freertos is used.  The thermostat recipe has the */
  22.     /* name of the recipe, the step number, the step */
  23.     /* temperature, and the step time in minutes. The set */
  24.     /* temperature is maintained during each step. */
  25. /****** END SYSTEM REQUIREMENTS *****/
  26.  
  27. /* START CODE */
  28.  
  29. /****** DEFINITION OF LIBRARIES *****/
  30. #include <DS18B20.h>    //https://github.com/matmunk/DS18B20
  31. #include <OneWire.h>   // Include OneWire library for DS18B20
  32.  
  33. /****** FUNCTION PROTOTYPES *****/
  34. void setup(void);
  35. void loop(void);
  36. void thermostatTask(void *pvParameters); // FreeRTOS task for thermostat
  37.  
  38. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  39. const uint8_t Temp_Sensor_DS18B20_DQ_PIN_D4 = 4;
  40.  
  41. /***** THERMOSTAT RECIPE STRUCTURE *****/
  42. struct ThermostatStep {
  43.     const char* recipeName;
  44.     int stepNumber;
  45.     float stepTemperature;
  46.     int stepTime; // in minutes
  47. };
  48.  
  49. /***** THERMOSTAT RECIPE EXAMPLE *****/
  50. ThermostatStep currentStep = {"Heating", 1, 25.0, 10}; // Example step
  51.  
  52. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  53. OneWire oneWire(Temp_Sensor_DS18B20_DQ_PIN_D4); // OneWire instance
  54. DS18B20 ds18b20(&oneWire); // DS18B20 instance
  55.  
  56. /****** FREE RTOS TASK HANDLE *****/
  57. TaskHandle_t thermostatTaskHandle = NULL;
  58.  
  59. void setup(void)
  60. {
  61.     // Initialize serial communication for debugging
  62.     Serial.begin(115200);
  63.    
  64.     // Initialize the DS18B20 sensor
  65.     if (!ds18b20.begin()) {
  66.         Serial.println("Could not find a valid DS18B20 sensor!");
  67.         while (1);
  68.     }
  69.  
  70.     // Create the thermostat task
  71.     xTaskCreate(
  72.         thermostatTask,      // Task function
  73.         "ThermostatTask",    // Name of the task
  74.         2048,                // Stack size (bytes)
  75.         NULL,                // Parameter passed to the task
  76.         1,                   // Task priority
  77.         &thermostatTaskHandle // Task handle
  78.     );
  79. }
  80.  
  81. void loop(void)
  82. {
  83.     // The loop can be used for other tasks if needed
  84.     vTaskDelay(1000 / portTICK_PERIOD_MS); // Delay for a second
  85. }
  86.  
  87. void thermostatTask(void *pvParameters)
  88. {
  89.     // Run the thermostat logic
  90.     while (true) {
  91.         // Request temperature from the sensor
  92.         ds18b20.requestTemperatures();
  93.         float currentTemperature = ds18b20.getTempC();
  94.  
  95.         // Check if the current temperature is below the set temperature
  96.         if (currentTemperature < currentStep.stepTemperature) {
  97.             // Logic to turn on heating (not implemented)
  98.             Serial.println("Heating ON");
  99.         } else {
  100.             // Logic to turn off heating (not implemented)
  101.             Serial.println("Heating OFF");
  102.         }
  103.  
  104.         // Wait for the duration of the step
  105.         vTaskDelay(currentStep.stepTime * 60000 / portTICK_PERIOD_MS); // Convert minutes to milliseconds
  106.     }
  107. }
  108.  
  109. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement