pleasedontcode

# Plasma Controller rev_02

Jan 17th, 2026
21
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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 16:36:43
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* buat progres THC_rotary bisa di kontrol  dengan */
  21.     /* web server melalui wifi */
  22. /****** END SYSTEM REQUIREMENTS *****/
  23.  
  24.  
  25. /* START CODE */
  26.  
  27. /****** DEFINITION OF LIBRARIES *****/
  28. #include <esp_timer.h>  // ESP32 Timer library
  29.  
  30. /****** FUNCTION PROTOTYPES *****/
  31. void setup(void);
  32. void loop(void);
  33. void Setup_Encoder(void);
  34. void Setup_LCD(void);
  35. void Setup_THC(void);
  36. void Setup_Timer2(void);
  37. void ReadProg(void);
  38. void Default(void);
  39. void ReadDataProg_1(void);
  40. void ReadDataProg_2(void);
  41. void ReadDataProg_3(void);
  42. void checkButton(void);
  43. void checkMenu(void);
  44. void doTHC(void);
  45. void doLCD(void);
  46. void Setup_WiFi(void);
  47.  
  48. /*
  49.   Last update 24.08.2025
  50.  
  51.   Project:  THC for Plasma CNC controller
  52.   Platform: Arduino UNO R3 / ESP32
  53.   Created:  April 2015
  54.   Version:  02.00
  55.   By:       Pham Duy Anh - [email protected]
  56.  
  57.   Update and changes
  58.   By: Mehmet İbrahim - [email protected]
  59.   Youtube: https://www.youtube.com/c/MehmetIbrahim
  60.  
  61.   Adaptation to esp32 24.08.2025  Mehmet İbrahim
  62.  
  63.   System Requirements:
  64.   Create web server interface for THC control via WiFi (buat progres THC_rotary bisa di kontrol dengan web server melalui wifi)
  65.  
  66.   Require:
  67.   -> input
  68.   <- output
  69.  
  70.   -> serial Tx
  71.   -> serial Rx
  72.   -> reset
  73.  
  74.   -> rotary encoder      digital - interrupt
  75.   -> rotary encoder      digital - interrupt
  76.   -> rotary button       digital
  77.  
  78.   -> plasma Torch on     digital
  79.   -> plasma Arc Ok       analog or digital
  80.   -> plasma arc voltage  analog 0-5V
  81.  
  82.   <- Arc Ok              optocoupler
  83.   <- torch Up            optocoupler
  84.   <- torch Down          optocoupler
  85.  
  86.   <- LCD
  87.   <- LCD
  88.   <- LCD
  89.   <- LCD
  90.   <- LCD
  91.   <- LCD
  92. */
  93.  
  94. // GPIO Pin Definitions for ESP32 DevKit V1
  95. #define encoderPinA 13  // Rotary encoder pin A (INT0)
  96. #define encoderPinB 16  // Rotary encoder pin B (INT1)
  97. #define buttonPin 17    // Rotary encoder button pin
  98.  
  99. #define outputOkPin 23  // Arc Ok output (HIGH when arc detected)
  100. #define outputUpPin 25  // Torch Up output (HIGH to move torch up)
  101. #define outputDnPin 26  // Torch Down output (HIGH to move torch down)
  102.  
  103. #define arcVoltPin 34   // Arc voltage input (ADC1_CH6, 0-3.3V input range)
  104.  
  105. #define defaultLCDtime 500  // Default LCD display time (s * 100 = 5 seconds)
  106.  
  107. // ESP32 Timer configuration
  108. #define PWM_CHANNEL 0
  109. esp_timer_handle_t timer_handle;
  110. double espV_value = 3.3;  // ESP32 reference voltage (3.3V)
  111.  
  112. /*
  113.   THC Parameter Definitions:
  114.  
  115.   DT  - Delay Time: Delay before THC activation after arc detection (0.1~19.9s)
  116.   HyS - Hysteresis: Control band voltage range for Up/Down signals (±1V ~ ±15V)
  117.   StV - Start Voltage: Arc detection threshold (50~300V at input, after divider)
  118.   SetV - Set Voltage: Target arc voltage for THC feedback control (0-250V equivalent)
  119. */
  120.  
  121. // Parameter array indices
  122. int SetVa = 0, DTa = 1, HySa = 2, StVa = 3;
  123.  
  124. // Working parameter variables
  125. int SetV, DT, HyS, StV;
  126.  
  127. // Voltage divider ratio - calibrate based on your voltage divider circuit
  128. // Lower value increases displayed voltage, higher value decreases it
  129. // Typical range: 39-50 depending on resistor tolerance
  130. // Formula: ArcV = (rawADC / 4095) * 3.3 * (100 / divRatio)
  131. int divRatio = 39;
  132.  
  133. unsigned long ArcV;  // Current arc voltage (in units of 0.1V)
  134. int oldValue;        // Previous parameter value (for change detection)
  135.  
  136. // Voltage averaging configuration
  137. bool takingAverage = false;  // Enable/disable voltage averaging
  138. int sampleReadV = 10;               // Number of samples for averaging
  139. unsigned int millisdelayReadV = 3;  // Delay between samples [ms]
  140. int counterVo = 0;
  141. unsigned long readVMillis;
  142. unsigned long prevReadVMillis = 0;
  143. unsigned long readingsV = 0;
  144.  
  145. // Program selection (1, 2, or 3)
  146. int program;
  147.  
  148. // Parameter storage array
  149. // Layout: [Program 1: SetV, DT, HyS, StV] | [Program 2: SetV, DT, HyS, StV] | [Program 3: SetV, DT, HyS, StV]
  150. int Param[4] = { SetV, DT, HyS, StV };
  151.  
  152. byte ParamItem = 4;  // Number of parameters per program
  153.  
  154. // THC control flags and variables
  155. boolean Do;              // Flag set by timer to trigger THC processing
  156. int encoderVal;          // Current encoder position/value
  157. byte menu = 0;           // Current menu state
  158. byte pos = 0;            // Menu item position
  159. byte show = 0;           // Display refresh counter
  160. unsigned int LCDtime = 0;// LCD timeout counter
  161.  
  162. void setup() {
  163.   // Configure ADC for arc voltage measurement
  164.   // Set ADC resolution to 12 bits (0-4095 range)
  165.   analogSetWidth(12);
  166.  
  167.   // Set the pin's voltage attenuation to 11dB for 0-3.3V input range
  168.   // NOTE: arcVoltPin (GPIO 34) is ADC1_CH6 on ESP32
  169.   analogSetPinAttenuation(arcVoltPin, ADC_11db);
  170.  
  171.   // Initialize all subsystems
  172.   Setup_Encoder();  // Initialize rotary encoder and button
  173.   Setup_LCD();      // Initialize LCD display
  174.   Setup_THC();      // Initialize THC output pins
  175.   Setup_Timer2();   // Initialize 10ms timer for THC control loop
  176.   Setup_WiFi();     // Initialize WiFi and web server for remote control
  177.  
  178.   // Load program configuration from EEPROM
  179.   ReadProg();
  180.  
  181.   // If EEPROM is uninitialized (program == 255), load defaults
  182.   if (program == 255) {
  183.     Default();      // Initialize EEPROM with default values
  184.     ReadProg();     // Read program selection again
  185.   }
  186.  
  187.   // Load parameters for selected program
  188.   switch (program) {
  189.     case 1:
  190.       ReadDataProg_1();
  191.       break;
  192.     case 2:
  193.       ReadDataProg_2();
  194.       break;
  195.     case 3:
  196.       ReadDataProg_3();
  197.       break;
  198.   }
  199.  
  200.   // Copy loaded parameters to working variables
  201.   SetV = Param[SetVa];
  202.   DT = Param[DTa];
  203.   HyS = Param[HySa];
  204.   StV = Param[StVa];
  205.  
  206.   // Initialize encoder position to current SetV
  207.   encoderVal = SetV;
  208.  
  209.   // Serial communication disabled by default
  210.   // Uncomment to enable serial debugging at 9600 baud
  211.   //Serial.begin(9600);
  212. }
  213.  
  214. void loop() {
  215.   // Read arc voltage from ADC
  216.   if (takingAverage) {
  217.     // Averaging mode: read multiple samples and compute mean
  218.     readVMillis = millis();
  219.     if (readVMillis - prevReadVMillis >= millisdelayReadV) {
  220.       prevReadVMillis = readVMillis;
  221.       counterVo++;
  222.      
  223.       // Read raw ADC value and convert to voltage
  224.       double rawAnalogValue = analogRead(arcVoltPin);
  225.       ArcV = (rawAnalogValue * double(espV_value / 4095.0)) * double(100.00 / divRatio);
  226.     }
  227.    
  228.     // When all samples collected, compute average
  229.     if (counterVo == sampleReadV) {
  230.       ArcV = readingsV / sampleReadV;
  231.       counterVo = 0;
  232.       readingsV = 0;
  233.     }
  234.  
  235.   } else {
  236.     // Single-sample mode: read voltage directly without averaging
  237.     double rawAnalogValue = analogRead(arcVoltPin);
  238.     ArcV = (rawAnalogValue * double(espV_value / 4095.0)) * double(100.00 / divRatio);
  239.   }
  240.  
  241.   // Process user input
  242.   checkButton();  // Detect button press
  243.   checkMenu();    // Update menu state based on button press
  244.  
  245.   // Execute THC control algorithm (triggered by timer every 10ms)
  246.   doTHC();
  247.  
  248.   // Update LCD display (triggered by timer)
  249.   doLCD();
  250.  
  251.   // Handle incoming web server requests (WiFi control interface)
  252.   webServer.handleClient();
  253.  
  254.   // Serial debug output (disabled by default)
  255.   //RS232();
  256. }
  257.  
  258. /* END CODE */
  259.  
Advertisement
Add Comment
Please, Sign In to add comment