Advertisement
pleasedontcode

**Thermostat Control** rev_01

Jan 21st, 2025
29
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 14:30:51
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* a thermostat with hysteresis operation logic using */
  21.     /* Freertos. */
  22. /****** END SYSTEM REQUIREMENTS *****/
  23.  
  24. /* START CODE */
  25.  
  26. /****** DEFINITION OF LIBRARIES *****/
  27. #include <Arduino.h>
  28. #include <FreeRTOS.h>
  29.  
  30. /****** FUNCTION PROTOTYPES *****/
  31. void setup(void);
  32. void loop(void);
  33. void thermostatTask(void *pvParameters);
  34.  
  35. /****** GLOBAL VARIABLES *****/
  36. const int temperatureSensorPin = A0; // Pin for temperature sensor
  37. const int heaterPin = 2;              // Pin for heater control
  38. const float setPoint = 25.0;          // Desired temperature
  39. const float hysteresis = 1.0;         // Hysteresis value
  40.  
  41. /****** SETUP FUNCTION *****/
  42. void setup(void)
  43. {
  44.     // Initialize serial communication
  45.     Serial.begin(115200);
  46.    
  47.     // Set heater pin as output
  48.     pinMode(heaterPin, OUTPUT);
  49.    
  50.     // Create the thermostat task
  51.     xTaskCreate(
  52.         thermostatTask,       // Task function
  53.         "Thermostat Task",    // Name of task
  54.         2048,                 // Stack size in words
  55.         NULL,                 // Task input parameter
  56.         1,                    // Priority of the task
  57.         NULL                  // Task handle
  58.     );
  59. }
  60.  
  61. /****** LOOP FUNCTION *****/
  62. void loop(void)
  63. {
  64.     // Empty loop as FreeRTOS handles tasks
  65. }
  66.  
  67. /****** THERMOSTAT TASK FUNCTION *****/
  68. void thermostatTask(void *pvParameters)
  69. {
  70.     float currentTemperature;
  71.  
  72.     while (true)
  73.     {
  74.         // Read the current temperature from the sensor
  75.         currentTemperature = analogRead(temperatureSensorPin) * (5.0 / 1023.0) * 100.0; // Convert to Celsius
  76.  
  77.         // Control the heater based on hysteresis logic
  78.         if (currentTemperature < (setPoint - hysteresis))
  79.         {
  80.             digitalWrite(heaterPin, HIGH); // Turn on heater
  81.             Serial.println("Heater ON");
  82.         }
  83.         else if (currentTemperature > (setPoint + hysteresis))
  84.         {
  85.             digitalWrite(heaterPin, LOW); // Turn off heater
  86.             Serial.println("Heater OFF");
  87.         }
  88.  
  89.         // Delay for a while before the next reading
  90.         vTaskDelay(pdMS_TO_TICKS(1000)); // Delay for 1 second
  91.     }
  92. }
  93.  
  94. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement