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: ESP8266 NodeMCU V1.0
- - Source Code created on: 2025-08-13 13:05:56
- ********* Pleasedontcode.com **********/
- /****** SYSTEM REQUIREMENTS *****/
- /****** SYSTEM REQUIREMENT 1 *****/
- /* esp32 with i2c display, swich 1 for start */
- /* button,swich 2 for stop button,swich 3 for delay */
- /* time setting up,swich 4 for delay time setting */
- /* down.relay 1 output. */
- /****** END SYSTEM REQUIREMENTS *****/
- /* START CODE */
- /****** DEFINITION OF LIBRARIES *****/
- // Include Wire library for I2C communication
- #include <Wire.h>
- // Include a library for the I2C display (assuming SSD1306 or similar, adjust if needed)
- #include <Adafruit_SSD1306.h>
- // Define display parameters
- #define SCREEN_WIDTH 128 // OLED display width, in pixels
- #define SCREEN_HEIGHT 64 // OLED display height, in pixels
- #define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
- Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
- // Define pin assignments
- const int buttonStartPin = D1; // Switch 1 for start
- const int buttonStopPin = D2; // Switch 2 for stop
- const int buttonDelayPin = D3; // Switch 3 for delay time setting
- const int switchDelayPin = D4; // Switch 4 for delay time decrease
- const int relayPin = D5; // Relay output
- // Variables for button states
- bool isRunning = false;
- unsigned long delayTime = 1000; // Default delay time in milliseconds
- unsigned long previousMillis = 0;
- // Variables to handle button debouncing
- unsigned long lastDebounceTimeStart = 0;
- unsigned long lastDebounceTimeStop = 0;
- unsigned long lastDebounceTimeDelay = 0;
- unsigned long lastDebounceTimeDelayDown = 0;
- const unsigned long debounceDelay = 50; // milliseconds
- // Variables for buttons state
- int lastStartButtonState = HIGH;
- int lastStopButtonState = HIGH;
- int lastDelayButtonState = HIGH;
- int lastDelayDownButtonState = HIGH;
- void setup() {
- // Initialize serial communication for debugging
- Serial.begin(115200);
- // Initialize display
- if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3C for 128x64
- Serial.println(F("SSD1306 allocation failed"));
- for(;;); // Don't proceed, loop forever
- }
- display.clearDisplay();
- display.setTextSize(1);
- display.setTextColor(SSD1306_WHITE);
- display.setCursor(0,0);
- display.println("System Initializing");
- display.display();
- // Initialize pins
- pinMode(buttonStartPin, INPUT_PULLUP);
- pinMode(buttonStopPin, INPUT_PULLUP);
- pinMode(buttonDelayPin, INPUT_PULLUP);
- pinMode(switchDelayPin, INPUT_PULLUP);
- pinMode(relayPin, OUTPUT);
- // Initialize relay state to off
- digitalWrite(relayPin, LOW);
- // Show initial message
- display.clearDisplay();
- display.setCursor(0,0);
- display.println("Ready");
- display.display();
- }
- void loop() {
- unsigned long currentMillis = millis();
- // Read buttons with debounce
- handleButtonStart(currentMillis);
- handleButtonStop(currentMillis);
- handleDelayAdjustment(currentMillis);
- // If start button pressed, start operation
- if (isRunning) {
- // Turn relay on
- digitalWrite(relayPin, HIGH);
- // Perform delay with non-blocking approach
- if (currentMillis - previousMillis >= delayTime) {
- previousMillis = currentMillis;
- // Toggle relay or perform other actions if needed
- // For continuous on/off, implement toggle here
- }
- } else {
- // Turn relay off
- digitalWrite(relayPin, LOW);
- }
- // Update display periodically
- static unsigned long lastDisplayUpdate = 0;
- if (currentMillis - lastDisplayUpdate > 500) { // update every 500ms
- lastDisplayUpdate = currentMillis;
- updateDisplay();
- }
- }
- // Handle start button
- void handleButtonStart(unsigned long currentMillis) {
- int reading = digitalRead(buttonStartPin);
- if (reading != lastStartButtonState) {
- lastDebounceTimeStart = currentMillis;
- }
- if ((currentMillis - lastDebounceTimeStart) > debounceDelay) {
- if (reading == LOW && lastStartButtonState == HIGH) {
- // Button pressed
- isRunning = true;
- }
- }
- lastStartButtonState = reading;
- }
- // Handle stop button
- void handleButtonStop(unsigned long currentMillis) {
- int reading = digitalRead(buttonStopPin);
- if (reading != lastStopButtonState) {
- lastDebounceTimeStop = currentMillis;
- }
- if ((currentMillis - lastDebounceTimeStop) > debounceDelay) {
- if (reading == LOW && lastStopButtonState == HIGH) {
- // Button pressed
- isRunning = false;
- }
- }
- lastStopButtonState = reading;
- }
- // Handle delay adjustment
- void handleDelayAdjustment(unsigned long currentMillis) {
- int delayButtonState = digitalRead(buttonDelayPin);
- int delayDownState = digitalRead(switchDelayPin);
- // Increase delay time
- if (delayButtonState != lastDelayButtonState) {
- lastDebounceTimeDelay = currentMillis;
- }
- if ((currentMillis - lastDebounceTimeDelay) > debounceDelay) {
- if (delayButtonState == LOW && lastDelayButtonState == HIGH) {
- delayTime += 100; // increase delay by 100ms
- }
- }
- lastDelayButtonState = delayButtonState;
- // Decrease delay time
- if (delayDownState != lastDelayDownButtonState) {
- lastDebounceTimeDelayDown = currentMillis;
- }
- if ((currentMillis - lastDebounceTimeDelayDown) > debounceDelay) {
- if (switchDelayPin == LOW && lastDelayDownButtonState == HIGH) {
- if (delayTime > 100) {
- delayTime -= 100; // decrease delay by 100ms
- }
- }
- }
- lastDelayDownButtonState = delayDownState;
- }
- // Update OLED display
- void updateDisplay() {
- display.clearDisplay();
- display.setCursor(0,0);
- display.print("Start: ");
- display.println(isRunning ? "Yes" : "No");
- display.print("Delay: ");
- display.print(delayTime);
- display.println(" ms");
- display.display();
- }
Advertisement
Add Comment
Please, Sign In to add comment