pleasedontcode

Relay Controller rev_03

Aug 13th, 2025
451
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: Relay Controller
  13.     - Source Code NOT compiled for: Arduino Nano ESP32
  14.     - Source Code created on: 2025-08-13 12:40:49
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* esp32 with i2c display,one relay out,one swich for */
  21.     /* start, one swich for stop, one swich for time */
  22.     /* setting up,one swich for time setting down,one */
  23.     /* ds18b20 temperature sensor. */
  24. /****** END SYSTEM REQUIREMENTS *****/
  25.  
  26.  
  27. /********* User code review feedback **********
  28. #### Feedback 1 ####
  29. - not working
  30.  
  31. #### Feedback 2 ####
  32. - no oled in my scope
  33.  
  34. ********* User code review feedback **********/
  35.  
  36. /* START CODE */
  37. /****** DEFINITION OF LIBRARIES *****/
  38. #include <Wire.h>
  39. #include <OneWire.h>
  40. #include <DallasTemperature.h>
  41.  
  42. // Define pins for switches
  43. const int startSwitchPin = 2;   // Start button
  44. const int stopSwitchPin = 3;    // Stop button
  45. const int timeUpSwitchPin = 4;  // Time setting up
  46. const int timeDownSwitchPin = 5; // Time setting down
  47.  
  48. // Define pin for relay
  49. const int relayPin = 6;
  50.  
  51. // Define pin for DS18B20 temperature sensor
  52. const int oneWireBusPin = 7;
  53.  
  54. // Create OneWire and DallasTemperature objects
  55. OneWire oneWire(oneWireBusPin);
  56. DallasTemperature sensors(&oneWire);
  57.  
  58. // Variables for system state
  59. bool systemRunning = false;
  60. int temperature = 0;
  61. int setTimeMinutes = 0;
  62.  
  63. void setup() {
  64.   // Initialize serial communication
  65.   Serial.begin(115200);
  66.  
  67.   // Initialize pins
  68.   pinMode(startSwitchPin, INPUT_PULLUP);
  69.   pinMode(stopSwitchPin, INPUT_PULLUP);
  70.   pinMode(timeUpSwitchPin, INPUT_PULLUP);
  71.   pinMode(timeDownSwitchPin, INPUT_PULLUP);
  72.   pinMode(relayPin, OUTPUT);
  73.  
  74.   // Initialize relay
  75.   digitalWrite(relayPin, LOW); // Relay off
  76.  
  77.   // Initialize temperature sensor
  78.   sensors.begin();
  79.  
  80.   // Initial status message
  81.   Serial.println("System initialized");
  82. }
  83.  
  84. void loop() {
  85.   handleSwitches();
  86.   readTemperature();
  87.   // Optional: Send status over serial for debugging
  88.   Serial.print("Temperature: ");
  89.   Serial.print(temperature);
  90.   Serial.print(" C, Set Time: ");
  91.   Serial.print(setTimeMinutes);
  92.   Serial.print(" min, Status: ");
  93.   Serial.println(systemRunning ? "RUNNING" : "STOPPED");
  94.   delay(200); // Loop delay
  95. }
  96.  
  97. void handleSwitches() {
  98.   // Read switch states
  99.   bool startState = digitalRead(startSwitchPin) == LOW;
  100.   bool stopState = digitalRead(stopSwitchPin) == LOW;
  101.   bool timeUpState = digitalRead(timeUpSwitchPin) == LOW;
  102.   bool timeDownState = digitalRead(timeDownSwitchPin) == LOW;
  103.  
  104.   if (startState) {
  105.     systemRunning = true;
  106.     digitalWrite(relayPin, HIGH); // Turn relay on
  107.   }
  108.   if (stopState) {
  109.     systemRunning = false;
  110.     digitalWrite(relayPin, LOW); // Turn relay off
  111.   }
  112.   if (timeUpState) {
  113.     setTimeMinutes++;
  114.   }
  115.   if (timeDownState) {
  116.     if (setTimeMinutes > 0) {
  117.       setTimeMinutes--;
  118.     }
  119.   }
  120. }
  121.  
  122. void readTemperature() {
  123.   sensors.requestTemperatures();
  124.   temperature = sensors.getTempCByIndex(0);
  125. }
  126.  
Advertisement
Add Comment
Please, Sign In to add comment