pleasedontcode

# Smart Controller rev_06

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: # Smart Controller
  13.     - Source Code NOT compiled for: ESP32 DevKit V1
  14.     - Source Code created on: 2026-01-17 17:25:02
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* buat progres THC_rotary bisa di kontrol  dan di */
  21.     /* monitor dengan web server real time */
  22. /****** END SYSTEM REQUIREMENTS *****/
  23.  
  24.  
  25. #include <EEPROM.h>
  26. #include "Wire.h"
  27. #include "_webserver.h"
  28.  
  29. // ============================================================
  30. // GLOBAL VARIABLES AND CONSTANTS
  31. // ============================================================
  32.  
  33. // Pin Definitions
  34. const int encoderPinA = 35;      // Encoder pin A (interrupt)
  35. const int encoderPinB = 34;      // Encoder pin B
  36. const int buttonPin = 32;         // Button pin
  37. const int outputUpPin = 5;        // Torch up output
  38. const int outputOkPin = 7;        // Arc ok output
  39. const int outputDnPin = 6;        // Torch down output
  40. const int arcVoltagePin = 36;     // Arc voltage input (ADC)
  41.  
  42. // EEPROM memory layout for storing THC parameters
  43. // Each program (1, 2, 3) has 4 parameters stored at different addresses
  44. // Program 1: addresses 0-3    (SetV, DT, HyS, StV)
  45. // Program 2: addresses 4-7    (SetV, DT, HyS, StV)
  46. // Program 3: addresses 8-11   (SetV, DT, HyS, StV)
  47. // Active program: address 12
  48.  
  49. // THC Parameters
  50. unsigned int SetV = 100;         // Set voltage target
  51. unsigned int DT = 5;             // Delay time (x0.1s)
  52. unsigned int HyS = 80;           // Hysteresis (x0.1V)
  53. unsigned int StV = 100;          // Start voltage
  54. unsigned int ArcV = 0;           // Current arc voltage
  55. unsigned int SetVa = 0;          // Address for SetV in EEPROM
  56. unsigned int DTa = 1;            // Address for DT in EEPROM
  57. unsigned int HySa = 2;           // Address for HyS in EEPROM
  58. unsigned int StVa = 3;           // Address for StV in EEPROM
  59.  
  60. // Program and Menu Variables
  61. int program = 1;                 // Current program (1-3)
  62. int menu = 0;                    // Current menu state
  63. int pos = 0;                     // Menu position
  64. volatile int encoderVal = 0;     // Encoder value
  65. int oldValue = -1;               // Previous encoder value for change detection
  66. unsigned int LCDtime = 0;        // LCD timeout counter
  67. unsigned int show = 0;           // LCD refresh counter
  68. unsigned int defaultLCDtime = 100; // LCD timeout in timer ticks (100 * 10ms = 1s)
  69.  
  70. // Button and Encoder Variables
  71. volatile boolean A, B, lastA;    // Encoder state variables
  72. volatile boolean ButtonOk = false; // Button pressed flag
  73. volatile boolean ButtonStat = false; // Current button state
  74. volatile boolean lastButtonStat = false; // Previous button state
  75.  
  76. // Timer and System Variables
  77. volatile boolean Do = false;     // Timer flag
  78. esp_timer_handle_t timer_handle; // ESP32 timer handle
  79. const int ParamItem = 4;         // Number of parameters per program
  80. unsigned int Param[4];           // Parameter array
  81.  
  82. // ============================================================
  83. // SETUP FUNCTION
  84. // ============================================================
  85. void setup() {
  86.   // Initialize serial communication
  87.   Serial.begin(115200);
  88.   delay(500);
  89.   Serial.println("\n\n[SYSTEM] THC Rotary Controller Initializing...");
  90.  
  91.   // Initialize EEPROM
  92.   EEPROM.begin(512);
  93.   delay(100);
  94.  
  95.   // Read program from EEPROM
  96.   ReadProg();
  97.   Serial.print("[EEPROM] Current Program: ");
  98.   Serial.println(program);
  99.  
  100.   // Load parameters for current program
  101.   if (program == 1) {
  102.     ReadDataProg_1();
  103.   } else if (program == 2) {
  104.     ReadDataProg_2();
  105.   } else {
  106.     ReadDataProg_3();
  107.   }
  108.  
  109.   // Set global parameters from loaded program
  110.   SetV = Param[0];
  111.   DT = Param[1];
  112.   HyS = Param[2];
  113.   StV = Param[3];
  114.  
  115.   Serial.print("[EEPROM] Parameters Loaded - SetV: ");
  116.   Serial.print(SetV);
  117.   Serial.print("V, DT: ");
  118.   Serial.print(DT);
  119.   Serial.print(", HyS: ");
  120.   Serial.print(HyS);
  121.   Serial.print(", StV: ");
  122.   Serial.println(StV);
  123.  
  124.   // Setup LCD display
  125.   Setup_LCD();
  126.   Serial.println("[LCD] LCD initialized");
  127.  
  128.   // Setup encoder and button
  129.   Setup_Encoder();
  130.   Serial.println("[ENCODER] Encoder and button initialized");
  131.  
  132.   // Setup THC control pins
  133.   Setup_THC();
  134.   Serial.println("[THC] THC outputs initialized");
  135.  
  136.   // Setup timer
  137.   Setup_Timer2();
  138.   Serial.println("[TIMER] Timer initialized");
  139.  
  140.   // Setup WiFi Access Point
  141.   setupWiFi();
  142.   Serial.println("[WiFi] WiFi Access Point initialized");
  143.  
  144.   // Start web server
  145.   startWebServer();
  146.   Serial.println("[WebServer] Web server started");
  147.  
  148.   // Initialize encoder value to set voltage
  149.   encoderVal = SetV;
  150.   oldValue = SetV;
  151.  
  152.   Serial.println("[SYSTEM] Initialization Complete");
  153.   Serial.println("[SYSTEM] Access web interface at http://192.168.4.1");
  154.   Serial.println("[SYSTEM] WiFi SSID: " + String(WIFI_SSID));
  155.   Serial.println("[SYSTEM] WiFi Password: " + String(WIFI_PASSWORD));
  156. }
  157.  
  158. // ============================================================
  159. // MAIN LOOP
  160. // ============================================================
  161. void loop() {
  162.   // Check button state changes
  163.   checkButton();
  164.  
  165.   // Process button press events
  166.   checkMenu();
  167.  
  168.   // Update LCD display
  169.   doLCD();
  170.  
  171.   // Execute THC control logic
  172.   doTHC();
  173.  
  174.   // Read arc voltage analog input
  175.   ArcV = analogRead(arcVoltagePin);
  176.  
  177.   // Handle web server requests
  178.   updateWebServerStatus();
  179.  
  180.   // Small delay to prevent watchdog timeout
  181.   delay(1);
  182. }
  183.  
  184. // ============================================================
  185. // EEPROM FUNCTIONS
  186. // ============================================================
  187.  
  188. // Initialize EEPROM with default values for all three programs
  189. void Default() {
  190.   Serial.println("[EEPROM] Loading default parameters...");
  191.  
  192.   // Program 1 default parameters
  193.   SetVa = 0;
  194.   DTa = 1;
  195.   HySa = 2;
  196.   StVa = 3;
  197.  
  198.   SetV = 100;  // Set voltage: 100V
  199.   DT = 5;      // Delay time: 0.5s
  200.   HyS = 80;    // Hysteresis: 8.0V
  201.   StV = 100;   // Start voltage: 100V
  202.  
  203.   EEPROM.write(0, SetV);
  204.   EEPROM.write(1, DT);
  205.   EEPROM.write(2, HyS);
  206.   EEPROM.write(3, StV);
  207.  
  208.   // Program 2 default parameters (same as Program 1)
  209.   EEPROM.write(4, SetV);
  210.   EEPROM.write(5, DT);
  211.   EEPROM.write(6, HyS);
  212.   EEPROM.write(7, StV);
  213.  
  214.   // Program 3 default parameters (same as Program 1)
  215.   EEPROM.write(8, SetV);
  216.   EEPROM.write(9, DT);
  217.   EEPROM.write(10, HyS);
  218.   EEPROM.write(11, StV);
  219.  
  220.   // Set Program 1 as active program at startup
  221.   EEPROM.write(12, 1);
  222.  
  223.   // Commit changes to physical EEPROM (ESP32 specific)
  224.   EEPROM.commit();
  225.   Serial.println("[EEPROM] Default parameters loaded to all programs");
  226. }
  227.  
  228. // Read the currently active program number from EEPROM
  229. void ReadProg() {
  230.   // Initialize EEPROM access for ESP32 (14 bytes needed)
  231.   EEPROM.begin(14);
  232.  
  233.   // Read the currently active program number from address 12
  234.   program = EEPROM.read(12);
  235.  
  236.   // Validate program number
  237.   if (program < 1 || program > 3) {
  238.     program = 1;
  239.   }
  240. }
  241.  
  242. // Load parameters for Program 1 from EEPROM addresses 0-3
  243. void ReadDataProg_1() {
  244.   // Param Address   0,   1,   2,   3
  245.   for (int j = 0; j < ParamItem; j++) {
  246.     Param[j] = EEPROM.read(j);
  247.   }
  248.   Serial.println("[EEPROM] Program 1 parameters read");
  249. }
  250.  
  251. // Load parameters for Program 2 from EEPROM addresses 4-7
  252. void ReadDataProg_2() {
  253.   // Param Address   4,   5,   6,   7
  254.   for (int j = 0; j < ParamItem; j++) {
  255.     Param[j] = EEPROM.read(j + 4);
  256.   }
  257.   Serial.println("[EEPROM] Program 2 parameters read");
  258. }
  259.  
  260. // Load parameters for Program 3 from EEPROM addresses 8-11
  261. void ReadDataProg_3() {
  262.   // Param Address   8,   9,   10,  11
  263.   for (int j = 0; j < ParamItem; j++) {
  264.     Param[j] = EEPROM.read(j + 8);
  265.   }
  266.   Serial.println("[EEPROM] Program 3 parameters read");
  267. }
  268.  
  269. // Write a single byte value to EEPROM at specified address
  270. void SaveData(int add, int value) {
  271.   // Write a single byte value to EEPROM at specified address
  272.   EEPROM.write(add, value);
  273.  
  274.   // Commit changes to physical EEPROM (ESP32 specific)
  275.   // This ensures data is permanently stored
  276.   EEPROM.commit();
  277.   Serial.print("[EEPROM] Data saved at address ");
  278.   Serial.print(add);
  279.   Serial.print(": ");
  280.   Serial.println(value);
  281. }
  282.  
  283. /* END CODE */
  284.  
Advertisement
Add Comment
Please, Sign In to add comment