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 15:20:20
- ********* Pleasedontcode.com **********/
- /****** SYSTEM REQUIREMENTS *****/
- /****** SYSTEM REQUIREMENT 1 *****/
- /* a thermostat with an offset on and offset off */
- /* logic. Freertos is used. It works according to a */
- /* multi-step recipe. In each step: Step number, Step */
- /* time, Step Temperature. Maintains the step */
- /* temperature for a set amount of time. */
- /****** END SYSTEM REQUIREMENTS *****/
- /* START CODE */
- /****** DEFINITION OF LIBRARIES *****/
- #include <DS18B20.h> // https://github.com/matmunk/DS18B20
- #include <OneWire.h> // Required for DS18B20
- /****** FUNCTION PROTOTYPES *****/
- void setup(void);
- void loop(void);
- void thermostatTask(void *pvParameters); // FreeRTOS task for thermostat
- /***** DEFINITION OF DIGITAL INPUT PINS *****/
- const uint8_t Temp_Sensor_DS18B20_DQ_PIN_D4 = 4;
- /***** THERMOSTAT PARAMETERS *****/
- struct Step {
- uint16_t stepNumber;
- uint32_t stepTime; // in milliseconds
- float stepTemperature; // in Celsius
- };
- Step steps[] = {
- {1, 30000, 22.0}, // Step 1: 30 seconds at 22°C
- {2, 30000, 25.0}, // Step 2: 30 seconds at 25°C
- {3, 30000, 20.0} // Step 3: 30 seconds at 20°C
- };
- const uint8_t totalSteps = sizeof(steps) / sizeof(steps[0]);
- uint8_t currentStep = 0;
- /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
- OneWire oneWire(Temp_Sensor_DS18B20_DQ_PIN_D4);
- DS18B20 tempSensor(&oneWire);
- void setup(void)
- {
- // Initialize serial communication
- Serial.begin(115200);
- // Initialize temperature sensor
- if (!tempSensor.begin()) {
- Serial.println("Could not find a valid DS18B20 sensor at the address.");
- while (1);
- }
- // Create FreeRTOS task for thermostat
- xTaskCreate(thermostatTask, "ThermostatTask", 2048, NULL, 1, NULL);
- }
- void loop(void)
- {
- // Main loop can be used for other tasks if needed
- vTaskDelay(1000 / portTICK_PERIOD_MS); // Prevent watchdog timer reset
- }
- void thermostatTask(void *pvParameters)
- {
- while (true) {
- // Check if we have completed all steps
- if (currentStep < totalSteps) {
- // Set the desired temperature
- float desiredTemperature = steps[currentStep].stepTemperature;
- Serial.print("Setting temperature to: ");
- Serial.println(desiredTemperature);
- // Wait for the specified duration
- vTaskDelay(steps[currentStep].stepTime / portTICK_PERIOD_MS);
- // Move to the next step
- currentStep++;
- } else {
- // Reset to the first step if all steps are completed
- currentStep = 0;
- }
- }
- }
- /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement