pleasedontcode

# Torch Control rev_10

Jan 17th, 2026
22
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 20.12 KB | None | 0 0
  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: # Torch Control
  13.     - Source Code compiled for: ESP32 DevKit V1
  14.     - Source Code created on: 2026-01-17 18:24:07
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* Gunakan ESP32 AsyncWebServer untuk memantau */
  21.     /* pembacaan encoder putar dan mengontrol parameter */
  22.     /* mekanisme THC dari jarak jauh melalui koneksi */
  23.     /* WebSocket, menyediakan visualisasi pengukur secara */
  24.     /* real-time dan eksekusi perintah dengan umpan balik */
  25.     /* status. */
  26. /****** END SYSTEM REQUIREMENTS *****/
  27.  
  28.  
  29.  
  30.  
  31.  
  32. /* START CODE */
  33.  
  34. /*
  35.  * System Requirements:
  36.  * Gunakan ESP32 AsyncWebServer untuk memantau pembacaan encoder putar dan mengontrol parameter
  37.  * mekanisme THC dari jarak jauh melalui koneksi WebSocket, menyediakan visualisasi pengukur
  38.  * secara real-time dan eksekusi perintah dengan umpan balik status.
  39.  */
  40.  
  41. #include <LiquidCrystal_I2C.h>
  42. #include "_global.h"
  43.  
  44. // ============ GLOBAL VARIABLE DEFINITIONS ============
  45.  
  46. // EEPROM address variables for each parameter
  47. int SetVa = 0;  // Set Voltage address
  48. int DTa = 1;    // Delay Time address
  49. int HySa = 2;   // Hysteresis address
  50. int StVa = 3;   // Start Voltage address
  51.  
  52. // THC control parameters
  53. int SetV = 100;      // Set voltage (0-250V)
  54. int DT = 5;          // Delay time (0.5s = value 5)
  55. int HyS = 80;        // Hysteresis (8.0V = value 80)
  56. int StV = 100;       // Start voltage (100V)
  57. int ArcV = 0;        // Arc voltage (read from sensor)
  58.  
  59. // Encoder variables
  60. int encoderVal = 100;  // Current encoder position value
  61. int oldValue = 0;      // Previous encoder value for change detection
  62.  
  63. // Program selection
  64. int program = 1;                    // Currently selected program (1, 2, or 3)
  65. const int ParamItem = 4;            // Number of parameters per program (SetV, DT, HyS, StV)
  66. int Param[4] = {100, 5, 80, 100};   // Array to store program parameters
  67.  
  68. // LCD menu control
  69. int menu = 0;                   // Current menu state (0=default, 1=main menu, etc)
  70. int pos = 0;                    // Position in menu
  71. int show = 0;                   // LCD update counter
  72. int LCDtime = 0;                // LCD timeout counter
  73. int defaultLCDtime = 30;        // LCD timeout in 50ms intervals (1.5s = 30*50ms)
  74.  
  75. // Timer and control variables
  76. esp_timer_handle_t timer_handle = nullptr;  // Timer handle
  77. bool Do = false;                // Timer trigger flag
  78.  
  79. // Pin definitions for torch control
  80. const int outputUpPin = 27;   // GPIO 27 - Torch UP control
  81. const int outputDnPin = 26;   // GPIO 26 - Torch DOWN control
  82. const int outputOkPin = 25;   // GPIO 25 - Torch OK control
  83.  
  84. // WebSocket update timing
  85. unsigned long lastWebSocketUpdate = 0;
  86. const unsigned long WEBSOCKET_UPDATE_INTERVAL = 100;  // 100ms update interval
  87.  
  88. // THC control variables
  89. unsigned int delayTime = 0;
  90. unsigned int SetVx10 = 1000;
  91.  
  92. // LCD initialization with I2C address 0x27 and 16x2 display
  93. LiquidCrystal_I2C lcd(0x27, 16, 2);
  94.  
  95. // Custom character definitions for display symbols
  96. byte armsUpDn[8] = {
  97.   B00000,
  98.   B00100,
  99.   B01110,
  100.   B00100,
  101.   B00000,
  102.   B01110,
  103.   B00000,
  104.   B00000
  105. };
  106.  
  107. byte customUp[8] = {
  108.   B00100,
  109.   B01110,
  110.   B10101,
  111.   B00100,
  112.   B00100,
  113.   B00100,
  114.   B00100,
  115.   B00000
  116. };
  117.  
  118. byte customDown[8] = {
  119.   B00100,
  120.   B00100,
  121.   B00100,
  122.   B00100,
  123.   B10101,
  124.   B01110,
  125.   B00100,
  126.   B00000
  127. };
  128.  
  129. // ============ SETUP FUNCTION - MAIN INITIALIZATION ============
  130. void setup() {
  131.   // Initialize serial communication
  132.   Serial.begin(115200);
  133.   delay(100);
  134.  
  135.   // Print startup banner
  136.   Serial.println("\n\n========================================");
  137.   Serial.println("  ESP32 THC System - Starting");
  138.   Serial.println("  Board: ESP32 DevKit V1");
  139.   Serial.println("  Version: 2.0");
  140.   Serial.println("========================================\n");
  141.  
  142.   // Initialize EEPROM
  143.   EEPROM.begin(512);
  144.   delay(100);
  145.   Serial.println("EEPROM initialized.");
  146.  
  147.   // Load program configuration from EEPROM
  148.   ReadProg();
  149.   Serial.println("Program configuration loaded from EEPROM.");
  150.  
  151.   // Setup pin modes for THC control
  152.   Setup_THC();
  153.   Serial.println("THC GPIO pins configured.");
  154.  
  155.   // Initialize and setup the LCD display
  156.   Setup_LCD();
  157.   Serial.println("LCD display initialized.");
  158.  
  159.   // Setup timer for periodic operations (10ms interval)
  160.   Setup_Timer2();
  161.   Serial.println("Timer setup complete - 10ms interval.");
  162.  
  163.   // Initialize WebServer and WiFi AP
  164.   startWiFiAP();
  165.   Serial.println("WiFi Access Point started.");
  166.  
  167.   setupWebServer();
  168.   Serial.println("AsyncWebServer initialized.");
  169.  
  170.   // Set initial encoder value to current SetV
  171.   encoderVal = SetV;
  172.  
  173.   // Print configuration summary
  174.   Serial.println("\n========== Configuration Summary ==========");
  175.   Serial.print("Program: ");
  176.   Serial.println(program);
  177.   Serial.print("Set Voltage: ");
  178.   Serial.print(SetV);
  179.   Serial.println(" V");
  180.   Serial.print("Delay Time: ");
  181.   Serial.print(DT / 10.0);
  182.   Serial.println(" s");
  183.   Serial.print("Hysteresis: ");
  184.   Serial.print(HyS / 10.0);
  185.   Serial.println(" V");
  186.   Serial.print("Start Voltage: ");
  187.   Serial.print(StV);
  188.   Serial.println(" V");
  189.   Serial.println("==========================================\n");
  190.  
  191.   Serial.println("Setup complete. System ready for operation.");
  192. }
  193.  
  194. // ============ LOOP FUNCTION - MAIN CONTROL LOOP ============
  195. void loop() {
  196.   // Execute THC control algorithm (on timer interrupt)
  197.   doTHC();
  198.  
  199.   // Update LCD display
  200.   doLCD();
  201.  
  202.   // Send real-time updates via WebSocket at specified interval
  203.   unsigned long currentMillis = millis();
  204.   if (currentMillis - lastWebSocketUpdate >= WEBSOCKET_UPDATE_INTERVAL) {
  205.     lastWebSocketUpdate = currentMillis;
  206.    
  207.     // Send encoder position update
  208.     sendEncoderUpdate();
  209.    
  210.     // Send THC status update
  211.     sendTHCStatus();
  212.    
  213.     // Send gauge update with arc voltage
  214.     sendGaugeUpdate();
  215.   }
  216.  
  217.   // Small delay to prevent watchdog timeout on ESP32
  218.   delay(5);
  219. }
  220.  
  221. // ============ EEPROM FUNCTIONS ============
  222.  
  223. // Read program selection and load associated parameters from EEPROM
  224. void ReadProg() {
  225.   // Read which program was last selected (stored at address 100)
  226.   program = EEPROM.read(100);
  227.  
  228.   // Validate program selection (must be 1, 2, or 3)
  229.   if (program < 1 || program > 3) {
  230.     program = 1;
  231.   }
  232.  
  233.   // Set EEPROM addresses based on selected program
  234.   // Each program uses 4 consecutive addresses
  235.   if (program == 1) {
  236.     SetVa = 0;   // Program 1: addresses 0-3
  237.     DTa = 1;
  238.     HySa = 2;
  239.     StVa = 3;
  240.     ReadDataProg_1();
  241.   } else if (program == 2) {
  242.     SetVa = 4;   // Program 2: addresses 4-7
  243.     DTa = 5;
  244.     HySa = 6;
  245.     StVa = 7;
  246.     ReadDataProg_2();
  247.   } else {
  248.     SetVa = 8;   // Program 3: addresses 8-11
  249.     DTa = 9;
  250.     HySa = 10;
  251.     StVa = 11;
  252.     ReadDataProg_3();
  253.   }
  254. }
  255.  
  256. // Read Program 1 parameters from EEPROM (addresses 0-3)
  257. void ReadDataProg_1() {
  258.   SetV = EEPROM.read(0);
  259.   DT = EEPROM.read(1);
  260.   HyS = EEPROM.read(2);
  261.   StV = EEPROM.read(3);
  262.  
  263.   // Validate ranges
  264.   if (SetV < 0 || SetV > 250) SetV = 100;
  265.   if (DT < 1 || DT > 200) DT = 5;
  266.   if (HyS < 1 || HyS > 99) HyS = 80;
  267.   if (StV < 50 || StV > 250) StV = 100;
  268.  
  269.   // Store in parameter array
  270.   Param[0] = SetV;
  271.   Param[1] = DT;
  272.   Param[2] = HyS;
  273.   Param[3] = StV;
  274. }
  275.  
  276. // Read Program 2 parameters from EEPROM (addresses 4-7)
  277. void ReadDataProg_2() {
  278.   SetV = EEPROM.read(4);
  279.   DT = EEPROM.read(5);
  280.   HyS = EEPROM.read(6);
  281.   StV = EEPROM.read(7);
  282.  
  283.   // Validate ranges
  284.   if (SetV < 0 || SetV > 250) SetV = 100;
  285.   if (DT < 1 || DT > 200) DT = 5;
  286.   if (HyS < 1 || HyS > 99) HyS = 80;
  287.   if (StV < 50 || StV > 250) StV = 100;
  288.  
  289.   // Store in parameter array
  290.   Param[0] = SetV;
  291.   Param[1] = DT;
  292.   Param[2] = HyS;
  293.   Param[3] = StV;
  294. }
  295.  
  296. // Read Program 3 parameters from EEPROM (addresses 8-11)
  297. void ReadDataProg_3() {
  298.   SetV = EEPROM.read(8);
  299.   DT = EEPROM.read(9);
  300.   HyS = EEPROM.read(10);
  301.   StV = EEPROM.read(11);
  302.  
  303.   // Validate ranges
  304.   if (SetV < 0 || SetV > 250) SetV = 100;
  305.   if (DT < 1 || DT > 200) DT = 5;
  306.   if (HyS < 1 || HyS > 99) HyS = 80;
  307.   if (StV < 50 || StV > 250) StV = 100;
  308.  
  309.   // Store in parameter array
  310.   Param[0] = SetV;
  311.   Param[1] = DT;
  312.   Param[2] = HyS;
  313.   Param[3] = StV;
  314. }
  315.  
  316. // Save a single parameter value to EEPROM
  317. void SaveData(int add, int value) {
  318.   EEPROM.write(add, value);
  319.   EEPROM.commit();  // Persist to flash
  320. }
  321.  
  322. // Load factory default values into EEPROM
  323. void Default() {
  324.   // Program 1 defaults
  325.   EEPROM.write(0, 100);   // SetV = 100V
  326.   EEPROM.write(1, 5);     // DT = 0.5s
  327.   EEPROM.write(2, 80);    // HyS = 8.0V
  328.   EEPROM.write(3, 100);   // StV = 100V
  329.  
  330.   // Program 2 defaults
  331.   EEPROM.write(4, 100);
  332.   EEPROM.write(5, 5);
  333.   EEPROM.write(6, 80);
  334.   EEPROM.write(7, 100);
  335.  
  336.   // Program 3 defaults
  337.   EEPROM.write(8, 100);
  338.   EEPROM.write(9, 5);
  339.   EEPROM.write(10, 80);
  340.   EEPROM.write(11, 100);
  341.  
  342.   // Set current program to 1
  343.   EEPROM.write(100, 1);
  344.  
  345.   EEPROM.commit();  // Persist all changes
  346. }
  347.  
  348. // Set and load a specific program
  349. void doProgramSet(int prg) {
  350.   // Validate program number
  351.   if (prg < 1 || prg > 3) {
  352.     prg = 1;
  353.   }
  354.  
  355.   // Update address pointers based on program selection
  356.   if (prg == 1) {
  357.     SetVa = 0;
  358.     DTa = 1;
  359.     HySa = 2;
  360.     StVa = 3;
  361.   } else if (prg == 2) {
  362.     SetVa = 4;
  363.     DTa = 5;
  364.     HySa = 6;
  365.     StVa = 7;
  366.   } else {
  367.     SetVa = 8;
  368.     DTa = 9;
  369.     HySa = 10;
  370.     StVa = 11;
  371.   }
  372.  
  373.   // Save selected program to EEPROM (persistent across power cycles)
  374.   SaveData(100, prg);
  375.   program = prg;
  376. }
  377.  
  378. // ============ TIMER SETUP FUNCTION ============
  379.  
  380. // Configure ESP32 high-resolution timer for 10ms periodic interrupt
  381. void Setup_Timer2() {
  382.   // Create timer configuration structure
  383.   esp_timer_create_args_t timer_args = {
  384.     .callback = &onTimerCallback,
  385.     .name = "my_periodic_timer"
  386.   };
  387.  
  388.   // Create the timer with specified configuration
  389.   ESP_ERROR_CHECK(esp_timer_create(&timer_args, &timer_handle));
  390.  
  391.   // Start the timer with 10,000 microsecond period (10ms)
  392.   // Timer will call onTimerCallback every 10ms
  393.   ESP_ERROR_CHECK(esp_timer_start_periodic(timer_handle, 10000));
  394. }
  395.  
  396. // Timer callback function - executes every 10ms
  397. void onTimerCallback(void* arg) {
  398.   // Set flag to indicate timer fired
  399.   // This flag is checked in doTHC() function during main loop
  400.   Do = true;
  401. }
  402.  
  403. // ============ LCD DISPLAY FUNCTIONS ============
  404.  
  405. // Initialize LCD display and setup custom characters
  406. void Setup_LCD() {
  407.   // Initialize LCD communication
  408.   lcd.init();
  409.   lcd.backlight();
  410.  
  411.   // Create custom characters for UP/DOWN arrows
  412.   lcd.createChar(0, armsUpDn);
  413.   lcd.createChar(1, customUp);
  414.   lcd.createChar(2, customDown);
  415.  
  416.   // Display startup message
  417.   lcd.setCursor(1, 0);
  418.   lcd.print("MEHMET IBRAHIM");
  419.   lcd.setCursor(3, 1);
  420.   lcd.print("Plasma THC");
  421.   delay(1500);
  422.   lcd.clear();
  423. }
  424.  
  425. // Update LCD display - main display controller
  426. void doLCD() {
  427.   if (show >= 2) {
  428.     show = 0;
  429.     switch (menu) {
  430.       case 0:
  431.         doLCDDefault();
  432.         break;
  433.       case 1:
  434.         doLCDMenu();
  435.         break;
  436.       case 11:
  437.         doLCDMenuSetup();
  438.         break;
  439.       case 111:
  440.         doLCDDelayTime();
  441.         break;
  442.       case 112:
  443.         doLCDHysreresis();
  444.         break;
  445.       case 113:
  446.         doLCDStartVoltage();
  447.         break;
  448.       case 114:
  449.         doLCDLoadDefault();
  450.         break;
  451.       case 12:
  452.         doLCDTest();
  453.         break;
  454.       case 115:
  455.         doTestUp();
  456.         break;
  457.       case 116:
  458.         doTestDown();
  459.         break;
  460.       case 13:
  461.         doLCDProgramSellect();
  462.         break;
  463.       case 121:
  464.         doProgramSet(1);
  465.         break;
  466.       case 122:
  467.         doProgramSet(2);
  468.         break;
  469.       case 123:
  470.         doProgramSet(3);
  471.         break;
  472.  
  473.       default:
  474.         doLCDDefault();
  475.     }
  476.   }
  477. }
  478.  
  479. // Display default/home screen with voltage and status
  480. void doLCDDefault() {
  481.   if (encoderVal < 0) encoderVal = 0;
  482.   else if (encoderVal > 250) encoderVal = 250;
  483.   SetV = encoderVal;
  484.   if (SetV != oldValue) {
  485.     SaveData(SetVa, SetV);
  486.     oldValue = SetV;
  487.   }
  488.   lcd.setCursor(0, 0);
  489.   lcd.print("P ");
  490.  
  491.   if (digitalRead(outputUpPin) == HIGH) {
  492.     lcd.write(1);
  493.   } else {
  494.     lcd.print(" ");
  495.   }
  496.   lcd.print(" ");
  497.   lcd.setCursor(4, 0);
  498.   lcd.print("Set.V: ");
  499.   lcd.print(SetV);
  500.   lcd.print("   ");
  501.   lcd.setCursor(0, 1);
  502.   lcd.print(program);
  503.   lcd.print(" ");
  504.   if (digitalRead(outputDnPin) == HIGH) {
  505.     lcd.write(2);
  506.   } else {
  507.     lcd.print(" ");
  508.   }
  509.   lcd.print(" ");
  510.   lcd.setCursor(4, 1);
  511.   lcd.print("Arc.V: ");
  512.   lcd.print(ArcV / 10);
  513.   lcd.print("   ");
  514. }
  515.  
  516. // Main menu navigation
  517. void doLCDMenu() {
  518.   if (encoderVal < 0) encoderVal = 3;
  519.   pos = encoderVal % 4;
  520.   switch (pos) {
  521.     case 0:
  522.       lcd.setCursor(0, 0);
  523.       lcd.print("> Exit          ");
  524.       lcd.setCursor(0, 1);
  525.       lcd.print("  Program       ");
  526.       break;
  527.     case 1:
  528.       lcd.setCursor(0, 0);
  529.       lcd.print("> Program       ");
  530.       lcd.setCursor(0, 1);
  531.       lcd.print("  Setup         ");
  532.       break;
  533.     case 2:
  534.       lcd.setCursor(0, 0);
  535.       lcd.print("> Setup         ");
  536.       lcd.setCursor(0, 1);
  537.       lcd.print("  Test          ");
  538.       break;
  539.     case 3:
  540.       lcd.setCursor(0, 0);
  541.       lcd.print("> Test          ");
  542.       lcd.setCursor(0, 1);
  543.       lcd.print("  Exit       ");
  544.       break;
  545.   }
  546. }
  547.  
  548. // Program selection menu
  549. void doLCDProgramSellect() {
  550.   if (encoderVal < 0) encoderVal = 3;
  551.   pos = abs(encoderVal % 4);
  552.   switch (pos) {
  553.     case 0:
  554.       lcd.setCursor(0, 0);
  555.       lcd.print(">> Exit         ");
  556.       lcd.setCursor(0, 1);
  557.       lcd.print("   Load Prog: 1 ");
  558.       break;
  559.     case 1:
  560.       lcd.setCursor(0, 0);
  561.       lcd.print(">> Load Prog: 1 ");
  562.       lcd.setCursor(0, 1);
  563.       lcd.print("   Load Prog: 2 ");
  564.       break;
  565.     case 2:
  566.       lcd.setCursor(0, 0);
  567.       lcd.print(">> Load Prog: 2 ");
  568.       lcd.setCursor(0, 1);
  569.       lcd.print("   Load Prog: 3 ");
  570.       break;
  571.     case 3:
  572.       lcd.setCursor(0, 0);
  573.       lcd.print(">> Load Prog: 3 ");
  574.       lcd.setCursor(0, 1);
  575.       lcd.print("   Exit         ");
  576.       break;
  577.   }
  578. }
  579.  
  580. // Setup menu navigation
  581. void doLCDMenuSetup() {
  582.   if (encoderVal < 0) encoderVal = 4;
  583.   pos = abs(encoderVal % 5);
  584.   switch (pos) {
  585.     case 0:
  586.       lcd.setCursor(0, 0);
  587.       lcd.print(">> Exit         ");
  588.       lcd.setCursor(0, 1);
  589.       lcd.print("   Delay Time   ");
  590.       break;
  591.     case 1:
  592.       lcd.setCursor(0, 0);
  593.       lcd.print(">> Delay Time   ");
  594.       lcd.setCursor(0, 1);
  595.       lcd.print("   Hysteresis   ");
  596.       break;
  597.     case 2:
  598.       lcd.setCursor(0, 0);
  599.       lcd.print(">> Hysteresis   ");
  600.       lcd.setCursor(0, 1);
  601.       lcd.print("   Start Voltage");
  602.       break;
  603.     case 3:
  604.       lcd.setCursor(0, 0);
  605.       lcd.print(">> Start Voltage");
  606.       lcd.setCursor(0, 1);
  607.       lcd.print("   Load Default ");
  608.       break;
  609.     case 4:
  610.       lcd.setCursor(0, 0);
  611.       lcd.print(">> Load Default ");
  612.       lcd.setCursor(0, 1);
  613.       lcd.print("   Exit         ");
  614.       break;
  615.   }
  616. }
  617.  
  618. // Test menu navigation
  619. void doLCDTest() {
  620.   if (encoderVal < 0) encoderVal = 2;
  621.   pos = abs(encoderVal % 3);
  622.   switch (pos) {
  623.     case 0:
  624.       lcd.setCursor(0, 0);
  625.       lcd.print("Test > Exit     ");
  626.       lcd.setCursor(0, 1);
  627.       lcd.print("       Torch Up ");
  628.       digitalWrite(outputDnPin, LOW);
  629.       digitalWrite(outputUpPin, LOW);
  630.       digitalWrite(outputOkPin, LOW);
  631.       break;
  632.     case 1:
  633.       lcd.setCursor(0, 0);
  634.       lcd.print("Test > Torch Up ");
  635.       lcd.setCursor(0, 1);
  636.       lcd.print("       Torch Dn ");
  637.       if (digitalRead(outputOkPin) == LOW) LCDtime = 0;
  638.       if (LCDtime >= 200) {
  639.         digitalWrite(outputDnPin, LOW);
  640.         digitalWrite(outputUpPin, LOW);
  641.         digitalWrite(outputOkPin, LOW);
  642.       }
  643.       break;
  644.     case 2:
  645.       lcd.setCursor(0, 0);
  646.       lcd.print("Test > Torch Dn ");
  647.       lcd.setCursor(0, 1);
  648.       lcd.print("       Exit     ");
  649.       if (digitalRead(outputOkPin) == LOW) LCDtime = 0;
  650.       if (LCDtime >= 200) {
  651.         digitalWrite(outputDnPin, LOW);
  652.         digitalWrite(outputUpPin, LOW);
  653.         digitalWrite(outputOkPin, LOW);
  654.       }
  655.       break;
  656.   }
  657. }
  658.  
  659. // Test torch UP - activate UP output
  660. void doTestUp() {
  661.   digitalWrite(outputDnPin, LOW);
  662.   digitalWrite(outputUpPin, HIGH);
  663.   digitalWrite(outputOkPin, HIGH);
  664.   LCDtime = 0;
  665.   menu = 12;
  666.   encoderVal = 1;
  667. }
  668.  
  669. // Test torch DOWN - activate DOWN output
  670. void doTestDown() {
  671.   digitalWrite(outputDnPin, HIGH);
  672.   digitalWrite(outputUpPin, LOW);
  673.   digitalWrite(outputOkPin, HIGH);
  674.   LCDtime = 0;
  675.   menu = 12;
  676.   encoderVal = 2;
  677. }
  678.  
  679. // Delay time adjustment display and control
  680. void doLCDDelayTime() {
  681.   if (encoderVal < 1) encoderVal = 1;
  682.   else if (encoderVal > 200) encoderVal = 200;
  683.  
  684.   DT = encoderVal;
  685.   if (DT != oldValue) {
  686.     SaveData(DTa, DT);
  687.     oldValue = DT;
  688.     LCDtime = 0;
  689.   }
  690.  
  691.   double x = DT / 10.00;
  692.   lcd.setCursor(0, 0);
  693.   lcd.print("Set > Delay Time");
  694.   lcd.setCursor(0, 1);
  695.   lcd.print("     : ");
  696.   lcd.print(x, 1);
  697.   lcd.print(" s       ");
  698. }
  699.  
  700. // Hysteresis adjustment display and control
  701. void doLCDHysreresis() {
  702.   if (encoderVal < 1) encoderVal = 1;
  703.   else if (encoderVal > 99) encoderVal = 99;
  704.  
  705.   HyS = encoderVal;
  706.   if (HyS != oldValue) {
  707.     SaveData(HySa, HyS);
  708.     oldValue = HyS;
  709.     LCDtime = 0;
  710.   }
  711.  
  712.   double x = HyS / 10.00;
  713.   lcd.setCursor(0, 0);
  714.   lcd.print("Set > Hysteresis");
  715.   lcd.setCursor(0, 1);
  716.   lcd.print("     :");
  717.   lcd.write(0);
  718.   lcd.print(x, 1);
  719.   lcd.print(" V       ");
  720. }
  721.  
  722. // Start voltage adjustment display and control
  723. void doLCDStartVoltage() {
  724.   if (encoderVal < 50) encoderVal = 50;
  725.   else if (encoderVal > 250) encoderVal = 250;
  726.  
  727.   StV = encoderVal;
  728.   if (StV != oldValue) {
  729.     SaveData(StVa, StV);
  730.     oldValue = StV;
  731.     LCDtime = 0;
  732.   }
  733.  
  734.   lcd.setCursor(0, 0);
  735.   lcd.print("Set > Start Volt");
  736.   lcd.setCursor(0, 1);
  737.   lcd.print("     : ");
  738.   lcd.print(StV);
  739.   lcd.print(" V       ");
  740. }
  741.  
  742. // Load default values - initialize EEPROM with factory defaults
  743. void doLCDLoadDefault() {
  744.   Default();
  745.  
  746.   for (byte i = 0; i < 100; i++) {
  747.     lcd.setCursor(0, 0);
  748.     lcd.print("     Default    ");
  749.     lcd.setCursor(0, 1);
  750.     lcd.print("Load   ");
  751.     lcd.print(i);
  752.     lcd.print(" ");
  753.     lcd.print("%");
  754.     lcd.print("        ");
  755.     delay(5);
  756.   }
  757.   lcd.setCursor(0, 0);
  758.   lcd.print("Default:  DONE  ");
  759.   lcd.setCursor(0, 1);
  760.   lcd.print("Please Restart  ");
  761.   exit(0);
  762. }
  763.  
  764. // ============ THC CONTROL FUNCTIONS ============
  765.  
  766. // Initialize THC control - setup GPIO pins
  767. void Setup_THC() {
  768.   pinMode(outputUpPin, OUTPUT);  // GPIO 27
  769.   pinMode(outputOkPin, OUTPUT);  // GPIO 25
  770.   pinMode(outputDnPin, OUTPUT);  // GPIO 26
  771.  
  772.   // Initialize all outputs to LOW (off)
  773.   digitalWrite(outputUpPin, LOW);
  774.   digitalWrite(outputOkPin, LOW);
  775.   digitalWrite(outputDnPin, LOW);
  776. }
  777.  
  778. // Main THC control algorithm - called from timer callback
  779. void doTHC() {
  780.   if (Do) {
  781.     Do = false;
  782.     LCDtime++;
  783.     show++;
  784.  
  785.     // Auto-return to default menu after timeout (1.5s = 30 * 50ms)
  786.     if (LCDtime > defaultLCDtime) {
  787.       menu = 0;
  788.       pos = 0;
  789.       LCDtime = 0;
  790.       encoderVal = SetV;
  791.     }
  792.    
  793.     // THC control logic - only active when arc voltage is reasonable
  794.     if ((500 < ArcV) && (ArcV < 2500)) {
  795.       // Increment delay counter until delay time is met
  796.       if (ArcV > StV * 10) delayTime++;
  797.  
  798.       // Once delay time reached, begin torch height control
  799.       if (delayTime >= DT * 10) {
  800.         SetVx10 = SetV * 10;
  801.         delayTime = DT * 10;
  802.  
  803.         // Signal arc detection confirmed
  804.         digitalWrite(outputOkPin, HIGH);
  805.  
  806.         // Torch height control with hysteresis
  807.         if (ArcV >= SetVx10 + HyS) {
  808.           // Arc voltage too high - move torch DOWN
  809.           digitalWrite(outputUpPin, LOW);
  810.           digitalWrite(outputDnPin, HIGH);
  811.         } else if (ArcV <= SetVx10 - HyS) {
  812.           // Arc voltage too low - move torch UP
  813.           digitalWrite(outputDnPin, LOW);
  814.           digitalWrite(outputUpPin, HIGH);
  815.         } else {
  816.           // Arc voltage within acceptable range - hold position
  817.           digitalWrite(outputUpPin, LOW);
  818.           digitalWrite(outputDnPin, LOW);
  819.         }
  820.       }
  821.     } else if (menu != 12) {
  822.       // No valid arc or not in test mode - deactivate all outputs
  823.       delayTime = 0;
  824.       digitalWrite(outputUpPin, LOW);
  825.       digitalWrite(outputOkPin, LOW);
  826.       digitalWrite(outputDnPin, LOW);
  827.     }
  828.   }
  829. }
  830.  
  831. /* END CODE */
  832.  
Advertisement
Add Comment
Please, Sign In to add comment