Advertisement
pleasedontcode

"Temperature Control" rev_05

Jan 21st, 2025
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /********* Pleasedontcode.com **********
  2.  
  3.     Pleasedontcode thanks you for automatic code generation! Enjoy your code!
  4.  
  5.     - Terms and Conditions:
  6.     You have a non-exclusive, revocable, worldwide, royalty-free license
  7.     for personal and commercial use. Attribution is optional; modifications
  8.     are allowed, but you're responsible for code maintenance. We're not
  9.     liable for any loss or damage. For full terms,
  10.     please visit pleasedontcode.com/termsandconditions.
  11.  
  12.     - Project: "Temperature Control"
  13.     - Source Code NOT compiled for: ESP32 DevKit V1
  14.     - Source Code created on: 2025-01-21 15:32:45
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* The thermostat maintains the set temperature for */
  21.     /* the specified time. */
  22. /****** END SYSTEM REQUIREMENTS *****/
  23.  
  24. /* START CODE */
  25.  
  26. /****** DEFINITION OF LIBRARIES *****/
  27. #include <DS18B20.h>  // https://github.com/matmunk/DS18B20
  28. #include <OneWire.h>  // Required for DS18B20
  29.  
  30. /****** FUNCTION PROTOTYPES *****/
  31. void setup(void);
  32. void loop(void);
  33.  
  34. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  35. const uint8_t Temp_Sensor_DS18B20_DQ_PIN_D4 = 4; // Data pin for DS18B20
  36.  
  37. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  38. OneWire oneWire(Temp_Sensor_DS18B20_DQ_PIN_D4); // Create OneWire instance
  39. DS18B20 sensor(&oneWire); // Create DS18B20 instance
  40.  
  41. // Desired temperature and time
  42. const float setTemperature = 25.0; // Set temperature in Celsius
  43. const unsigned long holdTime = 60000; // Hold time in milliseconds (60 seconds)
  44. unsigned long previousMillis = 0; // Store last time temperature was set
  45. bool temperatureMaintained = false; // Flag to check if temperature is maintained
  46.  
  47. void setup(void)
  48. {
  49.     Serial.begin(115200); // Initialize serial communication
  50.     sensor.begin(); // Initialize DS18B20 sensor
  51. }
  52.  
  53. void loop(void)
  54. {
  55.     // Request temperature from the sensor
  56.     sensor.requestTemperatures();
  57.  
  58.     float currentTemperature = sensor.getTempC(); // Get current temperature
  59.  
  60.     // Check if the current temperature is below the set temperature
  61.     if (currentTemperature < setTemperature && !temperatureMaintained) {
  62.         Serial.println("Heating up to set temperature..."); // Debug message
  63.         // Here, you would add code to turn on the heater (not implemented)
  64.     }
  65.     else if (currentTemperature >= setTemperature) {
  66.         // If the temperature is reached, start the timer
  67.         if (!temperatureMaintained) {
  68.             previousMillis = millis(); // Reset timer
  69.             temperatureMaintained = true; // Set flag to true
  70.             Serial.println("Set temperature reached, maintaining..."); // Debug message
  71.         }
  72.  
  73.         // Check if the hold time has elapsed
  74.         if (millis() - previousMillis >= holdTime) {
  75.             // Here, you would add code to turn off the heater (not implemented)
  76.             Serial.println("Hold time elapsed, turning off heater."); // Debug message
  77.             temperatureMaintained = false; // Reset flag
  78.         }
  79.     }
  80.  
  81.     // Print current temperature to serial monitor
  82.     Serial.print("Current Temperature: ");
  83.     Serial.println(currentTemperature);
  84.     delay(1000); // Delay to avoid flooding the serial output
  85. }
  86.  
  87. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement