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:48:17
- ********* 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. rtos is used. */
- /****** END SYSTEM REQUIREMENTS *****/
- /* START CODE */
- /****** DEFINITION OF LIBRARIES *****/
- #include <DS18B20.h> // https://github.com/matmunk/DS18B20
- #include <OneWire.h> // Include OneWire library for DS18B20
- /****** FUNCTION PROTOTYPES *****/
- void setup(void);
- void loop(void);
- void controlHeater(void);
- /***** 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); // Create OneWire instance
- DS18B20 sensor(&oneWire); // Create DS18B20 instance
- // Heater control pin
- const uint8_t HEATER_PIN = 5; // Define heater control pin
- // Target temperature
- const float TARGET_TEMPERATURE = 21.0;
- // Timing variables
- unsigned long previousMillis = 0;
- const long interval = 100000; // 100 seconds in milliseconds
- void setup(void)
- {
- // put your setup code here, to run once:
- pinMode(HEATER_PIN, OUTPUT); // Set heater pin as output
- digitalWrite(HEATER_PIN, LOW); // Ensure heater is off initially
- sensor.begin(); // Initialize the temperature sensor
- }
- void loop(void)
- {
- // put your main code here, to run repeatedly:
- controlHeater(); // Call heater control function
- }
- void controlHeater(void)
- {
- // Get the current temperature
- sensor.requestTemperatures();
- float currentTemperature = sensor.getTempC();
- // Check if it's time to control the heater
- unsigned long currentMillis = millis();
- if (currentMillis - previousMillis >= interval) {
- previousMillis = currentMillis; // Save the last time
- // Control the heater based on the temperature
- if (currentTemperature < TARGET_TEMPERATURE) {
- digitalWrite(HEATER_PIN, HIGH); // Turn on heater
- } else {
- digitalWrite(HEATER_PIN, LOW); // Turn off heater
- }
- }
- }
- /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement