Advertisement
pleasedontcode

"Thermostat Control" rev_06

Jan 21st, 2025
35
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:34:51
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* The thermostat maintains the set temperature for */
  21.     /* the specified time. rtos is used */
  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>   // Include OneWire library for DS18B20
  29.  
  30. /****** FUNCTION PROTOTYPES *****/
  31. void setup(void);
  32. void loop(void);
  33. void thermostatTask(void *pvParameters); // Task function for the thermostat
  34.  
  35. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  36. const uint8_t Temp_Sensor_DS18B20_DQ_PIN_D4 = 4;
  37.  
  38. /***** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  39. OneWire oneWire(Temp_Sensor_DS18B20_DQ_PIN_D4); // Create OneWire instance
  40. DS18B20 tempSensor(&oneWire); // Create DS18B20 instance
  41.  
  42. // Temperature settings
  43. float setTemperature = 25.0; // Desired temperature in Celsius
  44. const int holdTime = 60000; // Time to maintain temperature in milliseconds
  45.  
  46. void setup(void)
  47. {
  48.     // Initialize serial communication for debugging
  49.     Serial.begin(115200);
  50.    
  51.     // Initialize temperature sensor
  52.     if (!tempSensor.begin()) {
  53.         Serial.println("Could not find a valid DS18B20 sensor at the pin.");
  54.         while (1);
  55.     }
  56.  
  57.     // Create a FreeRTOS task for the thermostat
  58.     xTaskCreate(thermostatTask, "ThermostatTask", 2048, NULL, 1, NULL);
  59. }
  60.  
  61. void loop(void)
  62. {
  63.     // Main loop can be used for other tasks or can remain empty
  64. }
  65.  
  66. /****** THERMOSTAT TASK FUNCTION *****/
  67. void thermostatTask(void *pvParameters)
  68. {
  69.     while (true) {
  70.         // Request temperature from the sensor
  71.         tempSensor.requestTemperatures();
  72.         float currentTemperature = tempSensor.getTempC();
  73.  
  74.         // Print the current temperature
  75.         Serial.print("Current Temperature: ");
  76.         Serial.println(currentTemperature);
  77.  
  78.         // Check if the current temperature is below the set temperature
  79.         if (currentTemperature < setTemperature) {
  80.             // Code to turn on heating (e.g., digitalWrite(HEATER_PIN, HIGH);)
  81.             Serial.println("Heating ON");
  82.         } else {
  83.             // Code to turn off heating (e.g., digitalWrite(HEATER_PIN, LOW);)
  84.             Serial.println("Heating OFF");
  85.         }
  86.  
  87.         // Maintain the set temperature for the specified time
  88.         vTaskDelay(holdTime / portTICK_PERIOD_MS); // Delay for holdTime
  89.     }
  90. }
  91.  
  92. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement