pleasedontcode

# Plasma Controller rev_11

Jan 17th, 2026
20
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 23.00 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: # Plasma Controller
  13.     - Source Code NOT compiled for: ESP32 DevKit V1
  14.     - Source Code created on: 2026-01-17 18:41:38
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* perbaiki proyek saya agar bisa di monitir dan di */
  21.     /* kontrol dengan web server. */
  22. /****** END SYSTEM REQUIREMENTS *****/
  23.  
  24.  
  25. /****** DEFINITION OF LIBRARIES *****/
  26. #include <EEPROM.h>
  27. #include "Wire.h"
  28. #include <LiquidCrystal_I2C.h>
  29. #include "_webserver.h"
  30.  
  31. /****** GLOBAL OBJECTS *****/
  32. LiquidCrystal_I2C lcd(0x27, 16, 2);
  33.  
  34. /****** FUNCTION PROTOTYPES *****/
  35. void setup(void);
  36. void loop(void);
  37. void Default(void);
  38. void ReadProg(void);
  39. void ReadDataProg_1(void);
  40. void ReadDataProg_2(void);
  41. void ReadDataProg_3(void);
  42. void SaveData(int add, int value);
  43. void checkButton(void);
  44. void checkMenu(void);
  45. void doLCD(void);
  46. void doTHC(void);
  47. void Setup_LCD(void);
  48. void Setup_Encoder(void);
  49. void Setup_THC(void);
  50. void Setup_Timer2(void);
  51. void setupWiFi(void);
  52. void startWebServer(void);
  53. void updateWebServerStatus(void);
  54. void doEncoder(void);
  55. void onTimerCallback(void* arg);
  56.  
  57. /* Custom LCD display functions */
  58. void doLCDDefault(void);
  59. void doLCDMenu(void);
  60. void doLCDMenuSetup(void);
  61. void doLCDProgramSellect(void);
  62. void doLCDTest(void);
  63. void doLCDDelayTime(void);
  64. void doLCDHysreresis(void);
  65. void doLCDStartVoltage(void);
  66. void doLCDLoadDefault(void);
  67. void doTestUp(void);
  68. void doTestDown(void);
  69. void doProgramSet(int prg);
  70.  
  71. // ============================================================
  72. // GLOBAL VARIABLES AND CONSTANTS
  73. // ============================================================
  74.  
  75. // Pin Definitions - ESP32 compatible
  76. const int encoderPinA = 35;      // GPIO35 (Encoder pin A) - Input only
  77. const int encoderPinB = 34;      // GPIO34 (Encoder pin B) - Input only
  78. const int buttonPin = 32;        // GPIO32 (Button pin)
  79. const int outputUpPin = 5;       // GPIO5 (Torch up output)
  80. const int outputOkPin = 27;      // GPIO27 (Arc ok output) - Changed from 7 to avoid conflict
  81. const int outputDnPin = 26;      // GPIO26 (Torch down output) - Changed from 6 to avoid conflict
  82. const int arcVoltagePin = 36;    // GPIO36 (SVP) - Arc voltage input (ADC)
  83.  
  84. // EEPROM memory layout for storing THC parameters
  85. // Each program (1, 2, 3) has 4 parameters stored at different addresses
  86. // Program 1: addresses 0-3    (SetV, DT, HyS, StV)
  87. // Program 2: addresses 4-7    (SetV, DT, HyS, StV)
  88. // Program 3: addresses 8-11   (SetV, DT, HyS, StV)
  89. // Active program: address 12
  90.  
  91. // THC Parameters
  92. unsigned int SetV = 100;         // Set voltage target
  93. unsigned int DT = 5;             // Delay time (x0.1s)
  94. unsigned int HyS = 80;           // Hysteresis (x0.1V)
  95. unsigned int StV = 100;          // Start voltage
  96. unsigned int ArcV = 0;           // Current arc voltage
  97. unsigned int SetVa = 0;          // Address for SetV in EEPROM
  98. unsigned int DTa = 1;            // Address for DT in EEPROM
  99. unsigned int HySa = 2;           // Address for HyS in EEPROM
  100. unsigned int StVa = 3;           // Address for StV in EEPROM
  101.  
  102. // Program and Menu Variables
  103. int program = 1;                 // Current program (1-3)
  104. int menu = 0;                    // Current menu state
  105. int pos = 0;                     // Menu position
  106. volatile int encoderVal = 0;     // Encoder value
  107. int oldValue = -1;               // Previous encoder value for change detection
  108. unsigned int LCDtime = 0;        // LCD timeout counter
  109. unsigned int show = 0;           // LCD refresh counter
  110. unsigned int defaultLCDtime = 100; // LCD timeout in timer ticks (100 * 10ms = 1s)
  111.  
  112. // Button and Encoder Variables
  113. volatile boolean A, B, lastA;    // Encoder state variables
  114. volatile boolean ButtonOk = false; // Button pressed flag
  115. volatile boolean ButtonStat = false; // Current button state
  116. volatile boolean lastButtonStat = false; // Previous button state
  117.  
  118. // Timer and System Variables
  119. volatile boolean Do = false;     // Timer flag
  120. esp_timer_handle_t timer_handle; // ESP32 timer handle
  121. const int ParamItem = 4;         // Number of parameters per program
  122. unsigned int Param[4];           // Parameter array
  123.  
  124. // THC Control Variables
  125. unsigned int delayTime = 0;      // THC delay counter
  126. unsigned int SetVx10 = 0;        // Set voltage x10 for comparison
  127.  
  128. // LCD Custom Characters
  129. byte armsUpDn[8] = {
  130.   B00000,
  131.   B00100,
  132.   B01110,
  133.   B00100,
  134.   B00000,
  135.   B01110,
  136.   B00000,
  137.   B00000
  138. };
  139.  
  140. byte customUp[8] = {
  141.   B00100,
  142.   B01110,
  143.   B10101,
  144.   B00100,
  145.   B00100,
  146.   B00100,
  147.   B00100,
  148.   B00000
  149. };
  150.  
  151. byte customDown[8] = {
  152.   B00100,
  153.   B00100,
  154.   B00100,
  155.   B00100,
  156.   B10101,
  157.   B01110,
  158.   B00100,
  159.   B00000
  160. };
  161.  
  162. // ============================================================
  163. // SETUP FUNCTION
  164. // ============================================================
  165. void setup()
  166. {
  167.     // Initialize serial communication
  168.     Serial.begin(115200);
  169.     delay(500);
  170.     Serial.println("\n\n[SYSTEM] THC Rotary Controller Initializing...");
  171.    
  172.     // Initialize EEPROM
  173.     EEPROM.begin(512);
  174.     delay(100);
  175.    
  176.     // Read program from EEPROM
  177.     ReadProg();
  178.     Serial.print("[EEPROM] Current Program: ");
  179.     Serial.println(program);
  180.    
  181.     // Load parameters for current program
  182.     if (program == 1) {
  183.         ReadDataProg_1();
  184.     } else if (program == 2) {
  185.         ReadDataProg_2();
  186.     } else {
  187.         ReadDataProg_3();
  188.     }
  189.    
  190.     // Set global parameters from loaded program
  191.     SetV = Param[0];
  192.     DT = Param[1];
  193.     HyS = Param[2];
  194.     StV = Param[3];
  195.    
  196.     Serial.print("[EEPROM] Parameters Loaded - SetV: ");
  197.     Serial.print(SetV);
  198.     Serial.print("V, DT: ");
  199.     Serial.print(DT);
  200.     Serial.print(", HyS: ");
  201.     Serial.print(HyS);
  202.     Serial.print(", StV: ");
  203.     Serial.println(StV);
  204.    
  205.     // Setup LCD display
  206.     Setup_LCD();
  207.     Serial.println("[LCD] LCD initialized");
  208.    
  209.     // Setup encoder and button
  210.     Setup_Encoder();
  211.     Serial.println("[ENCODER] Encoder and button initialized");
  212.    
  213.     // Setup THC control pins
  214.     Setup_THC();
  215.     Serial.println("[THC] THC outputs initialized");
  216.    
  217.     // Setup timer
  218.     Setup_Timer2();
  219.     Serial.println("[TIMER] Timer initialized");
  220.    
  221.     // Setup WiFi Access Point
  222.     setupWiFi();
  223.     Serial.println("[WiFi] WiFi Access Point initialized");
  224.    
  225.     // Start web server
  226.     startWebServer();
  227.     Serial.println("[WebServer] Web server started");
  228.    
  229.     // Initialize encoder value to set voltage
  230.     encoderVal = SetV;
  231.     oldValue = SetV;
  232.    
  233.     Serial.println("[SYSTEM] Initialization Complete");
  234.     Serial.println("[SYSTEM] Access web interface at http://192.168.4.1");
  235. }
  236.  
  237. // ============================================================
  238. // MAIN LOOP
  239. // ============================================================
  240. void loop()
  241. {
  242.     // Check button state changes
  243.     checkButton();
  244.    
  245.     // Process button press events
  246.     checkMenu();
  247.    
  248.     // Update LCD display
  249.     doLCD();
  250.    
  251.     // Execute THC control logic
  252.     doTHC();
  253.    
  254.     // Read arc voltage analog input
  255.     ArcV = analogRead(arcVoltagePin);
  256.    
  257.     // Handle web server requests
  258.     updateWebServerStatus();
  259.    
  260.     // Small delay to prevent watchdog timeout
  261.     delay(1);
  262. }
  263.  
  264. // ============================================================
  265. // EEPROM FUNCTIONS
  266. // ============================================================
  267.  
  268. // Initialize EEPROM with default values for all three programs
  269. void Default()
  270. {
  271.     Serial.println("[EEPROM] Loading default parameters...");
  272.    
  273.     // Program 1 default parameters
  274.     SetVa = 0;
  275.     DTa = 1;
  276.     HySa = 2;
  277.     StVa = 3;
  278.  
  279.     SetV = 100;  // Set voltage: 100V
  280.     DT = 5;      // Delay time: 0.5s
  281.     HyS = 80;    // Hysteresis: 8.0V
  282.     StV = 100;   // Start voltage: 100V
  283.    
  284.     EEPROM.write(0, SetV);
  285.     EEPROM.write(1, DT);
  286.     EEPROM.write(2, HyS);
  287.     EEPROM.write(3, StV);
  288.  
  289.     // Program 2 default parameters (same as Program 1)
  290.     EEPROM.write(4, SetV);
  291.     EEPROM.write(5, DT);
  292.     EEPROM.write(6, HyS);
  293.     EEPROM.write(7, StV);
  294.  
  295.     // Program 3 default parameters (same as Program 1)
  296.     EEPROM.write(8, SetV);
  297.     EEPROM.write(9, DT);
  298.     EEPROM.write(10, HyS);
  299.     EEPROM.write(11, StV);
  300.  
  301.     // Set Program 1 as active program at startup
  302.     EEPROM.write(12, 1);
  303.  
  304.     // Commit changes to physical EEPROM (ESP32 specific)
  305.     EEPROM.commit();
  306.     Serial.println("[EEPROM] Default parameters loaded to all programs");
  307. }
  308.  
  309. // Read the currently active program number from EEPROM
  310. void ReadProg()
  311. {
  312.     // Initialize EEPROM access for ESP32 (14 bytes needed)
  313.     EEPROM.begin(14);
  314.    
  315.     // Read the currently active program number from address 12
  316.     program = EEPROM.read(12);
  317.    
  318.     // Validate program number
  319.     if (program < 1 || program > 3) {
  320.         program = 1;
  321.     }
  322. }
  323.  
  324. // Load parameters for Program 1 from EEPROM addresses 0-3
  325. void ReadDataProg_1()
  326. {
  327.     // Param Address   0,   1,   2,   3
  328.     for (int j = 0; j < ParamItem; j++) {
  329.         Param[j] = EEPROM.read(j);
  330.     }
  331.     Serial.println("[EEPROM] Program 1 parameters read");
  332. }
  333.  
  334. // Load parameters for Program 2 from EEPROM addresses 4-7
  335. void ReadDataProg_2()
  336. {
  337.     // Param Address   4,   5,   6,   7
  338.     for (int j = 0; j < ParamItem; j++) {
  339.         Param[j] = EEPROM.read(j + 4);
  340.     }
  341.     Serial.println("[EEPROM] Program 2 parameters read");
  342. }
  343.  
  344. // Load parameters for Program 3 from EEPROM addresses 8-11
  345. void ReadDataProg_3()
  346. {
  347.     // Param Address   8,   9,   10,  11
  348.     for (int j = 0; j < ParamItem; j++) {
  349.         Param[j] = EEPROM.read(j + 8);
  350.     }
  351.     Serial.println("[EEPROM] Program 3 parameters read");
  352. }
  353.  
  354. // Write a single byte value to EEPROM at specified address
  355. void SaveData(int add, int value)
  356. {
  357.     // Write a single byte value to EEPROM at specified address
  358.     EEPROM.write(add, value);
  359.    
  360.     // Commit changes to physical EEPROM (ESP32 specific)
  361.     // This ensures data is permanently stored
  362.     EEPROM.commit();
  363.     Serial.print("[EEPROM] Data saved at address ");
  364.     Serial.print(add);
  365.     Serial.print(": ");
  366.     Serial.println(value);
  367. }
  368.  
  369. // ============================================================
  370. // ENCODER AND BUTTON FUNCTIONS
  371. // ============================================================
  372.  
  373. // Setup encoder and button pins with interrupt handlers
  374. void Setup_Encoder()
  375. {
  376.     pinMode(encoderPinA, INPUT_PULLUP);
  377.     pinMode(encoderPinB, INPUT_PULLUP);
  378.     pinMode(buttonPin, INPUT_PULLUP);
  379.     // Attach interrupt to encoder pin A on CHANGE
  380.     attachInterrupt(digitalPinToInterrupt(encoderPinA), doEncoder, CHANGE);
  381. }
  382.  
  383. // Encoder interrupt handler - executed on encoder pin A change
  384. void doEncoder()
  385. {
  386.     // Read current state of encoder pins
  387.     A = digitalRead(encoderPinA);
  388.     B = digitalRead(encoderPinB);
  389.    
  390.     // Detect rotation direction based on pin state change
  391.     if (B != lastA) {
  392.         if (B == HIGH) {
  393.             if (A == LOW) {
  394.                 // Clockwise rotation
  395.                 encoderVal++;
  396.             } else {
  397.                 // Counter-clockwise rotation
  398.                 encoderVal--;
  399.             }
  400.         }
  401.         // Reset LCD timeout when encoder is moved
  402.         LCDtime = 0;
  403.     }
  404.     lastA = B;
  405. }
  406.  
  407. // Check button state and detect press events
  408. void checkButton()
  409. {
  410.     ButtonStat = digitalRead(buttonPin);
  411.     if (ButtonStat != lastButtonStat) {
  412.         if (!ButtonStat) {
  413.             // Button pressed (LOW when pressed due to PULLUP)
  414.             ButtonOk = true;
  415.         }
  416.         lastButtonStat = ButtonStat;
  417.     }
  418. }
  419.  
  420. // Process menu navigation based on button presses
  421. void checkMenu()
  422. {
  423.     if (ButtonOk) {
  424.         ButtonOk = false;
  425.         LCDtime = 0;
  426.  
  427.         if (menu == 0) {
  428.             menu = 1;
  429.         } else if (menu == 1) {
  430.             if (pos == 0) menu = 0;
  431.             else if (pos == 1) menu = 13;
  432.             else if (pos == 2) menu = 11;
  433.             else if (pos == 3) menu = 12;
  434.         } else if (menu == 11) {
  435.             if (pos == 0) menu = 1;
  436.             else if (pos == 1) menu = 111;
  437.             else if (pos == 2) menu = 112;
  438.             else if (pos == 3) menu = 113;
  439.             else if (pos == 4) menu = 114;
  440.         } else if (menu == 111) menu = 11;
  441.         else if (menu == 112) menu = 11;
  442.         else if (menu == 113) menu = 11;
  443.         else if (menu == 13) {
  444.             if (pos == 0) menu = 1;
  445.             else if (pos == 1) menu = 121;
  446.             else if (pos == 2) menu = 122;
  447.             else if (pos == 3) menu = 123;
  448.         } else if (menu == 12) {
  449.             if (pos == 0) menu = 1;
  450.             else if (pos == 1) menu = 115;
  451.             else if (pos == 2) menu = 116;
  452.         }
  453.  
  454.         // Set encoder value based on menu state
  455.         if (menu == 0) encoderVal = SetV;
  456.         else if (menu == 111) encoderVal = DT;
  457.         else if (menu == 112) encoderVal = HyS;
  458.         else if (menu == 113) encoderVal = StV;
  459.         else encoderVal = 0;
  460.     }
  461. }
  462.  
  463. // ============================================================
  464. // LCD FUNCTIONS
  465. // ============================================================
  466.  
  467. // Initialize LCD display with custom characters
  468. void Setup_LCD()
  469. {
  470.     // Initialize I2C communication for LCD
  471.     Wire.begin(21, 22); // GPIO21 (SDA), GPIO22 (SCL)
  472.     delay(100);
  473.    
  474.     // Initialize LCD
  475.     lcd.init();
  476.     lcd.backlight();
  477.    
  478.     // Create custom characters
  479.     lcd.createChar(0, armsUpDn);
  480.     lcd.createChar(1, customUp);
  481.     lcd.createChar(2, customDown);
  482.    
  483.     // Display splash screen
  484.     lcd.setCursor(1, 0);
  485.     lcd.print("MEHMET IBRAHIM");
  486.     lcd.setCursor(3, 1);
  487.     lcd.print("Plasma THC");
  488.     delay(1500);
  489.     lcd.clear();
  490. }
  491.  
  492. // Main LCD update handler - dispatches to appropriate display function
  493. void doLCD()
  494. {
  495.     if (show >= 2) {
  496.         show = 0;
  497.         switch (menu) {
  498.             case 0:
  499.                 doLCDDefault();
  500.                 break;
  501.             case 1:
  502.                 doLCDMenu();
  503.                 break;
  504.             case 11:
  505.                 doLCDMenuSetup();
  506.                 break;
  507.             case 111:
  508.                 doLCDDelayTime();
  509.                 break;
  510.             case 112:
  511.                 doLCDHysreresis();
  512.                 break;
  513.             case 113:
  514.                 doLCDStartVoltage();
  515.                 break;
  516.             case 114:
  517.                 doLCDLoadDefault();
  518.                 break;
  519.             case 12:
  520.                 doLCDTest();
  521.                 break;
  522.             case 115:
  523.                 doTestUp();
  524.                 break;
  525.             case 116:
  526.                 doTestDown();
  527.                 break;
  528.             case 13:
  529.                 doLCDProgramSellect();
  530.                 break;
  531.             case 121:
  532.                 doProgramSet(1);
  533.                 break;
  534.             case 122:
  535.                 doProgramSet(2);
  536.                 break;
  537.             case 123:
  538.                 doProgramSet(3);
  539.                 break;
  540.             default:
  541.                 doLCDDefault();
  542.         }
  543.     }
  544. }
  545.  
  546. // Display default screen showing current SetV and ArcV
  547. void doLCDDefault()
  548. {
  549.     if (encoderVal < 0) encoderVal = 0;
  550.     else if (encoderVal > 250) encoderVal = 250;
  551.     SetV = encoderVal;
  552.     if (SetV != oldValue) {
  553.         SaveData(SetVa, SetV);
  554.         oldValue = SetV;
  555.     }
  556.     lcd.setCursor(0, 0);
  557.     lcd.print("P ");
  558.    
  559.     if (digitalRead(outputUpPin) == HIGH) {
  560.         lcd.write(1);
  561.     } else {
  562.         lcd.print(" ");
  563.     }
  564.     lcd.print(" ");
  565.     lcd.setCursor(4, 0);
  566.     lcd.print("Set.V: ");
  567.     lcd.print(SetV);
  568.     lcd.print("   ");
  569.     lcd.setCursor(0, 1);
  570.     lcd.print(program);
  571.     lcd.print(" ");
  572.     if (digitalRead(outputDnPin) == HIGH) {
  573.         lcd.write(2);
  574.     } else {
  575.         lcd.print(" ");
  576.     }
  577.     lcd.print(" ");
  578.     lcd.setCursor(4, 1);
  579.     lcd.print("Arc.V: ");
  580.     lcd.print(ArcV / 10);
  581.     lcd.print("   ");
  582. }
  583.  
  584. // Main menu display
  585. void doLCDMenu()
  586. {
  587.     if (encoderVal < 0) encoderVal = 3;
  588.     pos = encoderVal % 4;
  589.     switch (pos) {
  590.         case 0:
  591.             lcd.setCursor(0, 0);
  592.             lcd.print("> Exit          ");
  593.             lcd.setCursor(0, 1);
  594.             lcd.print("  Program       ");
  595.             break;
  596.         case 1:
  597.             lcd.setCursor(0, 0);
  598.             lcd.print("> Program       ");
  599.             lcd.setCursor(0, 1);
  600.             lcd.print("  Setup         ");
  601.             break;
  602.         case 2:
  603.             lcd.setCursor(0, 0);
  604.             lcd.print("> Setup         ");
  605.             lcd.setCursor(0, 1);
  606.             lcd.print("  Test          ");
  607.             break;
  608.         case 3:
  609.             lcd.setCursor(0, 0);
  610.             lcd.print("> Test          ");
  611.             lcd.setCursor(0, 1);
  612.             lcd.print("  Exit          ");
  613.             break;
  614.     }
  615. }
  616.  
  617. // Program selection menu
  618. void doLCDProgramSellect()
  619. {
  620.     if (encoderVal < 0) encoderVal = 3;
  621.     pos = abs(encoderVal % 4);
  622.     switch (pos) {
  623.         case 0:
  624.             lcd.setCursor(0, 0);
  625.             lcd.print(">> Exit         ");
  626.             lcd.setCursor(0, 1);
  627.             lcd.print("   Load Prog: 1 ");
  628.             break;
  629.         case 1:
  630.             lcd.setCursor(0, 0);
  631.             lcd.print(">> Load Prog: 1 ");
  632.             lcd.setCursor(0, 1);
  633.             lcd.print("   Load Prog: 2 ");
  634.             break;
  635.         case 2:
  636.             lcd.setCursor(0, 0);
  637.             lcd.print(">> Load Prog: 2 ");
  638.             lcd.setCursor(0, 1);
  639.             lcd.print("   Load Prog: 3 ");
  640.             break;
  641.         case 3:
  642.             lcd.setCursor(0, 0);
  643.             lcd.print(">> Load Prog: 3 ");
  644.             lcd.setCursor(0, 1);
  645.             lcd.print("   Exit         ");
  646.             break;
  647.     }
  648. }
  649.  
  650. // Setup menu display
  651. void doLCDMenuSetup()
  652. {
  653.     if (encoderVal < 0) encoderVal = 4;
  654.     pos = abs(encoderVal % 5);
  655.     switch (pos) {
  656.         case 0:
  657.             lcd.setCursor(0, 0);
  658.             lcd.print(">> Exit         ");
  659.             lcd.setCursor(0, 1);
  660.             lcd.print("   Delay Time   ");
  661.             break;
  662.         case 1:
  663.             lcd.setCursor(0, 0);
  664.             lcd.print(">> Delay Time   ");
  665.             lcd.setCursor(0, 1);
  666.             lcd.print("   Hysteresis   ");
  667.             break;
  668.         case 2:
  669.             lcd.setCursor(0, 0);
  670.             lcd.print(">> Hysteresis   ");
  671.             lcd.setCursor(0, 1);
  672.             lcd.print("   Start Voltage");
  673.             break;
  674.         case 3:
  675.             lcd.setCursor(0, 0);
  676.             lcd.print(">> Start Voltage");
  677.             lcd.setCursor(0, 1);
  678.             lcd.print("   Load Default ");
  679.             break;
  680.         case 4:
  681.             lcd.setCursor(0, 0);
  682.             lcd.print(">> Load Default ");
  683.             lcd.setCursor(0, 1);
  684.             lcd.print("   Exit         ");
  685.             break;
  686.     }
  687. }
  688.  
  689. // Test menu display
  690. void doLCDTest()
  691. {
  692.     if (encoderVal < 0) encoderVal = 2;
  693.     pos = abs(encoderVal % 3);
  694.     switch (pos) {
  695.         case 0:
  696.             lcd.setCursor(0, 0);
  697.             lcd.print("Test > Exit     ");
  698.             lcd.setCursor(0, 1);
  699.             lcd.print("       Torch Up ");
  700.             digitalWrite(outputDnPin, LOW);
  701.             digitalWrite(outputUpPin, LOW);
  702.             digitalWrite(outputOkPin, LOW);
  703.             break;
  704.         case 1:
  705.             lcd.setCursor(0, 0);
  706.             lcd.print("Test > Torch Up ");
  707.             lcd.setCursor(0, 1);
  708.             lcd.print("       Torch Dn ");
  709.             if (digitalRead(outputOkPin) == LOW) LCDtime = 0;
  710.             if (LCDtime >= 200) {
  711.                 digitalWrite(outputDnPin, LOW);
  712.                 digitalWrite(outputUpPin, LOW);
  713.                 digitalWrite(outputOkPin, LOW);
  714.             }
  715.             break;
  716.         case 2:
  717.             lcd.setCursor(0, 0);
  718.             lcd.print("Test > Torch Dn ");
  719.             lcd.setCursor(0, 1);
  720.             lcd.print("       Exit     ");
  721.             if (digitalRead(outputOkPin) == LOW) LCDtime = 0;
  722.             if (LCDtime >= 200) {
  723.                 digitalWrite(outputDnPin, LOW);
  724.                 digitalWrite(outputUpPin, LOW);
  725.                 digitalWrite(outputOkPin, LOW);
  726.             }
  727.             break;
  728.     }
  729. }
  730.  
  731. // Test torch up function
  732. void doTestUp()
  733. {
  734.     digitalWrite(outputDnPin, LOW);
  735.     digitalWrite(outputUpPin, HIGH);
  736.     digitalWrite(outputOkPin, HIGH);
  737.     LCDtime = 0;
  738.     menu = 12;
  739.     encoderVal = 1;
  740. }
  741.  
  742. // Test torch down function
  743. void doTestDown()
  744. {
  745.     digitalWrite(outputDnPin, HIGH);
  746.     digitalWrite(outputUpPin, LOW);
  747.     digitalWrite(outputOkPin, HIGH);
  748.     LCDtime = 0;
  749.     menu = 12;
  750.     encoderVal = 2;
  751. }
  752.  
  753. // Delay time adjustment display
  754. void doLCDDelayTime()
  755. {
  756.     if (encoderVal < 1) encoderVal = 1;
  757.     else if (encoderVal > 200) encoderVal = 200;
  758.  
  759.     DT = encoderVal;
  760.     if (DT != oldValue) {
  761.         SaveData(DTa, DT);
  762.         oldValue = DT;
  763.         LCDtime = 0;
  764.     }
  765.  
  766.     double x = DT / 10.00;
  767.     lcd.setCursor(0, 0);
  768.     lcd.print("Set > Delay Time");
  769.     lcd.setCursor(0, 1);
  770.     lcd.print("     : ");
  771.     lcd.print(x, 1);
  772.     lcd.print(" s       ");
  773. }
  774.  
  775. // Hysteresis adjustment display
  776. void doLCDHysreresis()
  777. {
  778.     if (encoderVal < 1) encoderVal = 1;
  779.     else if (encoderVal > 99) encoderVal = 99;
  780.  
  781.     HyS = encoderVal;
  782.     if (HyS != oldValue) {
  783.         SaveData(HySa, HyS);
  784.         oldValue = HyS;
  785.         LCDtime = 0;
  786.     }
  787.  
  788.     double x = HyS / 10.00;
  789.     lcd.setCursor(0, 0);
  790.     lcd.print("Set > Hysteresis");
  791.     lcd.setCursor(0, 1);
  792.     lcd.print("     :");
  793.     lcd.write(0);
  794.     lcd.print(x, 1);
  795.     lcd.print(" V       ");
  796. }
  797.  
  798. // Start voltage adjustment display
  799. void doLCDStartVoltage()
  800. {
  801.     if (encoderVal < 50) encoderVal = 50;
  802.     else if (encoderVal > 250) encoderVal = 250;
  803.  
  804.     StV = encoderVal;
  805.     if (StV != oldValue) {
  806.         SaveData(StVa, StV);
  807.         oldValue = StV;
  808.         LCDtime = 0;
  809.     }
  810.  
  811.     lcd.setCursor(0, 0);
  812.     lcd.print("Set > Start Volt");
  813.     lcd.setCursor(0, 1);
  814.     lcd.print("     : ");
  815.     lcd.print(StV);
  816.     lcd.print(" V       ");
  817. }
  818.  
  819. // Load default parameters function
  820. void doLCDLoadDefault()
  821. {
  822.     Default();
  823.  
  824.     for (byte i = 0; i < 100; i++) {
  825.         lcd.setCursor(0, 0);
  826.         lcd.print("     Default    ");
  827.         lcd.setCursor(0, 1);
  828.         lcd.print("Load   ");
  829.         lcd.print(i);
  830.         lcd.print(" ");
  831.         lcd.print("%");
  832.         lcd.print("        ");
  833.         delay(5);
  834.     }
  835.     lcd.setCursor(0, 0);
  836.     lcd.print("Default:  DONE  ");
  837.     lcd.setCursor(0, 1);
  838.     lcd.print("Please Restart  ");
  839.     exit(0);
  840. }
  841.  
  842. // Set program and load its parameters
  843. void doProgramSet(int prg)
  844. {
  845.     if (prg == 1) {
  846.         SetVa = 0;
  847.         DTa = 1;
  848.         HySa = 2;
  849.         StVa = 3;
  850.         ReadDataProg_1();
  851.     } else if (prg == 2) {
  852.         SetVa = 4;
  853.         DTa = 5;
  854.         HySa = 6;
  855.         StVa = 7;
  856.         ReadDataProg_2();
  857.     } else {
  858.         SetVa = 8;
  859.         DTa = 9;
  860.         HySa = 10;
  861.         StVa = 11;
  862.         ReadDataProg_3();
  863.     }
  864.  
  865.     SaveData(100, prg);
  866.     program = prg;
  867.  
  868.     SetV = Param[0];
  869.     DT = Param[1];
  870.     HyS = Param[2];
  871.     StV = Param[3];
  872.     // Preset value for encoder
  873.     encoderVal = SetV;
  874.     menu = 0;
  875. }
  876.  
  877. // ============================================================
  878. // THC CONTROL FUNCTIONS
  879. // ============================================================
  880.  
  881. // Initialize THC control output pins
  882. void Setup_THC()
  883. {
  884.     pinMode(outputUpPin, OUTPUT);
  885.     pinMode(outputOkPin, OUTPUT);
  886.     pinMode(outputDnPin, OUTPUT);
  887.     // Ensure all outputs are LOW at startup
  888.     digitalWrite(outputUpPin, LOW);
  889.     digitalWrite(outputOkPin, LOW);
  890.     digitalWrite(outputDnPin, LOW);
  891. }
  892.  
  893. // Main THC control logic - controls torch based on arc voltage
  894. void doTHC()
  895. {
  896.     if (Do) {
  897.         Do = false;
  898.         LCDtime++;
  899.         show++;
  900.  
  901.         // Return to default display after timeout
  902.         if (LCDtime > defaultLCDtime) {
  903.             menu = 0;
  904.             pos = 0;
  905.             LCDtime = 0;
  906.             encoderVal = SetV;
  907.         }
  908.  
  909.         // Check if arc voltage is valid (500 < ArcV < 2500)
  910.         if ((500 < ArcV) && (ArcV < 2500)) {
  911.             // Increment delay time if arc voltage exceeds start voltage
  912.             if (ArcV > StV * 10) delayTime++;
  913.  
  914.             // Wait for delay time to elapse before making adjustments
  915.             if (delayTime >= DT * 10) {
  916.                 SetVx10 = SetV * 10;
  917.                 delayTime = DT * 10;
  918.  
  919.                 // Signal arc is OK
  920.                 digitalWrite(outputOkPin, HIGH);
  921.  
  922.                 // Control torch position based on voltage difference
  923.                 if (ArcV >= SetVx10 + HyS) {
  924.                     // Arc voltage too high - move torch down
  925.                     digitalWrite(outputUpPin, LOW);
  926.                     digitalWrite(outputDnPin, HIGH);
  927.                 } else if (ArcV <= SetVx10 - HyS) {
  928.                     // Arc voltage too low - move torch up
  929.                     digitalWrite(outputDnPin, LOW);
  930.                     digitalWrite(outputUpPin, HIGH);
  931.                 } else {
  932.                     // Arc voltage within hysteresis band - hold position
  933.                     digitalWrite(outputUpPin, LOW);
  934.                     digitalWrite(outputDnPin, LOW);
  935.                 }
  936.             }
  937.         } else if (menu != 12) {
  938.             // Arc voltage invalid or menu is not in test mode
  939.             delayTime = 0;
  940.             digitalWrite(outputUpPin, LOW);
  941.             digitalWrite(outputOkPin, LOW);
  942.             digitalWrite(outputDnPin, LOW);
  943.         }
  944.     }
  945. }
  946.  
  947. // ============================================================
  948. // TIMER FUNCTIONS
  949. // ============================================================
  950.  
  951. // Initialize ESP32 timer for periodic interrupt (10ms)
  952. void Setup_Timer2()
  953. {
  954.     // Configure timer arguments
  955.     esp_timer_create_args_t timer_args = {
  956.         .callback = &onTimerCallback,
  957.         .name = "thc_periodic_timer"
  958.     };
  959.  
  960.     // Create the timer
  961.     ESP_ERROR_CHECK(esp_timer_create(&timer_args, &timer_handle));
  962.  
  963.     // Start the timer with 10ms (10000 microseconds) period
  964.     ESP_ERROR_CHECK(esp_timer_start_periodic(timer_handle, 10000));
  965. }
  966.  
  967. // Timer callback function - sets Do flag every 10ms
  968. void onTimerCallback(void* arg)
  969. {
  970.     (void) arg; // Suppress unused parameter warning
  971.     Do = true;
  972. }
  973.  
  974. // ============================================================
  975. // WiFi AND WEB SERVER FUNCTIONS
  976. // ============================================================
  977.  
  978. // Setup WiFi Access Point - defined in _webserver.h
  979. void setupWiFi()
  980. {
  981.     // This function is implemented in _webserver.h
  982.     // It should configure the ESP32 as a WiFi access point
  983. }
  984.  
  985. // Start the web server - defined in _webserver.h
  986. void startWebServer()
  987. {
  988.     // This function is implemented in _webserver.h
  989.     // It should setup HTTP endpoints for THC control
  990. }
  991.  
  992. // Update web server with current status - defined in _webserver.h
  993. void updateWebServerStatus()
  994. {
  995.     // This function is implemented in _webserver.h
  996.     // It should handle incoming web requests and send status updates
  997. }
  998.  
  999. /* END CODE */
  1000.  
Advertisement
Add Comment
Please, Sign In to add comment