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: # Torch Control
- - Source Code compiled for: ESP32 DevKit V1
- - Source Code created on: 2026-01-17 18:24:07
- ********* Pleasedontcode.com **********/
- /****** SYSTEM REQUIREMENTS *****/
- /****** SYSTEM REQUIREMENT 1 *****/
- /* Gunakan ESP32 AsyncWebServer untuk memantau */
- /* pembacaan encoder putar dan mengontrol parameter */
- /* mekanisme THC dari jarak jauh melalui koneksi */
- /* WebSocket, menyediakan visualisasi pengukur secara */
- /* real-time dan eksekusi perintah dengan umpan balik */
- /* status. */
- /****** END SYSTEM REQUIREMENTS *****/
- /* START CODE */
- /*
- * System Requirements:
- * Gunakan ESP32 AsyncWebServer untuk memantau pembacaan encoder putar dan mengontrol parameter
- * mekanisme THC dari jarak jauh melalui koneksi WebSocket, menyediakan visualisasi pengukur
- * secara real-time dan eksekusi perintah dengan umpan balik status.
- */
- #include <LiquidCrystal_I2C.h>
- #include "_global.h"
- // ============ GLOBAL VARIABLE DEFINITIONS ============
- // EEPROM address variables for each parameter
- int SetVa = 0; // Set Voltage address
- int DTa = 1; // Delay Time address
- int HySa = 2; // Hysteresis address
- int StVa = 3; // Start Voltage address
- // THC control parameters
- int SetV = 100; // Set voltage (0-250V)
- int DT = 5; // Delay time (0.5s = value 5)
- int HyS = 80; // Hysteresis (8.0V = value 80)
- int StV = 100; // Start voltage (100V)
- int ArcV = 0; // Arc voltage (read from sensor)
- // Encoder variables
- int encoderVal = 100; // Current encoder position value
- int oldValue = 0; // Previous encoder value for change detection
- // Program selection
- int program = 1; // Currently selected program (1, 2, or 3)
- const int ParamItem = 4; // Number of parameters per program (SetV, DT, HyS, StV)
- int Param[4] = {100, 5, 80, 100}; // Array to store program parameters
- // LCD menu control
- int menu = 0; // Current menu state (0=default, 1=main menu, etc)
- int pos = 0; // Position in menu
- int show = 0; // LCD update counter
- int LCDtime = 0; // LCD timeout counter
- int defaultLCDtime = 30; // LCD timeout in 50ms intervals (1.5s = 30*50ms)
- // Timer and control variables
- esp_timer_handle_t timer_handle = nullptr; // Timer handle
- bool Do = false; // Timer trigger flag
- // Pin definitions for torch control
- const int outputUpPin = 27; // GPIO 27 - Torch UP control
- const int outputDnPin = 26; // GPIO 26 - Torch DOWN control
- const int outputOkPin = 25; // GPIO 25 - Torch OK control
- // WebSocket update timing
- unsigned long lastWebSocketUpdate = 0;
- const unsigned long WEBSOCKET_UPDATE_INTERVAL = 100; // 100ms update interval
- // THC control variables
- unsigned int delayTime = 0;
- unsigned int SetVx10 = 1000;
- // LCD initialization with I2C address 0x27 and 16x2 display
- LiquidCrystal_I2C lcd(0x27, 16, 2);
- // Custom character definitions for display symbols
- byte armsUpDn[8] = {
- B00000,
- B00100,
- B01110,
- B00100,
- B00000,
- B01110,
- B00000,
- B00000
- };
- byte customUp[8] = {
- B00100,
- B01110,
- B10101,
- B00100,
- B00100,
- B00100,
- B00100,
- B00000
- };
- byte customDown[8] = {
- B00100,
- B00100,
- B00100,
- B00100,
- B10101,
- B01110,
- B00100,
- B00000
- };
- // ============ SETUP FUNCTION - MAIN INITIALIZATION ============
- void setup() {
- // Initialize serial communication
- Serial.begin(115200);
- delay(100);
- // Print startup banner
- Serial.println("\n\n========================================");
- Serial.println(" ESP32 THC System - Starting");
- Serial.println(" Board: ESP32 DevKit V1");
- Serial.println(" Version: 2.0");
- Serial.println("========================================\n");
- // Initialize EEPROM
- EEPROM.begin(512);
- delay(100);
- Serial.println("EEPROM initialized.");
- // Load program configuration from EEPROM
- ReadProg();
- Serial.println("Program configuration loaded from EEPROM.");
- // Setup pin modes for THC control
- Setup_THC();
- Serial.println("THC GPIO pins configured.");
- // Initialize and setup the LCD display
- Setup_LCD();
- Serial.println("LCD display initialized.");
- // Setup timer for periodic operations (10ms interval)
- Setup_Timer2();
- Serial.println("Timer setup complete - 10ms interval.");
- // Initialize WebServer and WiFi AP
- startWiFiAP();
- Serial.println("WiFi Access Point started.");
- setupWebServer();
- Serial.println("AsyncWebServer initialized.");
- // Set initial encoder value to current SetV
- encoderVal = SetV;
- // Print configuration summary
- Serial.println("\n========== Configuration Summary ==========");
- Serial.print("Program: ");
- Serial.println(program);
- Serial.print("Set Voltage: ");
- Serial.print(SetV);
- Serial.println(" V");
- Serial.print("Delay Time: ");
- Serial.print(DT / 10.0);
- Serial.println(" s");
- Serial.print("Hysteresis: ");
- Serial.print(HyS / 10.0);
- Serial.println(" V");
- Serial.print("Start Voltage: ");
- Serial.print(StV);
- Serial.println(" V");
- Serial.println("==========================================\n");
- Serial.println("Setup complete. System ready for operation.");
- }
- // ============ LOOP FUNCTION - MAIN CONTROL LOOP ============
- void loop() {
- // Execute THC control algorithm (on timer interrupt)
- doTHC();
- // Update LCD display
- doLCD();
- // Send real-time updates via WebSocket at specified interval
- unsigned long currentMillis = millis();
- if (currentMillis - lastWebSocketUpdate >= WEBSOCKET_UPDATE_INTERVAL) {
- lastWebSocketUpdate = currentMillis;
- // Send encoder position update
- sendEncoderUpdate();
- // Send THC status update
- sendTHCStatus();
- // Send gauge update with arc voltage
- sendGaugeUpdate();
- }
- // Small delay to prevent watchdog timeout on ESP32
- delay(5);
- }
- // ============ EEPROM FUNCTIONS ============
- // Read program selection and load associated parameters from EEPROM
- void ReadProg() {
- // Read which program was last selected (stored at address 100)
- program = EEPROM.read(100);
- // Validate program selection (must be 1, 2, or 3)
- if (program < 1 || program > 3) {
- program = 1;
- }
- // Set EEPROM addresses based on selected program
- // Each program uses 4 consecutive addresses
- if (program == 1) {
- SetVa = 0; // Program 1: addresses 0-3
- DTa = 1;
- HySa = 2;
- StVa = 3;
- ReadDataProg_1();
- } else if (program == 2) {
- SetVa = 4; // Program 2: addresses 4-7
- DTa = 5;
- HySa = 6;
- StVa = 7;
- ReadDataProg_2();
- } else {
- SetVa = 8; // Program 3: addresses 8-11
- DTa = 9;
- HySa = 10;
- StVa = 11;
- ReadDataProg_3();
- }
- }
- // Read Program 1 parameters from EEPROM (addresses 0-3)
- void ReadDataProg_1() {
- SetV = EEPROM.read(0);
- DT = EEPROM.read(1);
- HyS = EEPROM.read(2);
- StV = EEPROM.read(3);
- // Validate ranges
- if (SetV < 0 || SetV > 250) SetV = 100;
- if (DT < 1 || DT > 200) DT = 5;
- if (HyS < 1 || HyS > 99) HyS = 80;
- if (StV < 50 || StV > 250) StV = 100;
- // Store in parameter array
- Param[0] = SetV;
- Param[1] = DT;
- Param[2] = HyS;
- Param[3] = StV;
- }
- // Read Program 2 parameters from EEPROM (addresses 4-7)
- void ReadDataProg_2() {
- SetV = EEPROM.read(4);
- DT = EEPROM.read(5);
- HyS = EEPROM.read(6);
- StV = EEPROM.read(7);
- // Validate ranges
- if (SetV < 0 || SetV > 250) SetV = 100;
- if (DT < 1 || DT > 200) DT = 5;
- if (HyS < 1 || HyS > 99) HyS = 80;
- if (StV < 50 || StV > 250) StV = 100;
- // Store in parameter array
- Param[0] = SetV;
- Param[1] = DT;
- Param[2] = HyS;
- Param[3] = StV;
- }
- // Read Program 3 parameters from EEPROM (addresses 8-11)
- void ReadDataProg_3() {
- SetV = EEPROM.read(8);
- DT = EEPROM.read(9);
- HyS = EEPROM.read(10);
- StV = EEPROM.read(11);
- // Validate ranges
- if (SetV < 0 || SetV > 250) SetV = 100;
- if (DT < 1 || DT > 200) DT = 5;
- if (HyS < 1 || HyS > 99) HyS = 80;
- if (StV < 50 || StV > 250) StV = 100;
- // Store in parameter array
- Param[0] = SetV;
- Param[1] = DT;
- Param[2] = HyS;
- Param[3] = StV;
- }
- // Save a single parameter value to EEPROM
- void SaveData(int add, int value) {
- EEPROM.write(add, value);
- EEPROM.commit(); // Persist to flash
- }
- // Load factory default values into EEPROM
- void Default() {
- // Program 1 defaults
- EEPROM.write(0, 100); // SetV = 100V
- EEPROM.write(1, 5); // DT = 0.5s
- EEPROM.write(2, 80); // HyS = 8.0V
- EEPROM.write(3, 100); // StV = 100V
- // Program 2 defaults
- EEPROM.write(4, 100);
- EEPROM.write(5, 5);
- EEPROM.write(6, 80);
- EEPROM.write(7, 100);
- // Program 3 defaults
- EEPROM.write(8, 100);
- EEPROM.write(9, 5);
- EEPROM.write(10, 80);
- EEPROM.write(11, 100);
- // Set current program to 1
- EEPROM.write(100, 1);
- EEPROM.commit(); // Persist all changes
- }
- // Set and load a specific program
- void doProgramSet(int prg) {
- // Validate program number
- if (prg < 1 || prg > 3) {
- prg = 1;
- }
- // Update address pointers based on program selection
- if (prg == 1) {
- SetVa = 0;
- DTa = 1;
- HySa = 2;
- StVa = 3;
- } else if (prg == 2) {
- SetVa = 4;
- DTa = 5;
- HySa = 6;
- StVa = 7;
- } else {
- SetVa = 8;
- DTa = 9;
- HySa = 10;
- StVa = 11;
- }
- // Save selected program to EEPROM (persistent across power cycles)
- SaveData(100, prg);
- program = prg;
- }
- // ============ TIMER SETUP FUNCTION ============
- // Configure ESP32 high-resolution timer for 10ms periodic interrupt
- void Setup_Timer2() {
- // Create timer configuration structure
- esp_timer_create_args_t timer_args = {
- .callback = &onTimerCallback,
- .name = "my_periodic_timer"
- };
- // Create the timer with specified configuration
- ESP_ERROR_CHECK(esp_timer_create(&timer_args, &timer_handle));
- // Start the timer with 10,000 microsecond period (10ms)
- // Timer will call onTimerCallback every 10ms
- ESP_ERROR_CHECK(esp_timer_start_periodic(timer_handle, 10000));
- }
- // Timer callback function - executes every 10ms
- void onTimerCallback(void* arg) {
- // Set flag to indicate timer fired
- // This flag is checked in doTHC() function during main loop
- Do = true;
- }
- // ============ LCD DISPLAY FUNCTIONS ============
- // Initialize LCD display and setup custom characters
- void Setup_LCD() {
- // Initialize LCD communication
- lcd.init();
- lcd.backlight();
- // Create custom characters for UP/DOWN arrows
- lcd.createChar(0, armsUpDn);
- lcd.createChar(1, customUp);
- lcd.createChar(2, customDown);
- // Display startup message
- lcd.setCursor(1, 0);
- lcd.print("MEHMET IBRAHIM");
- lcd.setCursor(3, 1);
- lcd.print("Plasma THC");
- delay(1500);
- lcd.clear();
- }
- // Update LCD display - main display controller
- void doLCD() {
- if (show >= 2) {
- show = 0;
- switch (menu) {
- case 0:
- doLCDDefault();
- break;
- case 1:
- doLCDMenu();
- break;
- case 11:
- doLCDMenuSetup();
- break;
- case 111:
- doLCDDelayTime();
- break;
- case 112:
- doLCDHysreresis();
- break;
- case 113:
- doLCDStartVoltage();
- break;
- case 114:
- doLCDLoadDefault();
- break;
- case 12:
- doLCDTest();
- break;
- case 115:
- doTestUp();
- break;
- case 116:
- doTestDown();
- break;
- case 13:
- doLCDProgramSellect();
- break;
- case 121:
- doProgramSet(1);
- break;
- case 122:
- doProgramSet(2);
- break;
- case 123:
- doProgramSet(3);
- break;
- default:
- doLCDDefault();
- }
- }
- }
- // Display default/home screen with voltage and status
- void doLCDDefault() {
- if (encoderVal < 0) encoderVal = 0;
- else if (encoderVal > 250) encoderVal = 250;
- SetV = encoderVal;
- if (SetV != oldValue) {
- SaveData(SetVa, SetV);
- oldValue = SetV;
- }
- lcd.setCursor(0, 0);
- lcd.print("P ");
- if (digitalRead(outputUpPin) == HIGH) {
- lcd.write(1);
- } else {
- lcd.print(" ");
- }
- lcd.print(" ");
- lcd.setCursor(4, 0);
- lcd.print("Set.V: ");
- lcd.print(SetV);
- lcd.print(" ");
- lcd.setCursor(0, 1);
- lcd.print(program);
- lcd.print(" ");
- if (digitalRead(outputDnPin) == HIGH) {
- lcd.write(2);
- } else {
- lcd.print(" ");
- }
- lcd.print(" ");
- lcd.setCursor(4, 1);
- lcd.print("Arc.V: ");
- lcd.print(ArcV / 10);
- lcd.print(" ");
- }
- // Main menu navigation
- void doLCDMenu() {
- if (encoderVal < 0) encoderVal = 3;
- pos = encoderVal % 4;
- switch (pos) {
- case 0:
- lcd.setCursor(0, 0);
- lcd.print("> Exit ");
- lcd.setCursor(0, 1);
- lcd.print(" Program ");
- break;
- case 1:
- lcd.setCursor(0, 0);
- lcd.print("> Program ");
- lcd.setCursor(0, 1);
- lcd.print(" Setup ");
- break;
- case 2:
- lcd.setCursor(0, 0);
- lcd.print("> Setup ");
- lcd.setCursor(0, 1);
- lcd.print(" Test ");
- break;
- case 3:
- lcd.setCursor(0, 0);
- lcd.print("> Test ");
- lcd.setCursor(0, 1);
- lcd.print(" Exit ");
- break;
- }
- }
- // Program selection menu
- void doLCDProgramSellect() {
- if (encoderVal < 0) encoderVal = 3;
- pos = abs(encoderVal % 4);
- switch (pos) {
- case 0:
- lcd.setCursor(0, 0);
- lcd.print(">> Exit ");
- lcd.setCursor(0, 1);
- lcd.print(" Load Prog: 1 ");
- break;
- case 1:
- lcd.setCursor(0, 0);
- lcd.print(">> Load Prog: 1 ");
- lcd.setCursor(0, 1);
- lcd.print(" Load Prog: 2 ");
- break;
- case 2:
- lcd.setCursor(0, 0);
- lcd.print(">> Load Prog: 2 ");
- lcd.setCursor(0, 1);
- lcd.print(" Load Prog: 3 ");
- break;
- case 3:
- lcd.setCursor(0, 0);
- lcd.print(">> Load Prog: 3 ");
- lcd.setCursor(0, 1);
- lcd.print(" Exit ");
- break;
- }
- }
- // Setup menu navigation
- void doLCDMenuSetup() {
- if (encoderVal < 0) encoderVal = 4;
- pos = abs(encoderVal % 5);
- switch (pos) {
- case 0:
- lcd.setCursor(0, 0);
- lcd.print(">> Exit ");
- lcd.setCursor(0, 1);
- lcd.print(" Delay Time ");
- break;
- case 1:
- lcd.setCursor(0, 0);
- lcd.print(">> Delay Time ");
- lcd.setCursor(0, 1);
- lcd.print(" Hysteresis ");
- break;
- case 2:
- lcd.setCursor(0, 0);
- lcd.print(">> Hysteresis ");
- lcd.setCursor(0, 1);
- lcd.print(" Start Voltage");
- break;
- case 3:
- lcd.setCursor(0, 0);
- lcd.print(">> Start Voltage");
- lcd.setCursor(0, 1);
- lcd.print(" Load Default ");
- break;
- case 4:
- lcd.setCursor(0, 0);
- lcd.print(">> Load Default ");
- lcd.setCursor(0, 1);
- lcd.print(" Exit ");
- break;
- }
- }
- // Test menu navigation
- void doLCDTest() {
- if (encoderVal < 0) encoderVal = 2;
- pos = abs(encoderVal % 3);
- switch (pos) {
- case 0:
- lcd.setCursor(0, 0);
- lcd.print("Test > Exit ");
- lcd.setCursor(0, 1);
- lcd.print(" Torch Up ");
- digitalWrite(outputDnPin, LOW);
- digitalWrite(outputUpPin, LOW);
- digitalWrite(outputOkPin, LOW);
- break;
- case 1:
- lcd.setCursor(0, 0);
- lcd.print("Test > Torch Up ");
- lcd.setCursor(0, 1);
- lcd.print(" Torch Dn ");
- if (digitalRead(outputOkPin) == LOW) LCDtime = 0;
- if (LCDtime >= 200) {
- digitalWrite(outputDnPin, LOW);
- digitalWrite(outputUpPin, LOW);
- digitalWrite(outputOkPin, LOW);
- }
- break;
- case 2:
- lcd.setCursor(0, 0);
- lcd.print("Test > Torch Dn ");
- lcd.setCursor(0, 1);
- lcd.print(" Exit ");
- if (digitalRead(outputOkPin) == LOW) LCDtime = 0;
- if (LCDtime >= 200) {
- digitalWrite(outputDnPin, LOW);
- digitalWrite(outputUpPin, LOW);
- digitalWrite(outputOkPin, LOW);
- }
- break;
- }
- }
- // Test torch UP - activate UP output
- void doTestUp() {
- digitalWrite(outputDnPin, LOW);
- digitalWrite(outputUpPin, HIGH);
- digitalWrite(outputOkPin, HIGH);
- LCDtime = 0;
- menu = 12;
- encoderVal = 1;
- }
- // Test torch DOWN - activate DOWN output
- void doTestDown() {
- digitalWrite(outputDnPin, HIGH);
- digitalWrite(outputUpPin, LOW);
- digitalWrite(outputOkPin, HIGH);
- LCDtime = 0;
- menu = 12;
- encoderVal = 2;
- }
- // Delay time adjustment display and control
- void doLCDDelayTime() {
- if (encoderVal < 1) encoderVal = 1;
- else if (encoderVal > 200) encoderVal = 200;
- DT = encoderVal;
- if (DT != oldValue) {
- SaveData(DTa, DT);
- oldValue = DT;
- LCDtime = 0;
- }
- double x = DT / 10.00;
- lcd.setCursor(0, 0);
- lcd.print("Set > Delay Time");
- lcd.setCursor(0, 1);
- lcd.print(" : ");
- lcd.print(x, 1);
- lcd.print(" s ");
- }
- // Hysteresis adjustment display and control
- void doLCDHysreresis() {
- if (encoderVal < 1) encoderVal = 1;
- else if (encoderVal > 99) encoderVal = 99;
- HyS = encoderVal;
- if (HyS != oldValue) {
- SaveData(HySa, HyS);
- oldValue = HyS;
- LCDtime = 0;
- }
- double x = HyS / 10.00;
- lcd.setCursor(0, 0);
- lcd.print("Set > Hysteresis");
- lcd.setCursor(0, 1);
- lcd.print(" :");
- lcd.write(0);
- lcd.print(x, 1);
- lcd.print(" V ");
- }
- // Start voltage adjustment display and control
- void doLCDStartVoltage() {
- if (encoderVal < 50) encoderVal = 50;
- else if (encoderVal > 250) encoderVal = 250;
- StV = encoderVal;
- if (StV != oldValue) {
- SaveData(StVa, StV);
- oldValue = StV;
- LCDtime = 0;
- }
- lcd.setCursor(0, 0);
- lcd.print("Set > Start Volt");
- lcd.setCursor(0, 1);
- lcd.print(" : ");
- lcd.print(StV);
- lcd.print(" V ");
- }
- // Load default values - initialize EEPROM with factory defaults
- void doLCDLoadDefault() {
- Default();
- for (byte i = 0; i < 100; i++) {
- lcd.setCursor(0, 0);
- lcd.print(" Default ");
- lcd.setCursor(0, 1);
- lcd.print("Load ");
- lcd.print(i);
- lcd.print(" ");
- lcd.print("%");
- lcd.print(" ");
- delay(5);
- }
- lcd.setCursor(0, 0);
- lcd.print("Default: DONE ");
- lcd.setCursor(0, 1);
- lcd.print("Please Restart ");
- exit(0);
- }
- // ============ THC CONTROL FUNCTIONS ============
- // Initialize THC control - setup GPIO pins
- void Setup_THC() {
- pinMode(outputUpPin, OUTPUT); // GPIO 27
- pinMode(outputOkPin, OUTPUT); // GPIO 25
- pinMode(outputDnPin, OUTPUT); // GPIO 26
- // Initialize all outputs to LOW (off)
- digitalWrite(outputUpPin, LOW);
- digitalWrite(outputOkPin, LOW);
- digitalWrite(outputDnPin, LOW);
- }
- // Main THC control algorithm - called from timer callback
- void doTHC() {
- if (Do) {
- Do = false;
- LCDtime++;
- show++;
- // Auto-return to default menu after timeout (1.5s = 30 * 50ms)
- if (LCDtime > defaultLCDtime) {
- menu = 0;
- pos = 0;
- LCDtime = 0;
- encoderVal = SetV;
- }
- // THC control logic - only active when arc voltage is reasonable
- if ((500 < ArcV) && (ArcV < 2500)) {
- // Increment delay counter until delay time is met
- if (ArcV > StV * 10) delayTime++;
- // Once delay time reached, begin torch height control
- if (delayTime >= DT * 10) {
- SetVx10 = SetV * 10;
- delayTime = DT * 10;
- // Signal arc detection confirmed
- digitalWrite(outputOkPin, HIGH);
- // Torch height control with hysteresis
- if (ArcV >= SetVx10 + HyS) {
- // Arc voltage too high - move torch DOWN
- digitalWrite(outputUpPin, LOW);
- digitalWrite(outputDnPin, HIGH);
- } else if (ArcV <= SetVx10 - HyS) {
- // Arc voltage too low - move torch UP
- digitalWrite(outputDnPin, LOW);
- digitalWrite(outputUpPin, HIGH);
- } else {
- // Arc voltage within acceptable range - hold position
- digitalWrite(outputUpPin, LOW);
- digitalWrite(outputDnPin, LOW);
- }
- }
- } else if (menu != 12) {
- // No valid arc or not in test mode - deactivate all outputs
- delayTime = 0;
- digitalWrite(outputUpPin, LOW);
- digitalWrite(outputOkPin, LOW);
- digitalWrite(outputDnPin, LOW);
- }
- }
- }
- /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment