pleasedontcode

# GPIO Controller rev_01

Jan 20th, 2026
18
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 11.37 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: # GPIO Controller
  13.     - Source Code NOT compiled for: ESP32 DevKit V1
  14.     - Source Code created on: 2026-01-20 16:19:17
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* **  Improvement 1: Initialize ESP32 DevKit V1 GPIO */
  21.     /* pins with appropriate digital input/output */
  22.     /* configurations and implement a main loop to */
  23.     /* monitor and control connected peripheral devices */
  24.     /* with proper timing intervals.  ** */
  25. /****** END SYSTEM REQUIREMENTS *****/
  26.  
  27.  
  28. /* START CODE */
  29.  
  30. /****** DEFINITION OF LIBRARIES *****/
  31.  
  32. /****** FUNCTION PROTOTYPES *****/
  33. void setup(void);
  34. void loop(void);
  35. void initializeGPIO(void);
  36. void configureDigitalIO(void);
  37. void monitorPeripherals(void);
  38. void controlPeripherals(void);
  39. void checkTimingIntervals(void);
  40.  
  41. /* ===== GPIO PIN CONFIGURATION DEFINITIONS ===== */
  42. /* GPIO pins available on ESP32 DevKit V1 for digital I/O */
  43. #define GPIO_OUTPUT_PIN_1 2    // GPIO 2 - Digital Output Pin 1
  44. #define GPIO_OUTPUT_PIN_2 4    // GPIO 4 - Digital Output Pin 2
  45. #define GPIO_OUTPUT_PIN_3 5    // GPIO 5 - Digital Output Pin 3
  46. #define GPIO_INPUT_PIN_1 12    // GPIO 12 - Digital Input Pin 1
  47. #define GPIO_INPUT_PIN_2 13    // GPIO 13 - Digital Input Pin 2
  48. #define GPIO_INPUT_PIN_3 14    // GPIO 14 - Digital Input Pin 3
  49.  
  50. /* ===== TIMING INTERVAL DEFINITIONS (in milliseconds) ===== */
  51. #define LOOP_TIMING_INTERVAL 100          // Main loop monitoring interval: 100ms
  52. #define PERIPHERAL_CHECK_INTERVAL 200     // Peripheral status check interval: 200ms
  53. #define OUTPUT_TOGGLE_INTERVAL 500        // Output control interval: 500ms
  54. #define INPUT_DEBOUNCE_INTERVAL 50        // Input debounce interval: 50ms
  55.  
  56. /* ===== TIMING STATE VARIABLES ===== */
  57. unsigned long lastPeripheralCheckTime = 0;   // Timestamp of last peripheral check
  58. unsigned long lastOutputToggleTime = 0;      // Timestamp of last output toggle
  59. unsigned long lastInputReadTime = 0;         // Timestamp of last input read
  60. unsigned long currentTime = 0;               // Current system time in milliseconds
  61.  
  62. /* ===== PERIPHERAL STATE VARIABLES ===== */
  63. volatile int digitalInputState1 = LOW;       // State of Digital Input Pin 1
  64. volatile int digitalInputState2 = LOW;       // State of Digital Input Pin 2
  65. volatile int digitalInputState3 = LOW;       // State of Digital Input Pin 3
  66. int previousInputState1 = LOW;               // Previous state of Input Pin 1 for change detection
  67. int previousInputState2 = LOW;               // Previous state of Input Pin 2 for change detection
  68. int previousInputState3 = LOW;               // Previous state of Input Pin 3 for change detection
  69. int digitalOutputState1 = LOW;               // State of Digital Output Pin 1
  70. int digitalOutputState2 = LOW;               // State of Digital Output Pin 2
  71. int digitalOutputState3 = LOW;               // State of Digital Output Pin 3
  72.  
  73. /****** SETUP FUNCTION *****/
  74. void setup(void)
  75. {
  76.     /* Initialize serial communication for debugging at 115200 baud rate */
  77.     Serial.begin(115200);
  78.    
  79.     /* Add a small delay to ensure serial monitor is ready */
  80.     delay(500);
  81.    
  82.     /* Print startup message to serial monitor */
  83.     Serial.println("\n\n========================================");
  84.     Serial.println("ESP32 DevKit V1 GPIO Control System");
  85.     Serial.println("Initializing GPIO Pins and Configurations...");
  86.     Serial.println("========================================\n");
  87.    
  88.     /* Call GPIO initialization function */
  89.     initializeGPIO();
  90.    
  91.     /* Call digital I/O configuration function */
  92.     configureDigitalIO();
  93.    
  94.     /* Print initialization complete message */
  95.     Serial.println("Setup complete! Main loop monitoring started.\n");
  96. }
  97.  
  98. /****** MAIN LOOP FUNCTION *****/
  99. void loop(void)
  100. {
  101.     /* Get current system time in milliseconds */
  102.     currentTime = millis();
  103.    
  104.     /* Check and execute timing intervals for monitoring and control */
  105.     checkTimingIntervals();
  106.    
  107.     /* Small delay to prevent watchdog timer issues */
  108.     delay(10);
  109. }
  110.  
  111. /****** GPIO INITIALIZATION FUNCTION *****/
  112. void initializeGPIO(void)
  113. {
  114.     /* Serial debug output for GPIO initialization start */
  115.     Serial.println("Initializing GPIO Pins:");
  116.    
  117.     /* Configure digital output pins */
  118.     pinMode(GPIO_OUTPUT_PIN_1, OUTPUT);
  119.     Serial.print("  GPIO Pin ");
  120.     Serial.print(GPIO_OUTPUT_PIN_1);
  121.     Serial.println(" configured as Digital OUTPUT");
  122.    
  123.     pinMode(GPIO_OUTPUT_PIN_2, OUTPUT);
  124.     Serial.print("  GPIO Pin ");
  125.     Serial.print(GPIO_OUTPUT_PIN_2);
  126.     Serial.println(" configured as Digital OUTPUT");
  127.    
  128.     pinMode(GPIO_OUTPUT_PIN_3, OUTPUT);
  129.     Serial.print("  GPIO Pin ");
  130.     Serial.print(GPIO_OUTPUT_PIN_3);
  131.     Serial.println(" configured as Digital OUTPUT");
  132.    
  133.     /* Configure digital input pins */
  134.     pinMode(GPIO_INPUT_PIN_1, INPUT);
  135.     Serial.print("  GPIO Pin ");
  136.     Serial.print(GPIO_INPUT_PIN_1);
  137.     Serial.println(" configured as Digital INPUT");
  138.    
  139.     pinMode(GPIO_INPUT_PIN_2, INPUT);
  140.     Serial.print("  GPIO Pin ");
  141.     Serial.print(GPIO_INPUT_PIN_2);
  142.     Serial.println(" configured as Digital INPUT");
  143.    
  144.     pinMode(GPIO_INPUT_PIN_3, INPUT);
  145.     Serial.print("  GPIO Pin ");
  146.     Serial.print(GPIO_INPUT_PIN_3);
  147.     Serial.println(" configured as Digital INPUT");
  148.    
  149.     /* Initialize all output pins to LOW state */
  150.     digitalWrite(GPIO_OUTPUT_PIN_1, LOW);
  151.     digitalWrite(GPIO_OUTPUT_PIN_2, LOW);
  152.     digitalWrite(GPIO_OUTPUT_PIN_3, LOW);
  153.    
  154.     Serial.println("All GPIO pins initialized successfully.\n");
  155. }
  156.  
  157. /****** DIGITAL I/O CONFIGURATION FUNCTION *****/
  158. void configureDigitalIO(void)
  159. {
  160.     /* Serial debug output for digital I/O configuration */
  161.     Serial.println("Configuring Digital I/O:");
  162.    
  163.     /* Configure output pin modes and initial states */
  164.     Serial.println("  Output Pins Configuration:");
  165.     Serial.print("    GPIO ");
  166.     Serial.print(GPIO_OUTPUT_PIN_1);
  167.     Serial.println(" - Output mode, initial state: LOW");
  168.    
  169.     Serial.print("    GPIO ");
  170.     Serial.print(GPIO_OUTPUT_PIN_2);
  171.     Serial.println(" - Output mode, initial state: LOW");
  172.    
  173.     Serial.print("    GPIO ");
  174.     Serial.print(GPIO_OUTPUT_PIN_3);
  175.     Serial.println(" - Output mode, initial state: LOW");
  176.    
  177.     /* Configure input pin modes and debounce settings */
  178.     Serial.println("  Input Pins Configuration:");
  179.     Serial.print("    GPIO ");
  180.     Serial.print(GPIO_INPUT_PIN_1);
  181.     Serial.print(" - Input mode, debounce interval: ");
  182.     Serial.print(INPUT_DEBOUNCE_INTERVAL);
  183.     Serial.println("ms");
  184.    
  185.     Serial.print("    GPIO ");
  186.     Serial.print(GPIO_INPUT_PIN_2);
  187.     Serial.print(" - Input mode, debounce interval: ");
  188.     Serial.print(INPUT_DEBOUNCE_INTERVAL);
  189.     Serial.println("ms");
  190.    
  191.     Serial.print("    GPIO ");
  192.     Serial.print(GPIO_INPUT_PIN_3);
  193.     Serial.print(" - Input mode, debounce interval: ");
  194.     Serial.print(INPUT_DEBOUNCE_INTERVAL);
  195.     Serial.println("ms");
  196.    
  197.     Serial.println("Digital I/O Configuration complete.\n");
  198.    
  199.     /* Display timing intervals for monitoring loop */
  200.     Serial.println("Timing Intervals Configuration:");
  201.     Serial.print("  Main loop monitoring interval: ");
  202.     Serial.print(LOOP_TIMING_INTERVAL);
  203.     Serial.println("ms");
  204.    
  205.     Serial.print("  Peripheral check interval: ");
  206.     Serial.print(PERIPHERAL_CHECK_INTERVAL);
  207.     Serial.println("ms");
  208.    
  209.     Serial.print("  Output control interval: ");
  210.     Serial.print(OUTPUT_TOGGLE_INTERVAL);
  211.     Serial.println("ms");
  212.    
  213.     Serial.print("  Input debounce interval: ");
  214.     Serial.print(INPUT_DEBOUNCE_INTERVAL);
  215.     Serial.println("ms\n");
  216. }
  217.  
  218. /****** TIMING INTERVALS CHECK FUNCTION *****/
  219. void checkTimingIntervals(void)
  220. {
  221.     /* Check if peripheral monitoring interval has elapsed */
  222.     if (currentTime - lastPeripheralCheckTime >= PERIPHERAL_CHECK_INTERVAL)
  223.     {
  224.         /* Update the last peripheral check timestamp */
  225.         lastPeripheralCheckTime = currentTime;
  226.        
  227.         /* Call function to monitor peripheral devices */
  228.         monitorPeripherals();
  229.     }
  230.    
  231.     /* Check if output control interval has elapsed */
  232.     if (currentTime - lastOutputToggleTime >= OUTPUT_TOGGLE_INTERVAL)
  233.     {
  234.         /* Update the last output toggle timestamp */
  235.         lastOutputToggleTime = currentTime;
  236.        
  237.         /* Call function to control peripheral devices */
  238.         controlPeripherals();
  239.     }
  240. }
  241.  
  242. /****** PERIPHERAL MONITORING FUNCTION *****/
  243. void monitorPeripherals(void)
  244. {
  245.     /* Check if input debounce interval has elapsed since last input read */
  246.     if (currentTime - lastInputReadTime >= INPUT_DEBOUNCE_INTERVAL)
  247.     {
  248.         /* Update the last input read timestamp */
  249.         lastInputReadTime = currentTime;
  250.        
  251.         /* Read current state of digital input pin 1 */
  252.         digitalInputState1 = digitalRead(GPIO_INPUT_PIN_1);
  253.        
  254.         /* Check if input state has changed from previous read */
  255.         if (digitalInputState1 != previousInputState1)
  256.         {
  257.             /* Update previous state to current state */
  258.             previousInputState1 = digitalInputState1;
  259.            
  260.             /* Report input state change to serial monitor */
  261.             Serial.print("INPUT 1 (GPIO ");
  262.             Serial.print(GPIO_INPUT_PIN_1);
  263.             Serial.print(") State Changed to: ");
  264.             Serial.println(digitalInputState1 ? "HIGH" : "LOW");
  265.         }
  266.        
  267.         /* Read current state of digital input pin 2 */
  268.         digitalInputState2 = digitalRead(GPIO_INPUT_PIN_2);
  269.        
  270.         /* Check if input state has changed from previous read */
  271.         if (digitalInputState2 != previousInputState2)
  272.         {
  273.             /* Update previous state to current state */
  274.             previousInputState2 = digitalInputState2;
  275.            
  276.             /* Report input state change to serial monitor */
  277.             Serial.print("INPUT 2 (GPIO ");
  278.             Serial.print(GPIO_INPUT_PIN_2);
  279.             Serial.print(") State Changed to: ");
  280.             Serial.println(digitalInputState2 ? "HIGH" : "LOW");
  281.         }
  282.        
  283.         /* Read current state of digital input pin 3 */
  284.         digitalInputState3 = digitalRead(GPIO_INPUT_PIN_3);
  285.        
  286.         /* Check if input state has changed from previous read */
  287.         if (digitalInputState3 != previousInputState3)
  288.         {
  289.             /* Update previous state to current state */
  290.             previousInputState3 = digitalInputState3;
  291.            
  292.             /* Report input state change to serial monitor */
  293.             Serial.print("INPUT 3 (GPIO ");
  294.             Serial.print(GPIO_INPUT_PIN_3);
  295.             Serial.print(") State Changed to: ");
  296.             Serial.println(digitalInputState3 ? "HIGH" : "LOW");
  297.         }
  298.     }
  299. }
  300.  
  301. /****** PERIPHERAL CONTROL FUNCTION *****/
  302. void controlPeripherals(void)
  303. {
  304.     /* Toggle digital output pin 1 state */
  305.     digitalOutputState1 = (digitalOutputState1 == LOW) ? HIGH : LOW;
  306.     digitalWrite(GPIO_OUTPUT_PIN_1, digitalOutputState1);
  307.    
  308.     /* Report output state change to serial monitor */
  309.     Serial.print("OUTPUT 1 (GPIO ");
  310.     Serial.print(GPIO_OUTPUT_PIN_1);
  311.     Serial.print(") Set to: ");
  312.     Serial.println(digitalOutputState1 ? "HIGH" : "LOW");
  313.    
  314.     /* Toggle digital output pin 2 state */
  315.     digitalOutputState2 = (digitalOutputState2 == LOW) ? HIGH : LOW;
  316.     digitalWrite(GPIO_OUTPUT_PIN_2, digitalOutputState2);
  317.    
  318.     /* Report output state change to serial monitor */
  319.     Serial.print("OUTPUT 2 (GPIO ");
  320.     Serial.print(GPIO_OUTPUT_PIN_2);
  321.     Serial.print(") Set to: ");
  322.     Serial.println(digitalOutputState2 ? "HIGH" : "LOW");
  323.    
  324.     /* Toggle digital output pin 3 state */
  325.     digitalOutputState3 = (digitalOutputState3 == LOW) ? HIGH : LOW;
  326.     digitalWrite(GPIO_OUTPUT_PIN_3, digitalOutputState3);
  327.    
  328.     /* Report output state change to serial monitor */
  329.     Serial.print("OUTPUT 3 (GPIO ");
  330.     Serial.print(GPIO_OUTPUT_PIN_3);
  331.     Serial.print(") Set to: ");
  332.     Serial.println(digitalOutputState3 ? "HIGH" : "LOW");
  333.    
  334.     /* Print timing information for monitoring purposes */
  335.     Serial.print("Timestamp: ");
  336.     Serial.print(currentTime);
  337.     Serial.println("ms\n");
  338. }
  339.  
  340. /* END CODE */
  341.  
Advertisement
Add Comment
Please, Sign In to add comment