Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /********* Pleasedontcode.com **********
- Pleasedontcode thanks you for automatic code generation! Enjoy your code!
- - Terms and Conditions:
- You have a non-exclusive, revocable, worldwide, royalty-free license
- for personal and commercial use. Attribution is optional; modifications
- are allowed, but you're responsible for code maintenance. We're not
- liable for any loss or damage. For full terms,
- please visit pleasedontcode.com/termsandconditions.
- - Project: **Thermostat Control**
- - Source Code NOT compiled for: ESP32 DevKit V1
- - Source Code created on: 2025-01-21 14:30:51
- ********* Pleasedontcode.com **********/
- /****** SYSTEM REQUIREMENTS *****/
- /****** SYSTEM REQUIREMENT 1 *****/
- /* a thermostat with hysteresis operation logic using */
- /* Freertos. */
- /****** END SYSTEM REQUIREMENTS *****/
- /* START CODE */
- /****** DEFINITION OF LIBRARIES *****/
- #include <Arduino.h>
- #include <FreeRTOS.h>
- /****** FUNCTION PROTOTYPES *****/
- void setup(void);
- void loop(void);
- void thermostatTask(void *pvParameters);
- /****** GLOBAL VARIABLES *****/
- const int temperatureSensorPin = A0; // Pin for temperature sensor
- const int heaterPin = 2; // Pin for heater control
- const float setPoint = 25.0; // Desired temperature
- const float hysteresis = 1.0; // Hysteresis value
- /****** SETUP FUNCTION *****/
- void setup(void)
- {
- // Initialize serial communication
- Serial.begin(115200);
- // Set heater pin as output
- pinMode(heaterPin, OUTPUT);
- // Create the thermostat task
- xTaskCreate(
- thermostatTask, // Task function
- "Thermostat Task", // Name of task
- 2048, // Stack size in words
- NULL, // Task input parameter
- 1, // Priority of the task
- NULL // Task handle
- );
- }
- /****** LOOP FUNCTION *****/
- void loop(void)
- {
- // Empty loop as FreeRTOS handles tasks
- }
- /****** THERMOSTAT TASK FUNCTION *****/
- void thermostatTask(void *pvParameters)
- {
- float currentTemperature;
- while (true)
- {
- // Read the current temperature from the sensor
- currentTemperature = analogRead(temperatureSensorPin) * (5.0 / 1023.0) * 100.0; // Convert to Celsius
- // Control the heater based on hysteresis logic
- if (currentTemperature < (setPoint - hysteresis))
- {
- digitalWrite(heaterPin, HIGH); // Turn on heater
- Serial.println("Heater ON");
- }
- else if (currentTemperature > (setPoint + hysteresis))
- {
- digitalWrite(heaterPin, LOW); // Turn off heater
- Serial.println("Heater OFF");
- }
- // Delay for a while before the next reading
- vTaskDelay(pdMS_TO_TICKS(1000)); // Delay for 1 second
- }
- }
- /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement