Advertisement
pleasedontcode

"Thermostat Control" rev_04

Jan 21st, 2025
36
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: "Thermostat Control"
  13.     - Source Code NOT compiled for: ESP32 DevKit V1
  14.     - Source Code created on: 2025-01-21 15:20:20
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* a thermostat with an offset on and offset off */
  21.     /* logic. Freertos is used. It works according to a */
  22.     /* multi-step recipe. In each step: Step number, Step */
  23.     /* time, Step Temperature. Maintains the step */
  24.     /* temperature for a set amount of time. */
  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>  // Required 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 PARAMETERS *****/
  42. struct Step {
  43.     uint16_t stepNumber;
  44.     uint32_t stepTime; // in milliseconds
  45.     float stepTemperature; // in Celsius
  46. };
  47.  
  48. Step steps[] = {
  49.     {1, 30000, 22.0}, // Step 1: 30 seconds at 22°C
  50.     {2, 30000, 25.0}, // Step 2: 30 seconds at 25°C
  51.     {3, 30000, 20.0}  // Step 3: 30 seconds at 20°C
  52. };
  53.  
  54. const uint8_t totalSteps = sizeof(steps) / sizeof(steps[0]);
  55. uint8_t currentStep = 0;
  56.  
  57. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  58. OneWire oneWire(Temp_Sensor_DS18B20_DQ_PIN_D4);
  59. DS18B20 tempSensor(&oneWire);
  60.  
  61. void setup(void)
  62. {
  63.     // Initialize serial communication
  64.     Serial.begin(115200);
  65.  
  66.     // Initialize temperature sensor
  67.     if (!tempSensor.begin()) {
  68.         Serial.println("Could not find a valid DS18B20 sensor at the address.");
  69.         while (1);
  70.     }
  71.  
  72.     // Create FreeRTOS task for thermostat
  73.     xTaskCreate(thermostatTask, "ThermostatTask", 2048, NULL, 1, NULL);
  74. }
  75.  
  76. void loop(void)
  77. {
  78.     // Main loop can be used for other tasks if needed
  79.     vTaskDelay(1000 / portTICK_PERIOD_MS); // Prevent watchdog timer reset
  80. }
  81.  
  82. void thermostatTask(void *pvParameters)
  83. {
  84.     while (true) {
  85.         // Check if we have completed all steps
  86.         if (currentStep < totalSteps) {
  87.             // Set the desired temperature
  88.             float desiredTemperature = steps[currentStep].stepTemperature;
  89.             Serial.print("Setting temperature to: ");
  90.             Serial.println(desiredTemperature);
  91.  
  92.             // Wait for the specified duration
  93.             vTaskDelay(steps[currentStep].stepTime / portTICK_PERIOD_MS);
  94.  
  95.             // Move to the next step
  96.             currentStep++;
  97.         } else {
  98.             // Reset to the first step if all steps are completed
  99.             currentStep = 0;
  100.         }
  101.     }
  102. }
  103.  
  104. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement