Advertisement
pleasedontcode

"Temperature Control" rev_10

Jan 21st, 2025
41
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:48:17
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* The thermostat turns the heater on and off to */
  21.     /* maintain a temperature of 21 degrees for 100 */
  22.     /* seconds. rtos is used. */
  23. /****** END SYSTEM REQUIREMENTS *****/
  24.  
  25. /* START CODE */
  26.  
  27. /****** DEFINITION OF LIBRARIES *****/
  28. #include <DS18B20.h>    // https://github.com/matmunk/DS18B20
  29. #include <OneWire.h>    // Include OneWire library for DS18B20
  30.  
  31. /****** FUNCTION PROTOTYPES *****/
  32. void setup(void);
  33. void loop(void);
  34. void controlHeater(void);
  35.  
  36. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  37. const uint8_t Temp_Sensor_DS18B20_DQ_PIN_D4 = 4;
  38.  
  39. /***** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  40. OneWire oneWire(Temp_Sensor_DS18B20_DQ_PIN_D4); // Create OneWire instance
  41. DS18B20 sensor(&oneWire); // Create DS18B20 instance
  42.  
  43. // Heater control pin
  44. const uint8_t HEATER_PIN = 5; // Define heater control pin
  45.  
  46. // Target temperature
  47. const float TARGET_TEMPERATURE = 21.0;
  48.  
  49. // Timing variables
  50. unsigned long previousMillis = 0;
  51. const long interval = 100000; // 100 seconds in milliseconds
  52.  
  53. void setup(void)
  54. {
  55.     // put your setup code here, to run once:
  56.     pinMode(HEATER_PIN, OUTPUT); // Set heater pin as output
  57.     digitalWrite(HEATER_PIN, LOW); // Ensure heater is off initially
  58.     sensor.begin(); // Initialize the temperature sensor
  59. }
  60.  
  61. void loop(void)
  62. {
  63.     // put your main code here, to run repeatedly:
  64.     controlHeater(); // Call heater control function
  65. }
  66.  
  67. void controlHeater(void)
  68. {
  69.     // Get the current temperature
  70.     sensor.requestTemperatures();
  71.     float currentTemperature = sensor.getTempC();
  72.  
  73.     // Check if it's time to control the heater
  74.     unsigned long currentMillis = millis();
  75.     if (currentMillis - previousMillis >= interval) {
  76.         previousMillis = currentMillis; // Save the last time
  77.  
  78.         // Control the heater based on the temperature
  79.         if (currentTemperature < TARGET_TEMPERATURE) {
  80.             digitalWrite(HEATER_PIN, HIGH); // Turn on heater
  81.         } else {
  82.             digitalWrite(HEATER_PIN, LOW); // Turn off heater
  83.         }
  84.     }
  85. }
  86.  
  87. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement