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: # GPIO Controller
- - Source Code NOT compiled for: ESP32 DevKit V1
- - Source Code created on: 2026-01-20 16:19:17
- ********* Pleasedontcode.com **********/
- /****** SYSTEM REQUIREMENTS *****/
- /****** SYSTEM REQUIREMENT 1 *****/
- /* ** Improvement 1: Initialize ESP32 DevKit V1 GPIO */
- /* pins with appropriate digital input/output */
- /* configurations and implement a main loop to */
- /* monitor and control connected peripheral devices */
- /* with proper timing intervals. ** */
- /****** END SYSTEM REQUIREMENTS *****/
- /* START CODE */
- /****** DEFINITION OF LIBRARIES *****/
- /****** FUNCTION PROTOTYPES *****/
- void setup(void);
- void loop(void);
- void initializeGPIO(void);
- void configureDigitalIO(void);
- void monitorPeripherals(void);
- void controlPeripherals(void);
- void checkTimingIntervals(void);
- /* ===== GPIO PIN CONFIGURATION DEFINITIONS ===== */
- /* GPIO pins available on ESP32 DevKit V1 for digital I/O */
- #define GPIO_OUTPUT_PIN_1 2 // GPIO 2 - Digital Output Pin 1
- #define GPIO_OUTPUT_PIN_2 4 // GPIO 4 - Digital Output Pin 2
- #define GPIO_OUTPUT_PIN_3 5 // GPIO 5 - Digital Output Pin 3
- #define GPIO_INPUT_PIN_1 12 // GPIO 12 - Digital Input Pin 1
- #define GPIO_INPUT_PIN_2 13 // GPIO 13 - Digital Input Pin 2
- #define GPIO_INPUT_PIN_3 14 // GPIO 14 - Digital Input Pin 3
- /* ===== TIMING INTERVAL DEFINITIONS (in milliseconds) ===== */
- #define LOOP_TIMING_INTERVAL 100 // Main loop monitoring interval: 100ms
- #define PERIPHERAL_CHECK_INTERVAL 200 // Peripheral status check interval: 200ms
- #define OUTPUT_TOGGLE_INTERVAL 500 // Output control interval: 500ms
- #define INPUT_DEBOUNCE_INTERVAL 50 // Input debounce interval: 50ms
- /* ===== TIMING STATE VARIABLES ===== */
- unsigned long lastPeripheralCheckTime = 0; // Timestamp of last peripheral check
- unsigned long lastOutputToggleTime = 0; // Timestamp of last output toggle
- unsigned long lastInputReadTime = 0; // Timestamp of last input read
- unsigned long currentTime = 0; // Current system time in milliseconds
- /* ===== PERIPHERAL STATE VARIABLES ===== */
- volatile int digitalInputState1 = LOW; // State of Digital Input Pin 1
- volatile int digitalInputState2 = LOW; // State of Digital Input Pin 2
- volatile int digitalInputState3 = LOW; // State of Digital Input Pin 3
- int previousInputState1 = LOW; // Previous state of Input Pin 1 for change detection
- int previousInputState2 = LOW; // Previous state of Input Pin 2 for change detection
- int previousInputState3 = LOW; // Previous state of Input Pin 3 for change detection
- int digitalOutputState1 = LOW; // State of Digital Output Pin 1
- int digitalOutputState2 = LOW; // State of Digital Output Pin 2
- int digitalOutputState3 = LOW; // State of Digital Output Pin 3
- /****** SETUP FUNCTION *****/
- void setup(void)
- {
- /* Initialize serial communication for debugging at 115200 baud rate */
- Serial.begin(115200);
- /* Add a small delay to ensure serial monitor is ready */
- delay(500);
- /* Print startup message to serial monitor */
- Serial.println("\n\n========================================");
- Serial.println("ESP32 DevKit V1 GPIO Control System");
- Serial.println("Initializing GPIO Pins and Configurations...");
- Serial.println("========================================\n");
- /* Call GPIO initialization function */
- initializeGPIO();
- /* Call digital I/O configuration function */
- configureDigitalIO();
- /* Print initialization complete message */
- Serial.println("Setup complete! Main loop monitoring started.\n");
- }
- /****** MAIN LOOP FUNCTION *****/
- void loop(void)
- {
- /* Get current system time in milliseconds */
- currentTime = millis();
- /* Check and execute timing intervals for monitoring and control */
- checkTimingIntervals();
- /* Small delay to prevent watchdog timer issues */
- delay(10);
- }
- /****** GPIO INITIALIZATION FUNCTION *****/
- void initializeGPIO(void)
- {
- /* Serial debug output for GPIO initialization start */
- Serial.println("Initializing GPIO Pins:");
- /* Configure digital output pins */
- pinMode(GPIO_OUTPUT_PIN_1, OUTPUT);
- Serial.print(" GPIO Pin ");
- Serial.print(GPIO_OUTPUT_PIN_1);
- Serial.println(" configured as Digital OUTPUT");
- pinMode(GPIO_OUTPUT_PIN_2, OUTPUT);
- Serial.print(" GPIO Pin ");
- Serial.print(GPIO_OUTPUT_PIN_2);
- Serial.println(" configured as Digital OUTPUT");
- pinMode(GPIO_OUTPUT_PIN_3, OUTPUT);
- Serial.print(" GPIO Pin ");
- Serial.print(GPIO_OUTPUT_PIN_3);
- Serial.println(" configured as Digital OUTPUT");
- /* Configure digital input pins */
- pinMode(GPIO_INPUT_PIN_1, INPUT);
- Serial.print(" GPIO Pin ");
- Serial.print(GPIO_INPUT_PIN_1);
- Serial.println(" configured as Digital INPUT");
- pinMode(GPIO_INPUT_PIN_2, INPUT);
- Serial.print(" GPIO Pin ");
- Serial.print(GPIO_INPUT_PIN_2);
- Serial.println(" configured as Digital INPUT");
- pinMode(GPIO_INPUT_PIN_3, INPUT);
- Serial.print(" GPIO Pin ");
- Serial.print(GPIO_INPUT_PIN_3);
- Serial.println(" configured as Digital INPUT");
- /* Initialize all output pins to LOW state */
- digitalWrite(GPIO_OUTPUT_PIN_1, LOW);
- digitalWrite(GPIO_OUTPUT_PIN_2, LOW);
- digitalWrite(GPIO_OUTPUT_PIN_3, LOW);
- Serial.println("All GPIO pins initialized successfully.\n");
- }
- /****** DIGITAL I/O CONFIGURATION FUNCTION *****/
- void configureDigitalIO(void)
- {
- /* Serial debug output for digital I/O configuration */
- Serial.println("Configuring Digital I/O:");
- /* Configure output pin modes and initial states */
- Serial.println(" Output Pins Configuration:");
- Serial.print(" GPIO ");
- Serial.print(GPIO_OUTPUT_PIN_1);
- Serial.println(" - Output mode, initial state: LOW");
- Serial.print(" GPIO ");
- Serial.print(GPIO_OUTPUT_PIN_2);
- Serial.println(" - Output mode, initial state: LOW");
- Serial.print(" GPIO ");
- Serial.print(GPIO_OUTPUT_PIN_3);
- Serial.println(" - Output mode, initial state: LOW");
- /* Configure input pin modes and debounce settings */
- Serial.println(" Input Pins Configuration:");
- Serial.print(" GPIO ");
- Serial.print(GPIO_INPUT_PIN_1);
- Serial.print(" - Input mode, debounce interval: ");
- Serial.print(INPUT_DEBOUNCE_INTERVAL);
- Serial.println("ms");
- Serial.print(" GPIO ");
- Serial.print(GPIO_INPUT_PIN_2);
- Serial.print(" - Input mode, debounce interval: ");
- Serial.print(INPUT_DEBOUNCE_INTERVAL);
- Serial.println("ms");
- Serial.print(" GPIO ");
- Serial.print(GPIO_INPUT_PIN_3);
- Serial.print(" - Input mode, debounce interval: ");
- Serial.print(INPUT_DEBOUNCE_INTERVAL);
- Serial.println("ms");
- Serial.println("Digital I/O Configuration complete.\n");
- /* Display timing intervals for monitoring loop */
- Serial.println("Timing Intervals Configuration:");
- Serial.print(" Main loop monitoring interval: ");
- Serial.print(LOOP_TIMING_INTERVAL);
- Serial.println("ms");
- Serial.print(" Peripheral check interval: ");
- Serial.print(PERIPHERAL_CHECK_INTERVAL);
- Serial.println("ms");
- Serial.print(" Output control interval: ");
- Serial.print(OUTPUT_TOGGLE_INTERVAL);
- Serial.println("ms");
- Serial.print(" Input debounce interval: ");
- Serial.print(INPUT_DEBOUNCE_INTERVAL);
- Serial.println("ms\n");
- }
- /****** TIMING INTERVALS CHECK FUNCTION *****/
- void checkTimingIntervals(void)
- {
- /* Check if peripheral monitoring interval has elapsed */
- if (currentTime - lastPeripheralCheckTime >= PERIPHERAL_CHECK_INTERVAL)
- {
- /* Update the last peripheral check timestamp */
- lastPeripheralCheckTime = currentTime;
- /* Call function to monitor peripheral devices */
- monitorPeripherals();
- }
- /* Check if output control interval has elapsed */
- if (currentTime - lastOutputToggleTime >= OUTPUT_TOGGLE_INTERVAL)
- {
- /* Update the last output toggle timestamp */
- lastOutputToggleTime = currentTime;
- /* Call function to control peripheral devices */
- controlPeripherals();
- }
- }
- /****** PERIPHERAL MONITORING FUNCTION *****/
- void monitorPeripherals(void)
- {
- /* Check if input debounce interval has elapsed since last input read */
- if (currentTime - lastInputReadTime >= INPUT_DEBOUNCE_INTERVAL)
- {
- /* Update the last input read timestamp */
- lastInputReadTime = currentTime;
- /* Read current state of digital input pin 1 */
- digitalInputState1 = digitalRead(GPIO_INPUT_PIN_1);
- /* Check if input state has changed from previous read */
- if (digitalInputState1 != previousInputState1)
- {
- /* Update previous state to current state */
- previousInputState1 = digitalInputState1;
- /* Report input state change to serial monitor */
- Serial.print("INPUT 1 (GPIO ");
- Serial.print(GPIO_INPUT_PIN_1);
- Serial.print(") State Changed to: ");
- Serial.println(digitalInputState1 ? "HIGH" : "LOW");
- }
- /* Read current state of digital input pin 2 */
- digitalInputState2 = digitalRead(GPIO_INPUT_PIN_2);
- /* Check if input state has changed from previous read */
- if (digitalInputState2 != previousInputState2)
- {
- /* Update previous state to current state */
- previousInputState2 = digitalInputState2;
- /* Report input state change to serial monitor */
- Serial.print("INPUT 2 (GPIO ");
- Serial.print(GPIO_INPUT_PIN_2);
- Serial.print(") State Changed to: ");
- Serial.println(digitalInputState2 ? "HIGH" : "LOW");
- }
- /* Read current state of digital input pin 3 */
- digitalInputState3 = digitalRead(GPIO_INPUT_PIN_3);
- /* Check if input state has changed from previous read */
- if (digitalInputState3 != previousInputState3)
- {
- /* Update previous state to current state */
- previousInputState3 = digitalInputState3;
- /* Report input state change to serial monitor */
- Serial.print("INPUT 3 (GPIO ");
- Serial.print(GPIO_INPUT_PIN_3);
- Serial.print(") State Changed to: ");
- Serial.println(digitalInputState3 ? "HIGH" : "LOW");
- }
- }
- }
- /****** PERIPHERAL CONTROL FUNCTION *****/
- void controlPeripherals(void)
- {
- /* Toggle digital output pin 1 state */
- digitalOutputState1 = (digitalOutputState1 == LOW) ? HIGH : LOW;
- digitalWrite(GPIO_OUTPUT_PIN_1, digitalOutputState1);
- /* Report output state change to serial monitor */
- Serial.print("OUTPUT 1 (GPIO ");
- Serial.print(GPIO_OUTPUT_PIN_1);
- Serial.print(") Set to: ");
- Serial.println(digitalOutputState1 ? "HIGH" : "LOW");
- /* Toggle digital output pin 2 state */
- digitalOutputState2 = (digitalOutputState2 == LOW) ? HIGH : LOW;
- digitalWrite(GPIO_OUTPUT_PIN_2, digitalOutputState2);
- /* Report output state change to serial monitor */
- Serial.print("OUTPUT 2 (GPIO ");
- Serial.print(GPIO_OUTPUT_PIN_2);
- Serial.print(") Set to: ");
- Serial.println(digitalOutputState2 ? "HIGH" : "LOW");
- /* Toggle digital output pin 3 state */
- digitalOutputState3 = (digitalOutputState3 == LOW) ? HIGH : LOW;
- digitalWrite(GPIO_OUTPUT_PIN_3, digitalOutputState3);
- /* Report output state change to serial monitor */
- Serial.print("OUTPUT 3 (GPIO ");
- Serial.print(GPIO_OUTPUT_PIN_3);
- Serial.print(") Set to: ");
- Serial.println(digitalOutputState3 ? "HIGH" : "LOW");
- /* Print timing information for monitoring purposes */
- Serial.print("Timestamp: ");
- Serial.print(currentTime);
- Serial.println("ms\n");
- }
- /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment