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:46:06
- ********* 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. using RTOS */
- /****** 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 tempSensor(&oneWire); // Create DS18B20 instance
- /****** THERMOSTAT SETTINGS *****/
- const float TARGET_TEMPERATURE = 21.0; // Target temperature in degrees Celsius
- const unsigned long HEATER_ON_DURATION = 100000; // Heater on duration in milliseconds (100 seconds)
- bool heaterState = false; // Heater state
- void setup(void)
- {
- // Initialize serial communication for debugging
- Serial.begin(115200);
- // Initialize temperature sensor
- if (!tempSensor.begin()) {
- Serial.println("Could not find a valid DS18B20 sensor!");
- while (1);
- }
- }
- void loop(void)
- {
- // Request temperature from sensor
- tempSensor.requestTemperatures();
- float currentTemperature = tempSensor.getTempC();
- // Print current temperature for debugging
- Serial.print("Current Temperature: ");
- Serial.println(currentTemperature);
- // Control the heater based on the current temperature
- controlHeater(currentTemperature);
- // Delay for a while before the next reading
- delay(1000); // Read temperature every second
- }
- void controlHeater(float currentTemperature)
- {
- if (currentTemperature < TARGET_TEMPERATURE && !heaterState) {
- // Turn the heater on
- heaterState = true;
- Serial.println("Heater ON");
- // Here you would add code to actually turn on the heater (e.g., digitalWrite(pin, HIGH));
- // Keep the heater on for the specified duration
- delay(HEATER_ON_DURATION);
- // Turn the heater off
- heaterState = false;
- Serial.println("Heater OFF");
- // Here you would add code to actually turn off the heater (e.g., digitalWrite(pin, LOW));
- }
- }
- /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement