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: # Smart Controller
- - Source Code NOT compiled for: ESP32 DevKit V1
- - Source Code created on: 2026-01-17 17:25:02
- ********* Pleasedontcode.com **********/
- /****** SYSTEM REQUIREMENTS *****/
- /****** SYSTEM REQUIREMENT 1 *****/
- /* buat progres THC_rotary bisa di kontrol dan di */
- /* monitor dengan web server real time */
- /****** END SYSTEM REQUIREMENTS *****/
- #include <EEPROM.h>
- #include "Wire.h"
- #include "_webserver.h"
- // ============================================================
- // GLOBAL VARIABLES AND CONSTANTS
- // ============================================================
- // Pin Definitions
- const int encoderPinA = 35; // Encoder pin A (interrupt)
- const int encoderPinB = 34; // Encoder pin B
- const int buttonPin = 32; // Button pin
- const int outputUpPin = 5; // Torch up output
- const int outputOkPin = 7; // Arc ok output
- const int outputDnPin = 6; // Torch down output
- const int arcVoltagePin = 36; // Arc voltage input (ADC)
- // EEPROM memory layout for storing THC parameters
- // Each program (1, 2, 3) has 4 parameters stored at different addresses
- // Program 1: addresses 0-3 (SetV, DT, HyS, StV)
- // Program 2: addresses 4-7 (SetV, DT, HyS, StV)
- // Program 3: addresses 8-11 (SetV, DT, HyS, StV)
- // Active program: address 12
- // THC Parameters
- unsigned int SetV = 100; // Set voltage target
- unsigned int DT = 5; // Delay time (x0.1s)
- unsigned int HyS = 80; // Hysteresis (x0.1V)
- unsigned int StV = 100; // Start voltage
- unsigned int ArcV = 0; // Current arc voltage
- unsigned int SetVa = 0; // Address for SetV in EEPROM
- unsigned int DTa = 1; // Address for DT in EEPROM
- unsigned int HySa = 2; // Address for HyS in EEPROM
- unsigned int StVa = 3; // Address for StV in EEPROM
- // Program and Menu Variables
- int program = 1; // Current program (1-3)
- int menu = 0; // Current menu state
- int pos = 0; // Menu position
- volatile int encoderVal = 0; // Encoder value
- int oldValue = -1; // Previous encoder value for change detection
- unsigned int LCDtime = 0; // LCD timeout counter
- unsigned int show = 0; // LCD refresh counter
- unsigned int defaultLCDtime = 100; // LCD timeout in timer ticks (100 * 10ms = 1s)
- // Button and Encoder Variables
- volatile boolean A, B, lastA; // Encoder state variables
- volatile boolean ButtonOk = false; // Button pressed flag
- volatile boolean ButtonStat = false; // Current button state
- volatile boolean lastButtonStat = false; // Previous button state
- // Timer and System Variables
- volatile boolean Do = false; // Timer flag
- esp_timer_handle_t timer_handle; // ESP32 timer handle
- const int ParamItem = 4; // Number of parameters per program
- unsigned int Param[4]; // Parameter array
- // ============================================================
- // SETUP FUNCTION
- // ============================================================
- void setup() {
- // Initialize serial communication
- Serial.begin(115200);
- delay(500);
- Serial.println("\n\n[SYSTEM] THC Rotary Controller Initializing...");
- // Initialize EEPROM
- EEPROM.begin(512);
- delay(100);
- // Read program from EEPROM
- ReadProg();
- Serial.print("[EEPROM] Current Program: ");
- Serial.println(program);
- // Load parameters for current program
- if (program == 1) {
- ReadDataProg_1();
- } else if (program == 2) {
- ReadDataProg_2();
- } else {
- ReadDataProg_3();
- }
- // Set global parameters from loaded program
- SetV = Param[0];
- DT = Param[1];
- HyS = Param[2];
- StV = Param[3];
- Serial.print("[EEPROM] Parameters Loaded - SetV: ");
- Serial.print(SetV);
- Serial.print("V, DT: ");
- Serial.print(DT);
- Serial.print(", HyS: ");
- Serial.print(HyS);
- Serial.print(", StV: ");
- Serial.println(StV);
- // Setup LCD display
- Setup_LCD();
- Serial.println("[LCD] LCD initialized");
- // Setup encoder and button
- Setup_Encoder();
- Serial.println("[ENCODER] Encoder and button initialized");
- // Setup THC control pins
- Setup_THC();
- Serial.println("[THC] THC outputs initialized");
- // Setup timer
- Setup_Timer2();
- Serial.println("[TIMER] Timer initialized");
- // Setup WiFi Access Point
- setupWiFi();
- Serial.println("[WiFi] WiFi Access Point initialized");
- // Start web server
- startWebServer();
- Serial.println("[WebServer] Web server started");
- // Initialize encoder value to set voltage
- encoderVal = SetV;
- oldValue = SetV;
- Serial.println("[SYSTEM] Initialization Complete");
- Serial.println("[SYSTEM] Access web interface at http://192.168.4.1");
- Serial.println("[SYSTEM] WiFi SSID: " + String(WIFI_SSID));
- Serial.println("[SYSTEM] WiFi Password: " + String(WIFI_PASSWORD));
- }
- // ============================================================
- // MAIN LOOP
- // ============================================================
- void loop() {
- // Check button state changes
- checkButton();
- // Process button press events
- checkMenu();
- // Update LCD display
- doLCD();
- // Execute THC control logic
- doTHC();
- // Read arc voltage analog input
- ArcV = analogRead(arcVoltagePin);
- // Handle web server requests
- updateWebServerStatus();
- // Small delay to prevent watchdog timeout
- delay(1);
- }
- // ============================================================
- // EEPROM FUNCTIONS
- // ============================================================
- // Initialize EEPROM with default values for all three programs
- void Default() {
- Serial.println("[EEPROM] Loading default parameters...");
- // Program 1 default parameters
- SetVa = 0;
- DTa = 1;
- HySa = 2;
- StVa = 3;
- SetV = 100; // Set voltage: 100V
- DT = 5; // Delay time: 0.5s
- HyS = 80; // Hysteresis: 8.0V
- StV = 100; // Start voltage: 100V
- EEPROM.write(0, SetV);
- EEPROM.write(1, DT);
- EEPROM.write(2, HyS);
- EEPROM.write(3, StV);
- // Program 2 default parameters (same as Program 1)
- EEPROM.write(4, SetV);
- EEPROM.write(5, DT);
- EEPROM.write(6, HyS);
- EEPROM.write(7, StV);
- // Program 3 default parameters (same as Program 1)
- EEPROM.write(8, SetV);
- EEPROM.write(9, DT);
- EEPROM.write(10, HyS);
- EEPROM.write(11, StV);
- // Set Program 1 as active program at startup
- EEPROM.write(12, 1);
- // Commit changes to physical EEPROM (ESP32 specific)
- EEPROM.commit();
- Serial.println("[EEPROM] Default parameters loaded to all programs");
- }
- // Read the currently active program number from EEPROM
- void ReadProg() {
- // Initialize EEPROM access for ESP32 (14 bytes needed)
- EEPROM.begin(14);
- // Read the currently active program number from address 12
- program = EEPROM.read(12);
- // Validate program number
- if (program < 1 || program > 3) {
- program = 1;
- }
- }
- // Load parameters for Program 1 from EEPROM addresses 0-3
- void ReadDataProg_1() {
- // Param Address 0, 1, 2, 3
- for (int j = 0; j < ParamItem; j++) {
- Param[j] = EEPROM.read(j);
- }
- Serial.println("[EEPROM] Program 1 parameters read");
- }
- // Load parameters for Program 2 from EEPROM addresses 4-7
- void ReadDataProg_2() {
- // Param Address 4, 5, 6, 7
- for (int j = 0; j < ParamItem; j++) {
- Param[j] = EEPROM.read(j + 4);
- }
- Serial.println("[EEPROM] Program 2 parameters read");
- }
- // Load parameters for Program 3 from EEPROM addresses 8-11
- void ReadDataProg_3() {
- // Param Address 8, 9, 10, 11
- for (int j = 0; j < ParamItem; j++) {
- Param[j] = EEPROM.read(j + 8);
- }
- Serial.println("[EEPROM] Program 3 parameters read");
- }
- // Write a single byte value to EEPROM at specified address
- void SaveData(int add, int value) {
- // Write a single byte value to EEPROM at specified address
- EEPROM.write(add, value);
- // Commit changes to physical EEPROM (ESP32 specific)
- // This ensures data is permanently stored
- EEPROM.commit();
- Serial.print("[EEPROM] Data saved at address ");
- Serial.print(add);
- Serial.print(": ");
- Serial.println(value);
- }
- /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment