pleasedontcode

Relay Controller rev_01

Aug 13th, 2025
608
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: ESP32 DevKit V1
  14.     - Source Code created on: 2025-08-13 13:23:08
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* esp32 with i2c lcd display.swich 1 for start.swich */
  21.     /* 2 for stop,swich 3 for time setting up,swich 4 for */
  22.     /* time setting down.relay 1 out put.delay 1 time is */
  23.     /* 1 to 5 minutes,delay 2 time is 1 to 10 minutes. */
  24. /****** END SYSTEM REQUIREMENTS *****/
  25.  
  26. /* START CODE */
  27. /****** DEFINITION OF LIBRARIES *****/
  28. #include <Wire.h>
  29. #include <LiquidCrystal_I2C.h>
  30.  
  31. /****** FUNCTION PROTOTYPES *****/
  32. void setup(void);
  33. void loop(void);
  34. void initializeHardware();
  35. void handleSwitches();
  36. void startOperation();
  37. void stopOperation();
  38. void increaseTime();
  39. void decreaseTime();
  40. void controlRelay();
  41.  
  42. #define I2C_ADDRESS 0x27 // Common I2C address for LCD, change if different
  43.  
  44. // Pin definitions
  45. const int switchStartPin = 12;    // Switch 1 for start
  46. const int switchStopPin = 13;     // Switch 2 for stop
  47. const int switchUpPin = 14;       // Switch 3 for time setting up
  48. const int switchDownPin = 15;     // Switch 4 for time setting down
  49. const int relayPin = 16;          // Relay output pin
  50.  
  51. // Variables for timing
  52. unsigned long delay1 = 60000; // Default 1 minute in milliseconds
  53. unsigned long delay2 = 600000; // Default 10 minutes in milliseconds
  54.  
  55. // State variables
  56. bool operationRunning = false;
  57.  
  58. // Initialize LCD
  59. LiquidCrystal_I2C lcd(I2C_ADDRESS, 16, 2);
  60.  
  61. void setup(void)
  62. {
  63.   initializeHardware();
  64. }
  65.  
  66. void loop(void)
  67. {
  68.   handleSwitches();
  69.   // Additional logic can be added here
  70. }
  71.  
  72. void initializeHardware()
  73. {
  74.   // Initialize serial communication for debugging
  75.   Serial.begin(115200);
  76.  
  77.   // Initialize LCD
  78.   lcd.init();
  79.   lcd.backlight();
  80.   lcd.clear();
  81.   lcd.setCursor(0, 0);
  82.   lcd.print("System Ready");
  83.  
  84.   // Initialize switch pins
  85.   pinMode(switchStartPin, INPUT_PULLUP);
  86.   pinMode(switchStopPin, INPUT_PULLUP);
  87.   pinMode(switchUpPin, INPUT_PULLUP);
  88.   pinMode(switchDownPin, INPUT_PULLUP);
  89.  
  90.   // Initialize relay pin
  91.   pinMode(relayPin, OUTPUT);
  92.   digitalWrite(relayPin, LOW);
  93. }
  94.  
  95. void handleSwitches()
  96. {
  97.   // Read switch states (active LOW)
  98.   if (digitalRead(switchStartPin) == LOW)
  99.   {
  100.     startOperation();
  101.     delay(300); // Debounce delay
  102.   }
  103.   if (digitalRead(switchStopPin) == LOW)
  104.   {
  105.     stopOperation();
  106.     delay(300);
  107.   }
  108.   if (digitalRead(switchUpPin) == LOW)
  109.   {
  110.     increaseTime();
  111.     delay(300);
  112.   }
  113.   if (digitalRead(switchDownPin) == LOW)
  114.   {
  115.     decreaseTime();
  116.     delay(300);
  117.   }
  118. }
  119.  
  120. void startOperation()
  121. {
  122.   if (!operationRunning)
  123.   {
  124.     operationRunning = true;
  125.     lcd.clear();
  126.     lcd.setCursor(0, 0);
  127.     lcd.print("Operation Start");
  128.     controlRelay();
  129.   }
  130. }
  131.  
  132. void stopOperation()
  133. {
  134.   if (operationRunning)
  135.   {
  136.     operationRunning = false;
  137.     lcd.clear();
  138.     lcd.setCursor(0, 0);
  139.     lcd.print("Operation Stop");
  140.     digitalWrite(relayPin, LOW);
  141.   }
  142. }
  143.  
  144. void increaseTime()
  145. {
  146.   // Increase delay1 and delay2 within limits
  147.   if (delay1 < 300000) // Max 5 minutes
  148.   {
  149.     delay1 += 60000; // Increase by 1 minute
  150.   }
  151.   if (delay2 < 600000) // Max 10 minutes
  152.   {
  153.     delay2 += 60000; // Increase by 1 minute
  154.   }
  155.   lcd.clear();
  156.   lcd.setCursor(0, 0);
  157.   lcd.print("Delay1:");
  158.   lcd.print(delay1 / 60000);
  159.   lcd.setCursor(0, 1);
  160.   lcd.print("Delay2:");
  161.   lcd.print(delay2 / 60000);
  162.   delay(500);
  163. }
  164.  
  165. void decreaseTime()
  166. {
  167.   // Decrease delay1 and delay2 within limits
  168.   if (delay1 > 60000) // Minimum 1 minute
  169.   {
  170.     delay1 -= 60000; // Decrease by 1 minute
  171.   }
  172.   if (delay2 > 60000) // Minimum 1 minute
  173.   {
  174.     delay2 -= 60000; // Decrease by 1 minute
  175.   }
  176.   lcd.clear();
  177.   lcd.setCursor(0, 0);
  178.   lcd.print("Delay1:");
  179.   lcd.print(delay1 / 60000);
  180.   lcd.setCursor(0, 1);
  181.   lcd.print("Delay2:");
  182.   lcd.print(delay2 / 60000);
  183.   delay(500);
  184. }
  185.  
  186. void controlRelay()
  187. {
  188.   if (operationRunning)
  189.   {
  190.     // Turn relay on
  191.     digitalWrite(relayPin, HIGH);
  192.     lcd.clear();
  193.     lcd.setCursor(0, 0);
  194.     lcd.print("Relay ON");
  195.     delay(delay1);
  196.     // Turn relay off
  197.     digitalWrite(relayPin, LOW);
  198.     lcd.clear();
  199.     lcd.setCursor(0, 0);
  200.     lcd.print("Relay OFF");
  201.     delay(delay2);
  202.     // Repeat or stop based on additional logic
  203.   }
  204. }
  205.  
Advertisement
Add Comment
Please, Sign In to add comment