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: "Temperature Control"
- - Source Code NOT compiled for: ESP32 DevKit V1
- - Source Code created on: 2025-01-21 15:49:47
- ********* Pleasedontcode.com **********/
- /****** SYSTEM REQUIREMENTS *****/
- /****** SYSTEM REQUIREMENT 1 *****/
- /* The thermostat turns the heater on and off to */
- /* maintain a temperature of 21 degrees for 100 */
- /* seconds. Use FreeRTOS */
- /****** END SYSTEM REQUIREMENTS *****/
- /* START CODE */
- /****** DEFINITION OF LIBRARIES *****/
- #include <DS18B20.h> // https://github.com/matmunk/DS18B20
- #include <OneWire.h>
- #include <freertos/FreeRTOS.h>
- #include <freertos/task.h>
- /****** FUNCTION PROTOTYPES *****/
- void setup(void);
- void loop(void);
- void heaterTask(void *pvParameters);
- void temperatureTask(void *pvParameters);
- /***** DEFINITION OF DIGITAL INPUT PINS *****/
- const uint8_t Temp_Sensor_DS18B20_DQ_PIN_D4 = 4;
- /***** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
- OneWire oneWire(Temp_Sensor_DS18B20_DQ_PIN_D4);
- DS18B20 sensor(&oneWire);
- /***** TARGET TEMPERATURE *****/
- const float TARGET_TEMPERATURE = 21.0;
- const int HEATER_ON_TIME = 10000; // Heater on time in milliseconds
- void setup(void)
- {
- // Initialize serial communication
- Serial.begin(115200);
- Serial.println("Thermostat System Starting...");
- // Initialize the temperature sensor
- sensor.begin();
- // Create FreeRTOS tasks
- xTaskCreate(heaterTask, "Heater Task", 2048, NULL, 1, NULL);
- xTaskCreate(temperatureTask, "Temperature Task", 2048, NULL, 1, NULL);
- }
- void loop(void)
- {
- // Empty loop as tasks are handled by FreeRTOS
- }
- void heaterTask(void *pvParameters)
- {
- while (true)
- {
- // Request temperature from the sensor
- sensor.requestTemperatures();
- float currentTemperature = sensor.getTempC();
- // Check if the current temperature is below the target
- if (currentTemperature < TARGET_TEMPERATURE)
- {
- Serial.println("Heater ON");
- // Simulate heater ON
- vTaskDelay(HEATER_ON_TIME / portTICK_PERIOD_MS);
- Serial.println("Heater OFF");
- }
- else
- {
- Serial.println("Temperature is sufficient, Heater OFF");
- }
- // Delay before the next temperature check
- vTaskDelay(1000 / portTICK_PERIOD_MS); // Check every second
- }
- }
- void temperatureTask(void *pvParameters)
- {
- while (true)
- {
- // Request temperature from the sensor
- sensor.requestTemperatures();
- float currentTemperature = sensor.getTempC();
- Serial.print("Current Temperature: ");
- Serial.println(currentTemperature);
- // Delay before the next temperature read
- vTaskDelay(2000 / portTICK_PERIOD_MS); // Read every 2 seconds
- }
- }
- /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement