IRF540NSTRLPBF
Controls
Properties
Availability & Pricing
Assets
id=n-channel-mosfet
id=TRANS_IRF540NSTRLPBF
id=IRF540NSTRLPBF
id=TO-263_418AA-01
Simulation
EN
or RST
pin).Key Changes:Cpp
#include #include #include // Or other OLED library #include const int RED_LED_PIN = 26; const int GREEN_LED_PIN = 27; const int BLUE_LED_PIN = 28; const int BUTTON_PIN = 32; const int INTERNAL_LED_PIN = 2; // On-board LED Preferences preferences; RotaryEncoder encoder; SSD1306Wire display(0x3D, SDA, SCL); // Adjust address as needed void setup() { pinMode(RED_LED_PIN, OUTPUT); pinMode(GREEN_LED_PIN, OUTPUT); pinMode(BLUE_LED_PIN, OUTPUT); pinMode(INTERNAL_LED_PIN, OUTPUT); pinMode(BUTTON_PIN, INPUT_PULLUP); digitalWrite(RED_LED_PIN, HIGH); digitalWrite(GREEN_LED_PIN, HIGH); digitalWrite(BLUE_LED_PIN, HIGH); Wire.begin(); display.init(); display.clear(); display.display(); preferences.begin("settings", false); attachInterrupt(digitalPinToInterrupt(BUTTON_PIN), handleButtonPress, FALLING); attachInterrupt(digitalPinToInterrupt(encoder.pinA), encoderTick, CHANGE); attachInterrupt(digitalPinToInterrupt(encoder.pinB), encoderTick, CHANGE); } void loop() { updateDisplay(); if (digitalRead(BUTTON_PIN) == LOW) { digitalWrite(GREEN_LED_PIN, LOW); // Active when button is pressed } else { digitalWrite(GREEN_LED_PIN, HIGH); } manageOutputs(); delay(10); // Simple debounce technique } void handleButtonPress() { static unsigned long lastInterruptTime = 0; unsigned long interruptTime = millis(); if (interruptTime - lastInterruptTime > 200) { // Debounce for 200ms // Handle button press logic } lastInterruptTime = interruptTime; } void encoderTick() { encoder.tick(); // Handle encoder movement } void manageOutputs() { if (digitalRead(INTERNAL_LED_PIN) == LOW) { digitalWrite(BLUE_LED_PIN, LOW); } else { digitalWrite(BLUE_LED_PIN, HIGH); } // Add other output management logic } void updateDisplay() { // Maintain the OLED update logic } void saveSettings() { preferences.putInt("some_setting", 123); // Example setting } void loadSettings() { int setting = preferences.getInt("some_setting", 0); // Default to 0 if not set }
Preferences
for storing settings.Key Features:Cpp
#include // Graphics and touchscreen library for the CYD #include // Instantiate display and preferences TFT_eSPI tft = TFT_eSPI(); Preferences preferences; // Define pins for remaining buttons const int SWITCH_ACTIVATE_PIN = 34; const int RESET_PIN = 35; // Assuming a pin for reset void setup() { preferences.begin("settings", false); // Initialize display tft.init(); tft.setRotation(1); // Adjust based on how your display is oriented tft.fillScreen(TFT_BLACK); pinMode(SWITCH_ACTIVATE_PIN, INPUT_PULLUP); pinMode(RESET_PIN, INPUT_PULLUP); // Attach interrupts if needed attachInterrupt(digitalPinToInterrupt(SWITCH_ACTIVATE_PIN), activateSwitch, FALLING); attachInterrupt(digitalPinToInterrupt(RESET_PIN), resetFunction, FALLING); // Initial display setup displayMainMenu(); } void loop() { if (tft.getTouch(&x, &y)) { handleTouch(x, y); } // Handle additional functionality as needed } void handleTouch(int x, int y) { // Implement touch handling logic based on coordinates if (isWithinBounds(x, y, 100, 200, 50, 100)) { // Example button coordinates activatePulse(); } // Add other touch handling scenarios } void activateSwitch() { // Logic to activate welding pulse } void resetFunction() { // Logic to reset ESP32 } void activatePulse() { // Implement actual pulse generation logic } void displayMainMenu() { tft.fillScreen(TFT_BLACK); tft.setCursor(10, 10); tft.setTextSize(2); tft.setTextColor(TFT_WHITE); tft.println("Main Menu"); // Display more UI elements for interaction } bool isWithinBounds(int x, int y, int x0, int x1, int y0, int y1) { return (x >= x0 && x <= x1 && y >= y0 && y <= y1); }
handleTouch(x, y)
function to manage user input via the touchscreen.Preferences
library for storing settings persistently.handleTouch
function.Additional NotesPlaintext
ESP32 GPIO ----> IN ----> | Gate Driver | ----> OUT ----> MOSFET Gates (Via Gate Resistor and Zener Diodes) VCC --- 12V GND --- System Ground Decoupling Capacitor (100 nF) between VCC and GND
ConclusionPlaintext
ESP32 GPIO ----> Relay Coil (Via Transistor if Necessary) ----> Relay NO Terminal ----> 12V ----> Gate Node (Via Existing Resistor)
ledc
library is commonly used for this purpose.ConsiderationsCpp
#include // Define constants int pwmChannel = 0; int frequency = 5000; // Set a frequency: 5 kHz int resolution = 8; // 8-bit resolution // Define the pin used const int gatePin = 5; // GPIO pin connected to the gate driver void setup() { // Set up PWM on the specified channel and pin ledcSetup(pwmChannel, frequency, resolution); ledcAttachPin(gatePin, pwmChannel); } void loop() { int dutyCycle = 128; // Adjust this for different current levels (0-255 for 8-bit resolution) // Set the PWM duty cycle ledcWrite(pwmChannel, dutyCycle); // Control the pulse duration; here, it's set to 500 ms on-time delay(500); // Turn off the MOSFETs ledcWrite(pwmChannel, 0); // Delay between pulses delay(500); }
Code ExplanationCpp
#include #include #include #include // Pin Definitions #define RED_LED_PIN 26 #define OLED_CS 5 #define OLED_DC 2 #define OLED_RST 15 #define OLED_MOSI 23 #define OLED_CLK 18 #define ROTARY_CLK 32 #define ROTARY_DT 33 #define ROTARY_SW 25 #define SWITCH_BACK 12 #define SWITCH_ACTIVATE 13 #define SWITCH_RESET 14 // Initialize OLED Adafruit_GC9A01 display = Adafruit_GC9A01(OLED_CS, OLED_DC, OLED_RST); // Initialize Rotary Encoder Encoder encoder(ROTARY_CLK, ROTARY_DT); // Menu Variables int delayTime = 100; int pulse1 = 40; int pauseDuration = 20; int pulse2 = 80; int debounceProtocol = 500; void setup() { pinMode(RED_LED_PIN, OUTPUT); pinMode(ROTARY_SW, INPUT_PULLUP); pinMode(SWITCH_BACK, INPUT_PULLUP); pinMode(SWITCH_ACTIVATE, INPUT_PULLUP); pinMode(SWITCH_RESET, INPUT_PULLUP); // Initialize display display.begin(); display.setRotation(0); display.fillScreen(0); // Display initial information displayMenu(); // Initialize serial for debugging Serial.begin(115200); } void loop() { // Check button states if (digitalRead(SWITCH_ACTIVATE) == LOW) { delay(100); if (digitalRead(SWITCH_ACTIVATE) == LOW) { executePulseSequence(); } } // Handle "Select" button logic for menu navigation static unsigned long lastPress = 0; if (digitalRead(ROTARY_SW) == LOW) { if (millis() - lastPress > 2000) { openMenu(); } lastPress = millis(); } // Update display or handle rotary input handleRotaryEncoder(); } void executePulseSequence() { // Ensure "Activate" is still held if (digitalRead(SWITCH_ACTIVATE) == HIGH) { return; // Exit if "Activate" is released } // Turn on LED to signal pulse start digitalWrite(RED_LED_PIN, HIGH); // Execute "Pulse Sequence" delay(delayTime); // Pulse 1 digitalWrite(RED_LED_PIN, HIGH); delay(pulse1); // Pause Duration digitalWrite(RED_LED_PIN, LOW); delay(pauseDuration); // Pulse 2 digitalWrite(RED_LED_PIN, HIGH); delay(pulse2); // Debounce Protocol digitalWrite(RED_LED_PIN, LOW); delay(debounceProtocol); // Turn off LED at the end of the sequence digitalWrite(RED_LED_PIN, LOW); } void displayMenu() { // Code to update and display menu on OLED } void openMenu() { // Code for menu navigation } void handleRotaryEncoder() { // Code to check rotary encoder inputs and update selections long newPos = encoder.read(); if (newPos > 0) { // Increase current menu item encoder.write(0); // Reset encoder } else if (newPos < 0) { // Decrease current menu item encoder.write(0); // Reset encoder } }
Ensure Adjustments Respect These ConstraintsCpp
// Define min and max constants for menu variables const int MIN_DELAY_TIME = 5; const int MAX_DELAY_TIME = 3000; const int MIN_PULSE1 = 10; const int MAX_PULSE1 = 5000; const int MIN_PAUSE_DURATION = 5; const int MAX_PAUSE_DURATION = 500; const int MIN_PULSE2 = 10; const int MAX_PULSE2 = 5000; const int MIN_DEBOUNCE_PROTOCOL = 100; const int MAX_DEBOUNCE_PROTOCOL = 3000;
Integrate into Rotary HandlingCpp
void adjustMenuItem(int &value, int increment, int minValue, int maxValue) { value += increment; if (value < minValue) { value = minValue; } if (value > maxValue) { value = maxValue; } }
handleRotaryEncoder()
function, ensure you apply these constraints:ExplanationCpp
void handleRotaryEncoder() { long newPos = encoder.read(); if (newPos != 0) { // Assume menuIndex points to the current item being modified switch (menuIndex) { case 0: // Delay Time adjustMenuItem(delayTime, newPos, MIN_DELAY_TIME, MAX_DELAY_TIME); break; case 1: // Pulse 1 adjustMenuItem(pulse1, newPos, MIN_PULSE1, MAX_PULSE1); break; case 2: // Pause Duration adjustMenuItem(pauseDuration, newPos, MIN_PAUSE_DURATION, MAX_PAUSE_DURATION); break; case 3: // Pulse 2 adjustMenuItem(pulse2, newPos, MIN_PULSE2, MAX_PULSE2); break; case 4: // Debounce Protocol adjustMenuItem(debounceProtocol, newPos, MIN_DEBOUNCE_PROTOCOL, MAX_DEBOUNCE_PROTOCOL); break; } encoder.write(0); // Reset encoder position displayMenu(); // Update display with new value } }
long newPos = encoder.read();
if (newPos != 0) {
// Assume menuIndex points to the current item being modified
switch (menuIndex) {
case 0: // Delay Time
adjustMenuItem(delayTime, newPos, MIN_DELAY_TIME, MAX_DELAY_TIME);
break;
case 1: // Pulse 1
adjustMenuItem(pulse1, newPos, MIN_PULSE1, MAX_PULSE1);
break;
case 2: // Pause Duration
adjustMenuItem(pauseDuration, newPos, MIN_PAUSE_DURATION, MAX_PAUSE_DURATION);
break;
case 3: // Pulse 2
adjustMenuItem(pulse2, newPos, MIN_PULSE2, MAX_PULSE2);
break;
case 4: // Debounce Protocol
adjustMenuItem(debounceProtocol, newPos, MIN_DEBOUNCE_PROTOCOL, MAX_DEBOUNCE_PROTOCOL);
break;
}
encoder.write(0); // Reset encoder position
displayMenu(); // Update display with new value
}
Preferences
or EEPROM
library. This ensures settings are retained across power cycles.Cpp
bool debounceButton(int pin) { bool currentState = digitalRead(pin); delay(10); // Delay for debounce return currentState == digitalRead(pin); // Only true if stable }
Cpp
#include Preferences preferences; void saveSettings() { preferences.begin("my-settings", false); preferences.putInt("delayTime", delayTime); preferences.putInt("pulse1", pulse1); preferences.putInt("pauseDuration", pauseDuration); preferences.putInt("pulse2", pulse2); preferences.putInt("debounceProtocol", debounceProtocol); preferences.end(); } void loadSettings() { preferences.begin("my-settings", true); delayTime = preferences.getInt("delayTime", 100); pulse1 = preferences.getInt("pulse1", 40); pauseDuration = preferences.getInt("pauseDuration", 20); pulse2 = preferences.getInt("pulse2", 80); debounceProtocol = preferences.getInt("debounceProtocol", 500); preferences.end(); }
handleRotaryEncoder()
function definitions. Remove the extra one to avoid conflicts.adjustMenuItem()
function should be defined outside the handleRotaryEncoder()
function for better organization and to avoid scope issues.menuIndex
.displayMenu()
and openMenu()
functions to handle rendering and navigation on the OLED screen.Key ImprovementsCpp
#include #include #include #include // Pin Definitions #define RED_LED_PIN 26 #define OLED_CS 5 #define OLED_DC 2 #define OLED_RST 15 #define OLED_MOSI 23 #define OLED_CLK 18 #define ROTARY_CLK 32 #define ROTARY_DT 33 #define ROTARY_SW 25 #define SWITCH_BACK 12 #define SWITCH_ACTIVATE 13 #define SWITCH_RESET 14 // Initialize OLED Adafruit_GC9A01 display = Adafruit_GC9A01(OLED_CS, OLED_DC, OLED_RST); // Initialize Rotary Encoder Encoder encoder(ROTARY_CLK, ROTARY_DT); // Menu Variables int delayTime = 100; int pulse1 = 40; int pauseDuration = 20; int pulse2 = 80; int debounceProtocol = 500; int menuIndex = 0; // To track current menu item // Define min and max constants for menu variables const int MIN_DELAY_TIME = 5; const int MAX_DELAY_TIME = 3000; const int MIN_PULSE1 = 10; const int MAX_PULSE1 = 5000; const int MIN_PAUSE_DURATION = 5; const int MAX_PAUSE_DURATION = 500; const int MIN_PULSE2 = 10; const int MAX_PULSE2 = 5000; const int MIN_DEBOUNCE_PROTOCOL = 100; const int MAX_DEBOUNCE_PROTOCOL = 3000; // Function to adjust menu items with constraints void adjustMenuItem(int &value, int increment, int minValue, int maxValue) { value += increment; if (value < minValue) { value = minValue; } if (value > maxValue) { value = maxValue; } } void setup() { pinMode(RED_LED_PIN, OUTPUT); pinMode(ROTARY_SW, INPUT_PULLUP); pinMode(SWITCH_BACK, INPUT_PULLUP); pinMode(SWITCH_ACTIVATE, INPUT_PULLUP); pinMode(SWITCH_RESET, INPUT_PULLUP); // Initialize display display.begin(); display.setRotation(0); display.fillScreen(0); // Display initial information displayMenu(); // Initialize serial for debugging Serial.begin(115200); } void loop() { // Check button states if (digitalRead(SWITCH_ACTIVATE) == LOW) { delay(100); // Basic debounce if (digitalRead(SWITCH_ACTIVATE) == LOW) { executePulseSequence(); } } // Handle "Select" button logic for menu navigation static unsigned long lastPress = 0; if (digitalRead(ROTARY_SW) == LOW) { if (millis() - lastPress > 2000) { openMenu(); } lastPress = millis(); } // Update display or handle rotary input handleRotaryEncoder(); } void executePulseSequence() { // Ensure "Activate" is still held if (digitalRead(SWITCH_ACTIVATE) == HIGH) { return; // Exit if "Activate" is released } // Turn on LED to signal pulse start digitalWrite(RED_LED_PIN, HIGH); // Execute "Pulse Sequence" delay(delayTime); // Pulse 1 digitalWrite(RED_LED_PIN, HIGH); delay(pulse1); // Pause Duration digitalWrite(RED_LED_PIN, LOW); delay(pauseDuration); // Pulse 2 digitalWrite(RED_LED_PIN, HIGH); delay(pulse2); // Debounce Protocol digitalWrite(RED_LED_PIN, LOW); delay(debounceProtocol); // Turn off LED at the end of the sequence digitalWrite(RED_LED_PIN, LOW); } void displayMenu() { // Code to update and display menu on OLED } void openMenu() { // Code for menu navigation } void handleRotaryEncoder() { long newPos = encoder.read(); if (newPos != 0) { int increment = (newPos > 0) ? 1 : -1; switch (menuIndex) { case 0: // Delay Time adjustMenuItem(delayTime, increment, MIN_DELAY_TIME, MAX_DELAY_TIME); break; case 1: // Pulse 1 adjustMenuItem(pulse1, increment, MIN_PULSE1, MAX_PULSE1); break; case 2: // Pause Duration adjustMenuItem(pauseDuration, increment, MIN_PAUSE_DURATION, MAX_PAUSE_DURATION); break; case 3: // Pulse 2 adjustMenuItem(pulse2, increment, MIN_PULSE2, MAX_PULSE2); break; case 4: // Debounce Protocol adjustMenuItem(debounceProtocol, increment, MIN_DEBOUNCE_PROTOCOL, MAX_DEBOUNCE_PROTOCOL); break; } encoder.write(0); // Reset encoder position displayMenu(); // Update display with new value } }
handleRotaryEncoder()
: Ensures clarity and prevents conflicts.menuIndex
variable to track which setting is currently being adjusted.displayMenu()
and openMenu()
functions.Key PointsCpp
#include #include #include #include // Pin Definitions #define RED_LED_PIN 26 #define OLED_CS 5 #define OLED_DC 2 #define OLED_RST 15 #define OLED_MOSI 23 #define OLED_CLK 18 #define ROTARY_CLK 32 #define ROTARY_DT 33 #define ROTARY_SW 25 #define SWITCH_BACK 12 #define SWITCH_ACTIVATE 13 #define SWITCH_RESET 14 // Initialize OLED Adafruit_GC9A01 display = Adafruit_GC9A01(OLED_CS, OLED_DC, OLED_RST); // Initialize Rotary Encoder Encoder encoder(ROTARY_CLK, ROTARY_DT); // Menu Variables int delayTime = 100; int pulse1 = 40; int pauseDuration = 20; int pulse2 = 80; int debounceProtocol = 500; int menuIndex = 0; // To track the current menu item // Define min and max constants for menu variables const int MIN_DELAY_TIME = 5; const int MAX_DELAY_TIME = 3000; const int MIN_PULSE1 = 10; const int MAX_PULSE1 = 5000; const int MIN_PAUSE_DURATION = 5; const int MAX_PAUSE_DURATION = 500; const int MIN_PULSE2 = 10; const int MAX_PULSE2 = 5000; const int MIN_DEBOUNCE_PROTOCOL = 100; const int MAX_DEBOUNCE_PROTOCOL = 3000; // Function to adjust menu items with constraints void adjustMenuItem(int &value, int increment, int minValue, int maxValue) { value += increment; if (value < minValue) { value = minValue; } if (value > maxValue) { value = maxValue; } } void setup() { pinMode(RED_LED_PIN, OUTPUT); pinMode(ROTARY_SW, INPUT_PULLUP); pinMode(SWITCH_BACK, INPUT_PULLUP); pinMode(SWITCH_ACTIVATE, INPUT_PULLUP); pinMode(SWITCH_RESET, INPUT_PULLUP); // Initialize display display.begin(); display.setRotation(0); display.fillScreen(0); // Display initial menu displayMenu(); // Initialize serial for debugging Serial.begin(115200); } void loop() { // Check button states if (digitalRead(SWITCH_ACTIVATE) == LOW) { delay(100); // Basic debounce if (digitalRead(SWITCH_ACTIVATE) == LOW) { executePulseSequence(); } } // Handle "Select" button logic for menu opening static unsigned long lastPress = 0; if (digitalRead(ROTARY_SW) == LOW) { if (millis() - lastPress > 2000) { openMenu(); } lastPress = millis(); } // Update display and handle rotary input handleRotaryEncoder(); } void executePulseSequence() { // Ensure "Activate" is still held if (digitalRead(SWITCH_ACTIVATE) == HIGH) { return; // Exit if "Activate" is released } // Turn on LED to signal pulse start digitalWrite(RED_LED_PIN, HIGH); // Execute "Pulse Sequence" delay(delayTime); // Pulse 1 digitalWrite(RED_LED_PIN, HIGH); delay(pulse1); // Pause Duration digitalWrite(RED_LED_PIN, LOW); delay(pauseDuration); // Pulse 2 digitalWrite(RED_LED_PIN, HIGH); delay(pulse2); // Debounce Protocol digitalWrite(RED_LED_PIN, LOW); delay(debounceProtocol); } void displayMenu() { display.clearDisplay(); display.setCursor(0, 0); display.setTextSize(1); display.setTextColor(WHITE); switch (menuIndex) { case 0: display.println("1. Delay Time: " + String(delayTime) + " ms"); break; case 1: display.println("2. Pulse 1: " + String(pulse1) + " ms"); break; case 2: display.println("3. Pause Duration: " + String(pauseDuration) + " ms"); break; case 3: display.println("4. Pulse 2: " + String(pulse2) + " ms"); break; case 4: display.println("5. Debounce Protocol: " + String(debounceProtocol) + " ms"); break; } display.display(); } void openMenu() { // Simple menu operation simulation for changing settings displayMenu(); delay(500); // Small delay to allow reading } void handleRotaryEncoder() { long newPos = encoder.read(); if (newPos != 0) { int increment = (newPos > 0) ? 1 : -1; switch (menuIndex) { case 0: // Delay Time adjustMenuItem(delayTime, increment, MIN_DELAY_TIME, MAX_DELAY_TIME); break; case 1: // Pulse 1 adjustMenuItem(pulse1, increment, MIN_PULSE1, MAX_PULSE1); break; case 2: // Pause Duration adjustMenuItem(pauseDuration, increment, MIN_PAUSE_DURATION, MAX_PAUSE_DURATION); break; case 3: // Pulse 2 adjustMenuItem(pulse2, increment, MIN_PULSE2, MAX_PULSE2); break; case 4: // Debounce Protocol adjustMenuItem(debounceProtocol, increment, MIN_DEBOUNCE_PROTOCOL, MAX_DEBOUNCE_PROTOCOL); break; } encoder.write(0); // Reset encoder position to zero displayMenu(); // Update display with the new value } }
menuIndex
. This will allow you to scroll through menu items and adjust their values.displayMenu()
function now dynamically updates the OLED based on the selected menu index.RESET
switch physically connects to the reset pin of the ESP32 (GPIO0), there's no code handling needed, as this is a hardware-only reset mechanism.Key ChangesCpp
#include #include #include #include // Pin Definitions #define RED_LED_PIN 26 #define PULSE_OUTPUT_PIN 27 // Define a pin for MOSFET gate driver output #define OLED_CS 5 #define OLED_DC 2 #define OLED_RST 15 #define OLED_MOSI 23 #define OLED_CLK 18 #define ROTARY_CLK 32 #define ROTARY_DT 33 #define ROTARY_SW 25 #define SWITCH_BACK 12 #define SWITCH_ACTIVATE 13 #define SWITCH_RESET 0 // (This is for your reference; handling reset in hardware) // Initialize OLED Adafruit_GC9A01 display = Adafruit_GC9A01(OLED_CS, OLED_DC, OLED_RST); // Initialize Rotary Encoder Encoder encoder(ROTARY_CLK, ROTARY_DT); // Menu Variables int delayTime = 100; int pulse1 = 40; int pauseDuration = 20; int pulse2 = 80; int debounceProtocol = 500; int menuIndex = 0; // To track the current menu item // Define min and max constants for menu variables const int MIN_DELAY_TIME = 5; const int MAX_DELAY_TIME = 3000; const int MIN_PULSE1 = 10; const int MAX_PULSE1 = 5000; const int MIN_PAUSE_DURATION = 5; const int MAX_PAUSE_DURATION = 500; const int MIN_PULSE2 = 10; const int MAX_PULSE2 = 5000; const int MIN_DEBOUNCE_PROTOCOL = 100; const int MAX_DEBOUNCE_PROTOCOL = 3000; // Function to adjust menu items with constraints void adjustMenuItem(int &value, int increment, int minValue, int maxValue) { value += increment; if (value < minValue) { value = minValue; } if (value > maxValue) { value = maxValue; } } void setup() { pinMode(RED_LED_PIN, OUTPUT); pinMode(PULSE_OUTPUT_PIN, OUTPUT); pinMode(ROTARY_SW, INPUT_PULLUP); pinMode(SWITCH_BACK, INPUT_PULLUP); pinMode(SWITCH_ACTIVATE, INPUT_PULLUP); pinMode(SWITCH_RESET, INPUT_PULLUP); // No handling needed in code // Initialize display display.begin(); display.setRotation(0); display.fillScreen(0); // Display initial menu displayMenu(); // Initialize serial for debugging Serial.begin(115200); } void loop() { // Check button states if (digitalRead(SWITCH_ACTIVATE) == LOW) { delay(100); // Basic debounce if (digitalRead(SWITCH_ACTIVATE) == LOW) { executePulseSequence(); } } // Handle "Select" button logic for menu opening static unsigned long lastPress = 0; if (digitalRead(ROTARY_SW) == LOW) { if (millis() - lastPress > 2000) { openMenu(); } lastPress = millis(); } // Update display and handle rotary input handleRotaryEncoder(); } void executePulseSequence() { // Ensure "Activate" is still held if (digitalRead(SWITCH_ACTIVATE) == HIGH) { return; // Exit if "Activate" is released } // Turn on LED to signal pulse start digitalWrite(RED_LED_PIN, HIGH); digitalWrite(PULSE_OUTPUT_PIN, LOW); // Start with the pin in low state // Execute "Pulse Sequence" delay(delayTime); // Pulse 1 digitalWrite(PULSE_OUTPUT_PIN, HIGH); delay(pulse1); // Pause Duration digitalWrite(PULSE_OUTPUT_PIN, LOW); delay(pauseDuration); // Pulse 2 digitalWrite(PULSE_OUTPUT_PIN, HIGH); delay(pulse2); // Debounce Protocol digitalWrite(PULSE_OUTPUT_PIN, LOW); delay(debounceProtocol); // Turn off LED at the end of the sequence digitalWrite(RED_LED_PIN, LOW); } void displayMenu() { display.clearDisplay(); display.setCursor(0, 0); display.setTextSize(1); display.setTextColor(WHITE); switch (menuIndex) { case 0: display.println("1. Delay Time: " + String(delayTime) + " ms"); break; case 1: display.println("2. Pulse 1: " + String(pulse1) + " ms"); break; case 2: display.println("3. Pause Duration: " + String(pauseDuration) + " ms"); break; case 3: display.println("4. Pulse 2: " + String(pulse2) + " ms"); break; case 4: display.println("5. Debounce Protocol: " + String(debounceProtocol) + " ms"); break; } display.display(); } void openMenu() { // Simple menu operation simulation for changing settings displayMenu(); delay(500); // Small delay to allow reading } void handleRotaryEncoder() { long newPos = encoder.read(); if (newPos != 0) { int increment = (newPos > 0) ? 1 : -1; switch (menuIndex) { case 0: // Delay Time adjustMenuItem(delayTime, increment, MIN_DELAY_TIME, MAX_DELAY_TIME); break; case 1: // Pulse 1 adjustMenuItem(pulse1, increment, MIN_PULSE1, MAX_PULSE1); break; case 2: // Pause Duration adjustMenuItem(pauseDuration, increment, MIN_PAUSE_DURATION, MAX_PAUSE_DURATION); break; case 3: // Pulse 2 adjustMenuItem(pulse2, increment, MIN_PULSE2, MAX_PULSE2); break; case 4: // Debounce Protocol adjustMenuItem(debounceProtocol, increment, MIN_DEBOUNCE_PROTOCOL, MAX_DEBOUNCE_PROTOCOL); break; } encoder.write(0); // Reset encoder position to zero displayMenu(); // Update display with the new value } }
PULSE_OUTPUT_PIN
: Added a definition for the output pin connected to the MOSFET gate driver. It's set to low at the start of the sequence, toggles high during pulses, and returns to low after completion.SWITCH_RESET
: It's recommended to connect the reset switch directly from the EN (enable) or RST pin to ground on the hardware for a physical reset. No code handling is needed; ensure your wiring matches this setup.