pleasedontcode

Connection Troubleshooting rev_02

Aug 13th, 2025
629
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: Connection Troubleshooting
  13.     - Source Code NOT compiled for: ESP32 DevKit V1
  14.     - Source Code created on: 2025-08-13 13:29:59
  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.  
  27. /********* User code review feedback **********
  28. #### Feedback 1 ####
  29. - wifi not seen
  30.  
  31. ********* User code review feedback **********/
  32.  
  33. /* START CODE */
  34. /****** DEFINITION OF LIBRARIES *****/
  35. #include <Wire.h>
  36. #include <LiquidCrystal_I2C.h>
  37. #include <WiFi.h>
  38.  
  39. /****** FUNCTION PROTOTYPES *****/
  40. void setup(void);
  41. void loop(void);
  42. void initializeHardware();
  43. void handleSwitches();
  44. void startOperation();
  45. void stopOperation();
  46. void increaseTime();
  47. void decreaseTime();
  48. void controlRelay();
  49. void initializeWiFi();
  50.  
  51. #define I2C_ADDRESS 0x27 // Common I2C address for LCD, change if different
  52.  
  53. // Pin definitions
  54. const int switchStartPin = 12;    // Switch 1 for start
  55. const int switchStopPin = 13;     // Switch 2 for stop
  56. const int switchUpPin = 14;       // Switch 3 for time setting up
  57. const int switchDownPin = 15;     // Switch 4 for time setting down
  58. const int relayPin = 16;          // Relay output pin
  59.  
  60. // Variables for timing (converted to minutes for easier user control)
  61. unsigned long delay1 = 1 * 60000; // Default 1 minute in milliseconds, min 1 min, max 5 min
  62. unsigned long delay2 = 10 * 60000; // Default 10 minutes in milliseconds, min 1 min, max 10 min
  63.  
  64. // State variables
  65. bool operationRunning = false;
  66. bool relayActive = false;  // To manage relay state within the loop
  67.  
  68. // Initialize LCD
  69. LiquidCrystal_I2C lcd(I2C_ADDRESS, 16, 2);
  70.  
  71. void setup(void)
  72. {
  73.   initializeHardware();
  74.   initializeWiFi();
  75. }
  76.  
  77. void loop(void)
  78. {
  79.   handleSwitches();
  80.  
  81.   if (operationRunning)
  82.   {
  83.     controlRelay();
  84.   }
  85. }
  86.  
  87. void initializeHardware()
  88. {
  89.   // Initialize serial communication for debugging
  90.   Serial.begin(115200);
  91.  
  92.   // Initialize LCD
  93.   lcd.init();
  94.   lcd.backlight();
  95.   lcd.clear();
  96.   lcd.setCursor(0, 0);
  97.   lcd.print("System Ready");
  98.  
  99.   // Initialize switch pins
  100.   pinMode(switchStartPin, INPUT_PULLUP);
  101.   pinMode(switchStopPin, INPUT_PULLUP);
  102.   pinMode(switchUpPin, INPUT_PULLUP);
  103.   pinMode(switchDownPin, INPUT_PULLUP);
  104.  
  105.   // Initialize relay pin
  106.   pinMode(relayPin, OUTPUT);
  107.   digitalWrite(relayPin, LOW);
  108. }
  109.  
  110. void initializeWiFi()
  111. {
  112.   const char* ssid = "your_SSID";       // Replace with your network SSID
  113.   const char* password = "your_PASSWORD"; // Replace with your network password
  114.   WiFi.begin(ssid, password);
  115.   Serial.print("Connecting to WiFi");
  116.   while (WiFi.status() != WL_CONNECTED)
  117.   {
  118.     delay(500);
  119.     Serial.print(".");
  120.   }
  121.   Serial.println("WiFi connected");
  122.   Serial.print("IP address: ");
  123.   Serial.println(WiFi.localIP());
  124. }
  125.  
  126. void handleSwitches()
  127. {
  128.   // Read switch states (active LOW)
  129.   if (digitalRead(switchStartPin) == LOW)
  130.   {
  131.     startOperation();
  132.     delay(300); // Debounce delay
  133.   }
  134.   if (digitalRead(switchStopPin) == LOW)
  135.   {
  136.     stopOperation();
  137.     delay(300);
  138.   }
  139.   if (digitalRead(switchUpPin) == LOW)
  140.   {
  141.     increaseTime();
  142.     delay(300);
  143.   }
  144.   if (digitalRead(switchDownPin) == LOW)
  145.   {
  146.     decreaseTime();
  147.     delay(300);
  148.   }
  149. }
  150.  
  151. void startOperation()
  152. {
  153.   if (!operationRunning)
  154.   {
  155.     operationRunning = true;
  156.     lcd.clear();
  157.     lcd.setCursor(0, 0);
  158.     lcd.print("Operation Start");
  159.   }
  160. }
  161.  
  162. void stopOperation()
  163. {
  164.   if (operationRunning)
  165.   {
  166.     operationRunning = false;
  167.     digitalWrite(relayPin, LOW);
  168.     lcd.clear();
  169.     lcd.setCursor(0, 0);
  170.     lcd.print("Operation Stop");
  171.   }
  172. }
  173.  
  174. void increaseTime()
  175. {
  176.   // Increase delay1 and delay2 within limits
  177.   if (delay1 < 5 * 60000) // Max 5 minutes
  178.   {
  179.     delay1 += 60000; // Increase by 1 minute
  180.   }
  181.   if (delay2 < 10 * 60000) // Max 10 minutes
  182.   {
  183.     delay2 += 60000; // Increase by 1 minute
  184.   }
  185.   // Display updated delays
  186.   lcd.clear();
  187.   lcd.setCursor(0, 0);
  188.   lcd.print("Delay1:");
  189.   lcd.print(delay1 / 60000);
  190.   lcd.setCursor(0, 1);
  191.   lcd.print("Delay2:");
  192.   lcd.print(delay2 / 60000);
  193.   delay(500);
  194. }
  195.  
  196. void decreaseTime()
  197. {
  198.   // Decrease delay1 and delay2 within limits
  199.   if (delay1 > 60000) // Minimum 1 minute
  200.   {
  201.     delay1 -= 60000; // Decrease by 1 minute
  202.   }
  203.   if (delay2 > 60000) // Minimum 1 minute
  204.   {
  205.     delay2 -= 60000; // Decrease by 1 minute
  206.   }
  207.   // Display updated delays
  208.   lcd.clear();
  209.   lcd.setCursor(0, 0);
  210.   lcd.print("Delay1:");
  211.   lcd.print(delay1 / 60000);
  212.   lcd.setCursor(0, 1);
  213.   lcd.print("Delay2:");
  214.   lcd.print(delay2 / 60000);
  215.   delay(500);
  216. }
  217.  
  218. void controlRelay()
  219. {
  220.   static unsigned long lastToggleTime = 0;
  221.   unsigned long currentTime = millis();
  222.  
  223.   if (!relayActive)
  224.   {
  225.     // Turn relay on
  226.     digitalWrite(relayPin, HIGH);
  227.     relayActive = true;
  228.     lastToggleTime = currentTime;
  229.     lcd.clear();
  230.     lcd.setCursor(0, 0);
  231.     lcd.print("Relay ON");
  232.   }
  233.   else
  234.   {
  235.     // Relay is ON, check if delay1 passed to turn off
  236.     if (currentTime - lastToggleTime >= delay1)
  237.     {
  238.       // Turn relay off
  239.       digitalWrite(relayPin, LOW);
  240.       relayActive = false;
  241.       lastToggleTime = currentTime;
  242.       lcd.clear();
  243.       lcd.setCursor(0, 0);
  244.       lcd.print("Relay OFF");
  245.     }
  246.   }
  247.  
  248.   // This function is called repeatedly in loop(), so it manages timing internally
  249. }
  250.  
Advertisement
Add Comment
Please, Sign In to add comment