pleasedontcode

Display Controller rev_03

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