Advertisement
pleasedontcode

"Temperature Control" rev_09

Jan 21st, 2025
34
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:46:06
  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. using RTOS */
  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 tempSensor(&oneWire); // Create DS18B20 instance
  42.  
  43. /****** THERMOSTAT SETTINGS *****/
  44. const float TARGET_TEMPERATURE = 21.0; // Target temperature in degrees Celsius
  45. const unsigned long HEATER_ON_DURATION = 100000; // Heater on duration in milliseconds (100 seconds)
  46. bool heaterState = false; // Heater state
  47.  
  48. void setup(void)
  49. {
  50.     // Initialize serial communication for debugging
  51.     Serial.begin(115200);
  52.    
  53.     // Initialize temperature sensor
  54.     if (!tempSensor.begin()) {
  55.         Serial.println("Could not find a valid DS18B20 sensor!");
  56.         while (1);
  57.     }
  58. }
  59.  
  60. void loop(void)
  61. {
  62.     // Request temperature from sensor
  63.     tempSensor.requestTemperatures();
  64.     float currentTemperature = tempSensor.getTempC();
  65.  
  66.     // Print current temperature for debugging
  67.     Serial.print("Current Temperature: ");
  68.     Serial.println(currentTemperature);
  69.  
  70.     // Control the heater based on the current temperature
  71.     controlHeater(currentTemperature);
  72.  
  73.     // Delay for a while before the next reading
  74.     delay(1000); // Read temperature every second
  75. }
  76.  
  77. void controlHeater(float currentTemperature)
  78. {
  79.     if (currentTemperature < TARGET_TEMPERATURE && !heaterState) {
  80.         // Turn the heater on
  81.         heaterState = true;
  82.         Serial.println("Heater ON");
  83.         // Here you would add code to actually turn on the heater (e.g., digitalWrite(pin, HIGH));
  84.        
  85.         // Keep the heater on for the specified duration
  86.         delay(HEATER_ON_DURATION);
  87.        
  88.         // Turn the heater off
  89.         heaterState = false;
  90.         Serial.println("Heater OFF");
  91.         // Here you would add code to actually turn off the heater (e.g., digitalWrite(pin, LOW));
  92.     }
  93. }
  94.  
  95. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement