pleasedontcode

# Plasma Controller rev_12

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