Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /********* Pleasedontcode.com **********
- Pleasedontcode thanks you for automatic code generation! Enjoy your code!
- - Terms and Conditions:
- You have a non-exclusive, revocable, worldwide, royalty-free license
- for personal and commercial use. Attribution is optional; modifications
- are allowed, but you're responsible for code maintenance. We're not
- liable for any loss or damage. For full terms,
- please visit pleasedontcode.com/termsandconditions.
- - Project: # Plasma Controller
- - Source Code NOT compiled for: ESP32 DevKit V1
- - Source Code created on: 2026-01-17 16:36:43
- ********* Pleasedontcode.com **********/
- /****** SYSTEM REQUIREMENTS *****/
- /****** SYSTEM REQUIREMENT 1 *****/
- /* buat progres THC_rotary bisa di kontrol dengan */
- /* web server melalui wifi */
- /****** END SYSTEM REQUIREMENTS *****/
- /* START CODE */
- /****** DEFINITION OF LIBRARIES *****/
- #include <esp_timer.h> // ESP32 Timer library
- /****** FUNCTION PROTOTYPES *****/
- void setup(void);
- void loop(void);
- void Setup_Encoder(void);
- void Setup_LCD(void);
- void Setup_THC(void);
- void Setup_Timer2(void);
- void ReadProg(void);
- void Default(void);
- void ReadDataProg_1(void);
- void ReadDataProg_2(void);
- void ReadDataProg_3(void);
- void checkButton(void);
- void checkMenu(void);
- void doTHC(void);
- void doLCD(void);
- void Setup_WiFi(void);
- /*
- Last update 24.08.2025
- Project: THC for Plasma CNC controller
- Platform: Arduino UNO R3 / ESP32
- Created: April 2015
- Version: 02.00
- By: Pham Duy Anh - [email protected]
- Update and changes
- By: Mehmet İbrahim - [email protected]
- Youtube: https://www.youtube.com/c/MehmetIbrahim
- Adaptation to esp32 24.08.2025 Mehmet İbrahim
- System Requirements:
- Create web server interface for THC control via WiFi (buat progres THC_rotary bisa di kontrol dengan web server melalui wifi)
- Require:
- -> input
- <- output
- -> serial Tx
- -> serial Rx
- -> reset
- -> rotary encoder digital - interrupt
- -> rotary encoder digital - interrupt
- -> rotary button digital
- -> plasma Torch on digital
- -> plasma Arc Ok analog or digital
- -> plasma arc voltage analog 0-5V
- <- Arc Ok optocoupler
- <- torch Up optocoupler
- <- torch Down optocoupler
- <- LCD
- <- LCD
- <- LCD
- <- LCD
- <- LCD
- <- LCD
- */
- // GPIO Pin Definitions for ESP32 DevKit V1
- #define encoderPinA 13 // Rotary encoder pin A (INT0)
- #define encoderPinB 16 // Rotary encoder pin B (INT1)
- #define buttonPin 17 // Rotary encoder button pin
- #define outputOkPin 23 // Arc Ok output (HIGH when arc detected)
- #define outputUpPin 25 // Torch Up output (HIGH to move torch up)
- #define outputDnPin 26 // Torch Down output (HIGH to move torch down)
- #define arcVoltPin 34 // Arc voltage input (ADC1_CH6, 0-3.3V input range)
- #define defaultLCDtime 500 // Default LCD display time (s * 100 = 5 seconds)
- // ESP32 Timer configuration
- #define PWM_CHANNEL 0
- esp_timer_handle_t timer_handle;
- double espV_value = 3.3; // ESP32 reference voltage (3.3V)
- /*
- THC Parameter Definitions:
- DT - Delay Time: Delay before THC activation after arc detection (0.1~19.9s)
- HyS - Hysteresis: Control band voltage range for Up/Down signals (±1V ~ ±15V)
- StV - Start Voltage: Arc detection threshold (50~300V at input, after divider)
- SetV - Set Voltage: Target arc voltage for THC feedback control (0-250V equivalent)
- */
- // Parameter array indices
- int SetVa = 0, DTa = 1, HySa = 2, StVa = 3;
- // Working parameter variables
- int SetV, DT, HyS, StV;
- // Voltage divider ratio - calibrate based on your voltage divider circuit
- // Lower value increases displayed voltage, higher value decreases it
- // Typical range: 39-50 depending on resistor tolerance
- // Formula: ArcV = (rawADC / 4095) * 3.3 * (100 / divRatio)
- int divRatio = 39;
- unsigned long ArcV; // Current arc voltage (in units of 0.1V)
- int oldValue; // Previous parameter value (for change detection)
- // Voltage averaging configuration
- bool takingAverage = false; // Enable/disable voltage averaging
- int sampleReadV = 10; // Number of samples for averaging
- unsigned int millisdelayReadV = 3; // Delay between samples [ms]
- int counterVo = 0;
- unsigned long readVMillis;
- unsigned long prevReadVMillis = 0;
- unsigned long readingsV = 0;
- // Program selection (1, 2, or 3)
- int program;
- // Parameter storage array
- // Layout: [Program 1: SetV, DT, HyS, StV] | [Program 2: SetV, DT, HyS, StV] | [Program 3: SetV, DT, HyS, StV]
- int Param[4] = { SetV, DT, HyS, StV };
- byte ParamItem = 4; // Number of parameters per program
- // THC control flags and variables
- boolean Do; // Flag set by timer to trigger THC processing
- int encoderVal; // Current encoder position/value
- byte menu = 0; // Current menu state
- byte pos = 0; // Menu item position
- byte show = 0; // Display refresh counter
- unsigned int LCDtime = 0;// LCD timeout counter
- void setup() {
- // Configure ADC for arc voltage measurement
- // Set ADC resolution to 12 bits (0-4095 range)
- analogSetWidth(12);
- // Set the pin's voltage attenuation to 11dB for 0-3.3V input range
- // NOTE: arcVoltPin (GPIO 34) is ADC1_CH6 on ESP32
- analogSetPinAttenuation(arcVoltPin, ADC_11db);
- // Initialize all subsystems
- Setup_Encoder(); // Initialize rotary encoder and button
- Setup_LCD(); // Initialize LCD display
- Setup_THC(); // Initialize THC output pins
- Setup_Timer2(); // Initialize 10ms timer for THC control loop
- Setup_WiFi(); // Initialize WiFi and web server for remote control
- // Load program configuration from EEPROM
- ReadProg();
- // If EEPROM is uninitialized (program == 255), load defaults
- if (program == 255) {
- Default(); // Initialize EEPROM with default values
- ReadProg(); // Read program selection again
- }
- // Load parameters for selected program
- switch (program) {
- case 1:
- ReadDataProg_1();
- break;
- case 2:
- ReadDataProg_2();
- break;
- case 3:
- ReadDataProg_3();
- break;
- }
- // Copy loaded parameters to working variables
- SetV = Param[SetVa];
- DT = Param[DTa];
- HyS = Param[HySa];
- StV = Param[StVa];
- // Initialize encoder position to current SetV
- encoderVal = SetV;
- // Serial communication disabled by default
- // Uncomment to enable serial debugging at 9600 baud
- //Serial.begin(9600);
- }
- void loop() {
- // Read arc voltage from ADC
- if (takingAverage) {
- // Averaging mode: read multiple samples and compute mean
- readVMillis = millis();
- if (readVMillis - prevReadVMillis >= millisdelayReadV) {
- prevReadVMillis = readVMillis;
- counterVo++;
- // Read raw ADC value and convert to voltage
- double rawAnalogValue = analogRead(arcVoltPin);
- ArcV = (rawAnalogValue * double(espV_value / 4095.0)) * double(100.00 / divRatio);
- }
- // When all samples collected, compute average
- if (counterVo == sampleReadV) {
- ArcV = readingsV / sampleReadV;
- counterVo = 0;
- readingsV = 0;
- }
- } else {
- // Single-sample mode: read voltage directly without averaging
- double rawAnalogValue = analogRead(arcVoltPin);
- ArcV = (rawAnalogValue * double(espV_value / 4095.0)) * double(100.00 / divRatio);
- }
- // Process user input
- checkButton(); // Detect button press
- checkMenu(); // Update menu state based on button press
- // Execute THC control algorithm (triggered by timer every 10ms)
- doTHC();
- // Update LCD display (triggered by timer)
- doLCD();
- // Handle incoming web server requests (WiFi control interface)
- webServer.handleClient();
- // Serial debug output (disabled by default)
- //RS232();
- }
- /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment