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: Connection Troubleshooting
- - Source Code NOT compiled for: ESP32 DevKit V1
- - Source Code created on: 2025-08-13 13:29:59
- ********* 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 *****/
- /********* User code review feedback **********
- #### Feedback 1 ####
- - wifi not seen
- ********* User code review feedback **********/
- /* START CODE */
- /****** DEFINITION OF LIBRARIES *****/
- #include <Wire.h>
- #include <LiquidCrystal_I2C.h>
- #include <WiFi.h>
- /****** FUNCTION PROTOTYPES *****/
- void setup(void);
- void loop(void);
- void initializeHardware();
- void handleSwitches();
- void startOperation();
- void stopOperation();
- void increaseTime();
- void decreaseTime();
- void controlRelay();
- void initializeWiFi();
- #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 (converted to minutes for easier user control)
- unsigned long delay1 = 1 * 60000; // Default 1 minute in milliseconds, min 1 min, max 5 min
- unsigned long delay2 = 10 * 60000; // Default 10 minutes in milliseconds, min 1 min, max 10 min
- // State variables
- bool operationRunning = false;
- bool relayActive = false; // To manage relay state within the loop
- // Initialize LCD
- LiquidCrystal_I2C lcd(I2C_ADDRESS, 16, 2);
- void setup(void)
- {
- initializeHardware();
- initializeWiFi();
- }
- void loop(void)
- {
- handleSwitches();
- if (operationRunning)
- {
- controlRelay();
- }
- }
- 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 initializeWiFi()
- {
- const char* ssid = "your_SSID"; // Replace with your network SSID
- const char* password = "your_PASSWORD"; // Replace with your network password
- WiFi.begin(ssid, password);
- Serial.print("Connecting to WiFi");
- while (WiFi.status() != WL_CONNECTED)
- {
- delay(500);
- Serial.print(".");
- }
- Serial.println("WiFi connected");
- Serial.print("IP address: ");
- Serial.println(WiFi.localIP());
- }
- 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");
- }
- }
- void stopOperation()
- {
- if (operationRunning)
- {
- operationRunning = false;
- digitalWrite(relayPin, LOW);
- lcd.clear();
- lcd.setCursor(0, 0);
- lcd.print("Operation Stop");
- }
- }
- void increaseTime()
- {
- // Increase delay1 and delay2 within limits
- if (delay1 < 5 * 60000) // Max 5 minutes
- {
- delay1 += 60000; // Increase by 1 minute
- }
- if (delay2 < 10 * 60000) // Max 10 minutes
- {
- delay2 += 60000; // Increase by 1 minute
- }
- // Display updated delays
- 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
- }
- // Display updated delays
- 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()
- {
- static unsigned long lastToggleTime = 0;
- unsigned long currentTime = millis();
- if (!relayActive)
- {
- // Turn relay on
- digitalWrite(relayPin, HIGH);
- relayActive = true;
- lastToggleTime = currentTime;
- lcd.clear();
- lcd.setCursor(0, 0);
- lcd.print("Relay ON");
- }
- else
- {
- // Relay is ON, check if delay1 passed to turn off
- if (currentTime - lastToggleTime >= delay1)
- {
- // Turn relay off
- digitalWrite(relayPin, LOW);
- relayActive = false;
- lastToggleTime = currentTime;
- lcd.clear();
- lcd.setCursor(0, 0);
- lcd.print("Relay OFF");
- }
- }
- // This function is called repeatedly in loop(), so it manages timing internally
- }
Advertisement
Add Comment
Please, Sign In to add comment