Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /********* Pleasedontcode.com **********
- Pleasedontcode thanks you for automatic code generation! Enjoy your code!
- - Terms and Conditions:
- You have a non-exclusive, revocable, worldwide, royalty-free license
- for personal and commercial use. Attribution is optional; modifications
- are allowed, but you're responsible for code maintenance. We're not
- liable for any loss or damage. For full terms,
- please visit pleasedontcode.com/termsandconditions.
- - Project: Relay Controller
- - Source Code NOT compiled for: ESP32 DevKit V1
- - Source Code created on: 2025-08-13 13:23:08
- ********* Pleasedontcode.com **********/
- /****** SYSTEM REQUIREMENTS *****/
- /****** SYSTEM REQUIREMENT 1 *****/
- /* esp32 with i2c lcd display.swich 1 for start.swich */
- /* 2 for stop,swich 3 for time setting up,swich 4 for */
- /* time setting down.relay 1 out put.delay 1 time is */
- /* 1 to 5 minutes,delay 2 time is 1 to 10 minutes. */
- /****** END SYSTEM REQUIREMENTS *****/
- /* START CODE */
- /****** DEFINITION OF LIBRARIES *****/
- #include <Wire.h>
- #include <LiquidCrystal_I2C.h>
- /****** FUNCTION PROTOTYPES *****/
- void setup(void);
- void loop(void);
- void initializeHardware();
- void handleSwitches();
- void startOperation();
- void stopOperation();
- void increaseTime();
- void decreaseTime();
- void controlRelay();
- #define I2C_ADDRESS 0x27 // Common I2C address for LCD, change if different
- // Pin definitions
- const int switchStartPin = 12; // Switch 1 for start
- const int switchStopPin = 13; // Switch 2 for stop
- const int switchUpPin = 14; // Switch 3 for time setting up
- const int switchDownPin = 15; // Switch 4 for time setting down
- const int relayPin = 16; // Relay output pin
- // Variables for timing
- unsigned long delay1 = 60000; // Default 1 minute in milliseconds
- unsigned long delay2 = 600000; // Default 10 minutes in milliseconds
- // State variables
- bool operationRunning = false;
- // Initialize LCD
- LiquidCrystal_I2C lcd(I2C_ADDRESS, 16, 2);
- void setup(void)
- {
- initializeHardware();
- }
- void loop(void)
- {
- handleSwitches();
- // Additional logic can be added here
- }
- void initializeHardware()
- {
- // Initialize serial communication for debugging
- Serial.begin(115200);
- // Initialize LCD
- lcd.init();
- lcd.backlight();
- lcd.clear();
- lcd.setCursor(0, 0);
- lcd.print("System Ready");
- // Initialize switch pins
- pinMode(switchStartPin, INPUT_PULLUP);
- pinMode(switchStopPin, INPUT_PULLUP);
- pinMode(switchUpPin, INPUT_PULLUP);
- pinMode(switchDownPin, INPUT_PULLUP);
- // Initialize relay pin
- pinMode(relayPin, OUTPUT);
- digitalWrite(relayPin, LOW);
- }
- void handleSwitches()
- {
- // Read switch states (active LOW)
- if (digitalRead(switchStartPin) == LOW)
- {
- startOperation();
- delay(300); // Debounce delay
- }
- if (digitalRead(switchStopPin) == LOW)
- {
- stopOperation();
- delay(300);
- }
- if (digitalRead(switchUpPin) == LOW)
- {
- increaseTime();
- delay(300);
- }
- if (digitalRead(switchDownPin) == LOW)
- {
- decreaseTime();
- delay(300);
- }
- }
- void startOperation()
- {
- if (!operationRunning)
- {
- operationRunning = true;
- lcd.clear();
- lcd.setCursor(0, 0);
- lcd.print("Operation Start");
- controlRelay();
- }
- }
- void stopOperation()
- {
- if (operationRunning)
- {
- operationRunning = false;
- lcd.clear();
- lcd.setCursor(0, 0);
- lcd.print("Operation Stop");
- digitalWrite(relayPin, LOW);
- }
- }
- void increaseTime()
- {
- // Increase delay1 and delay2 within limits
- if (delay1 < 300000) // Max 5 minutes
- {
- delay1 += 60000; // Increase by 1 minute
- }
- if (delay2 < 600000) // Max 10 minutes
- {
- delay2 += 60000; // Increase by 1 minute
- }
- lcd.clear();
- lcd.setCursor(0, 0);
- lcd.print("Delay1:");
- lcd.print(delay1 / 60000);
- lcd.setCursor(0, 1);
- lcd.print("Delay2:");
- lcd.print(delay2 / 60000);
- delay(500);
- }
- void decreaseTime()
- {
- // Decrease delay1 and delay2 within limits
- if (delay1 > 60000) // Minimum 1 minute
- {
- delay1 -= 60000; // Decrease by 1 minute
- }
- if (delay2 > 60000) // Minimum 1 minute
- {
- delay2 -= 60000; // Decrease by 1 minute
- }
- lcd.clear();
- lcd.setCursor(0, 0);
- lcd.print("Delay1:");
- lcd.print(delay1 / 60000);
- lcd.setCursor(0, 1);
- lcd.print("Delay2:");
- lcd.print(delay2 / 60000);
- delay(500);
- }
- void controlRelay()
- {
- if (operationRunning)
- {
- // Turn relay on
- digitalWrite(relayPin, HIGH);
- lcd.clear();
- lcd.setCursor(0, 0);
- lcd.print("Relay ON");
- delay(delay1);
- // Turn relay off
- digitalWrite(relayPin, LOW);
- lcd.clear();
- lcd.setCursor(0, 0);
- lcd.print("Relay OFF");
- delay(delay2);
- // Repeat or stop based on additional logic
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment