pleasedontcode

Relay Controller rev_02

Aug 13th, 2025
583
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: ESP8266 NodeMCU V1.0
  14.     - Source Code created on: 2025-08-13 13:05:56
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* esp32 with i2c display, swich 1 for start */
  21.     /* button,swich 2 for stop button,swich 3 for delay */
  22.     /* time setting up,swich 4 for delay time setting */
  23.     /* down.relay 1 output. */
  24. /****** END SYSTEM REQUIREMENTS *****/
  25.  
  26. /* START CODE */
  27. /****** DEFINITION OF LIBRARIES *****/
  28. // Include Wire library for I2C communication
  29. #include <Wire.h>
  30. // Include a library for the I2C display (assuming SSD1306 or similar, adjust if needed)
  31. #include <Adafruit_SSD1306.h>
  32.  
  33. // Define display parameters
  34. #define SCREEN_WIDTH 128 // OLED display width, in pixels
  35. #define SCREEN_HEIGHT 64 // OLED display height, in pixels
  36. #define OLED_RESET -1   // Reset pin # (or -1 if sharing Arduino reset pin)
  37. Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
  38.  
  39. // Define pin assignments
  40. const int buttonStartPin = D1;   // Switch 1 for start
  41. const int buttonStopPin = D2;    // Switch 2 for stop
  42. const int buttonDelayPin = D3;   // Switch 3 for delay time setting
  43. const int switchDelayPin = D4;   // Switch 4 for delay time decrease
  44. const int relayPin = D5;         // Relay output
  45.  
  46. // Variables for button states
  47. bool isRunning = false;
  48. unsigned long delayTime = 1000; // Default delay time in milliseconds
  49. unsigned long previousMillis = 0;
  50.  
  51. // Variables to handle button debouncing
  52. unsigned long lastDebounceTimeStart = 0;
  53. unsigned long lastDebounceTimeStop = 0;
  54. unsigned long lastDebounceTimeDelay = 0;
  55. unsigned long lastDebounceTimeDelayDown = 0;
  56. const unsigned long debounceDelay = 50; // milliseconds
  57.  
  58. // Variables for buttons state
  59. int lastStartButtonState = HIGH;
  60. int lastStopButtonState = HIGH;
  61. int lastDelayButtonState = HIGH;
  62. int lastDelayDownButtonState = HIGH;
  63.  
  64. void setup() {
  65.   // Initialize serial communication for debugging
  66.   Serial.begin(115200);
  67.  
  68.   // Initialize display
  69.   if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3C for 128x64
  70.     Serial.println(F("SSD1306 allocation failed"));
  71.     for(;;); // Don't proceed, loop forever
  72.   }
  73.   display.clearDisplay();
  74.   display.setTextSize(1);
  75.   display.setTextColor(SSD1306_WHITE);
  76.   display.setCursor(0,0);
  77.   display.println("System Initializing");
  78.   display.display();
  79.  
  80.   // Initialize pins
  81.   pinMode(buttonStartPin, INPUT_PULLUP);
  82.   pinMode(buttonStopPin, INPUT_PULLUP);
  83.   pinMode(buttonDelayPin, INPUT_PULLUP);
  84.   pinMode(switchDelayPin, INPUT_PULLUP);
  85.   pinMode(relayPin, OUTPUT);
  86.  
  87.   // Initialize relay state to off
  88.   digitalWrite(relayPin, LOW);
  89.  
  90.   // Show initial message
  91.   display.clearDisplay();
  92.   display.setCursor(0,0);
  93.   display.println("Ready");
  94.   display.display();
  95. }
  96.  
  97. void loop() {
  98.   unsigned long currentMillis = millis();
  99.  
  100.   // Read buttons with debounce
  101.   handleButtonStart(currentMillis);
  102.   handleButtonStop(currentMillis);
  103.   handleDelayAdjustment(currentMillis);
  104.  
  105.   // If start button pressed, start operation
  106.   if (isRunning) {
  107.     // Turn relay on
  108.     digitalWrite(relayPin, HIGH);
  109.  
  110.     // Perform delay with non-blocking approach
  111.     if (currentMillis - previousMillis >= delayTime) {
  112.       previousMillis = currentMillis;
  113.       // Toggle relay or perform other actions if needed
  114.       // For continuous on/off, implement toggle here
  115.     }
  116.   } else {
  117.     // Turn relay off
  118.     digitalWrite(relayPin, LOW);
  119.   }
  120.  
  121.   // Update display periodically
  122.   static unsigned long lastDisplayUpdate = 0;
  123.   if (currentMillis - lastDisplayUpdate > 500) { // update every 500ms
  124.     lastDisplayUpdate = currentMillis;
  125.     updateDisplay();
  126.   }
  127. }
  128.  
  129. // Handle start button
  130. void handleButtonStart(unsigned long currentMillis) {
  131.   int reading = digitalRead(buttonStartPin);
  132.   if (reading != lastStartButtonState) {
  133.     lastDebounceTimeStart = currentMillis;
  134.   }
  135.   if ((currentMillis - lastDebounceTimeStart) > debounceDelay) {
  136.     if (reading == LOW && lastStartButtonState == HIGH) {
  137.       // Button pressed
  138.       isRunning = true;
  139.     }
  140.   }
  141.   lastStartButtonState = reading;
  142. }
  143.  
  144. // Handle stop button
  145. void handleButtonStop(unsigned long currentMillis) {
  146.   int reading = digitalRead(buttonStopPin);
  147.   if (reading != lastStopButtonState) {
  148.     lastDebounceTimeStop = currentMillis;
  149.   }
  150.   if ((currentMillis - lastDebounceTimeStop) > debounceDelay) {
  151.     if (reading == LOW && lastStopButtonState == HIGH) {
  152.       // Button pressed
  153.       isRunning = false;
  154.     }
  155.   }
  156.   lastStopButtonState = reading;
  157. }
  158.  
  159. // Handle delay adjustment
  160. void handleDelayAdjustment(unsigned long currentMillis) {
  161.   int delayButtonState = digitalRead(buttonDelayPin);
  162.   int delayDownState = digitalRead(switchDelayPin);
  163.  
  164.   // Increase delay time
  165.   if (delayButtonState != lastDelayButtonState) {
  166.     lastDebounceTimeDelay = currentMillis;
  167.   }
  168.   if ((currentMillis - lastDebounceTimeDelay) > debounceDelay) {
  169.     if (delayButtonState == LOW && lastDelayButtonState == HIGH) {
  170.       delayTime += 100; // increase delay by 100ms
  171.     }
  172.   }
  173.   lastDelayButtonState = delayButtonState;
  174.  
  175.   // Decrease delay time
  176.   if (delayDownState != lastDelayDownButtonState) {
  177.     lastDebounceTimeDelayDown = currentMillis;
  178.   }
  179.   if ((currentMillis - lastDebounceTimeDelayDown) > debounceDelay) {
  180.     if (switchDelayPin == LOW && lastDelayDownButtonState == HIGH) {
  181.       if (delayTime > 100) {
  182.         delayTime -= 100; // decrease delay by 100ms
  183.       }
  184.     }
  185.   }
  186.   lastDelayDownButtonState = delayDownState;
  187. }
  188.  
  189. // Update OLED display
  190. void updateDisplay() {
  191.   display.clearDisplay();
  192.   display.setCursor(0,0);
  193.   display.print("Start: ");
  194.   display.println(isRunning ? "Yes" : "No");
  195.   display.print("Delay: ");
  196.   display.print(delayTime);
  197.   display.println(" ms");
  198.   display.display();
  199. }
  200.  
Advertisement
Add Comment
Please, Sign In to add comment