Advertisement
pleasedontcode

Temp Control rev_01

Oct 13th, 2024
80
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: Temp Control
  13.     - Source Code NOT compiled for: Arduino Uno
  14.     - Source Code created on: 2024-10-13 15:37:09
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* Power Management System for Smart Homes: Build a */
  21.     /* power management system that automatically turns */
  22.     /* off unused devices or circuits to conserve energy */
  23.     /* based on usage patterns detected by sensors. */
  24. /****** END SYSTEM REQUIREMENTS *****/
  25.  
  26. /****** DEFINITION OF LIBRARIES *****/
  27. #include <DS18B20.h>    //https://github.com/matmunk/DS18B20
  28. #include <Relay.h>  //https://github.com/rafaelnsantos/Relay
  29.  
  30. /****** FUNCTION PROTOTYPES *****/
  31. void setup(void);
  32. void loop(void);
  33.  
  34. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  35. const uint8_t temp_sens_DS18B20_DQ_PIN_D2       = 2;
  36.  
  37. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  38. const uint8_t led_LED_PIN_D3        = 3;
  39. const uint8_t relay_RelayModule_Signal_PIN_D4       = 4;
  40.  
  41. /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
  42. /***** used to store raw data *****/
  43. bool    led_LED_PIN_D3_rawData      = 0;
  44. bool    relay_RelayModule_Signal_PIN_D4_rawData     = 0;
  45.  
  46. /***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
  47. /***** used to store data after characteristic curve transformation *****/
  48. float   led_LED_PIN_D3_phyData      = 0.0;
  49. float   relay_RelayModule_Signal_PIN_D4_phyData     = 0.0;
  50.  
  51. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  52. // Initialize DS18B20 temperature sensor with the address of the device
  53. uint8_t address[] = {40, 250, 31, 218, 4, 0, 0, 52}; // Example address
  54. DS18B20 ds(address); // Create DS18B20 object with the specified address
  55.  
  56. // Initialize Relay object on pin 4, Normally Open (NO)
  57. Relay relay(relay_RelayModule_Signal_PIN_D4, true); // Use true for Normally Open configuration
  58.  
  59. void setup(void)
  60. {
  61.     // put your setup code here, to run once:
  62.  
  63.     pinMode(temp_sens_DS18B20_DQ_PIN_D2, INPUT); // Set temperature sensor pin as input
  64.  
  65.     pinMode(led_LED_PIN_D3, OUTPUT); // Set LED pin as output
  66.     pinMode(relay_RelayModule_Signal_PIN_D4, OUTPUT); // Set relay pin as output
  67.  
  68.     // Initialize the relay
  69.     relay.begin(); // Initialize the relay pin
  70. }
  71.  
  72. void loop(void)
  73. {
  74.     // put your main code here, to run repeatedly:
  75.  
  76.     updateOutputs(); // Refresh output data
  77.  
  78.     // Update the DS18B20 temperature sensor
  79.     ds.Update(); // Call the update method to read temperature
  80.     int16_t temperature = ds.GetTemperature<int16_t>(); // Get temperature in 1/16 degrees Celsius
  81.  
  82.     // Check if temperature exceeds certain thresholds
  83.     if (temperature > 250) { // Example high threshold (25.0°C)
  84.         relay.turnOn(); // Turn relay on if temperature is too high
  85.         led_LED_PIN_D3_rawData = true; // Turn on LED to indicate high temperature
  86.     } else {
  87.         relay.turnOff(); // Turn relay off if temperature is normal
  88.         led_LED_PIN_D3_rawData = false; // Turn off LED
  89.     }
  90.  
  91.     delay(1000); // Delay for stability
  92. }
  93.  
  94. void updateOutputs()
  95. {
  96.     digitalWrite(led_LED_PIN_D3, led_LED_PIN_D3_rawData); // Update LED state
  97.     digitalWrite(relay_RelayModule_Signal_PIN_D4, relay_RelayModule_Signal_PIN_D4_rawData); // Update relay state
  98. }
  99.  
  100. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement