pleasedontcode

# GPIO Control rev_02

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