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: # Drum Controller
- - Source Code NOT compiled for: ESP32 DevKit V1
- - Source Code created on: 2026-02-22 22:31:22
- ********* Pleasedontcode.com **********/
- /****** SYSTEM REQUIREMENTS *****/
- /****** SYSTEM REQUIREMENT 1 *****/
- /* **Improvement 1**: "Configure ESP32 DevKit V1 GPIO */
- /* pins for digital I/O operations with proper */
- /* initialization and state management." */
- /****** END SYSTEM REQUIREMENTS *****/
- /****** DEFINITION OF LIBRARIES *****/
- #include <Arduino.h>
- #include <BLEMIDI_Transport.h>
- #include <hardware/BLEMIDI_ESP32_NimBLE.h>
- #include "GPIO_Manager.h"
- BLEMIDI_CREATE_INSTANCE("ESP32Drums", MIDI);
- // Analog input pins for ESP32 DevKit V1
- // Note: Pins 34 and 35 are input-only ADC pins
- #define PAD1 32 // ADC4
- #define PAD2 33 // ADC5
- #define PAD3 34 // ADC6 (input-only)
- #define PAD4 35 // ADC7 (input-only)
- // Digital I/O pins for control signals and status indicators
- #define STATUS_LED_PIN 25 // Output pin for status LED
- #define ERROR_LED_PIN 26 // Output pin for error indicator LED
- #define MODE_BUTTON_PIN 27 // Input pin for mode selection button
- // Array to store the digital I/O pins for initialization
- static const uint8_t digitalIO_pins[] = {
- STATUS_LED_PIN,
- ERROR_LED_PIN,
- MODE_BUTTON_PIN
- };
- // Array to store the modes for corresponding digital I/O pins
- static const GPIO_Mode digitalIO_modes[] = {
- GPIO_MODE_OUTPUT, // STATUS_LED_PIN - Output for LED
- GPIO_MODE_OUTPUT, // ERROR_LED_PIN - Output for LED
- GPIO_MODE_INPUT_PULLUP // MODE_BUTTON_PIN - Input with pull-up
- };
- // Assign MIDI notes for each pad
- int padNotes[4] = {36, 38, 42, 46}; // Kick, Snare, Hi-Hat, Tom
- // Threshold for hit detection
- int threshold = 200;
- // Debounce timing (in milliseconds)
- unsigned long lastHitTime[4] = {0, 0, 0, 0};
- const unsigned long debounceDelay = 100; // Minimum time between pad hits
- // Status tracking variables
- volatile uint8_t system_running = 1; // Flag to track if system is running
- volatile uint8_t last_mode_button_state = HIGH; // Track mode button state
- unsigned long last_mode_button_time = 0; // Track mode button debounce time
- const unsigned long button_debounce_ms = 50; // Button debounce time in ms
- /****** FUNCTION PROTOTYPES *****/
- void setup(void);
- void loop(void);
- void initialize_digital_io(void);
- void handle_mode_button(void);
- void update_status_indicators(void);
- void process_drum_pads(void);
- void log_gpio_configuration(void);
- /**
- * initialize_digital_io()
- * Initialize all digital I/O pins using GPIO Manager
- * Configures output pins for status indicators and input pins for user controls
- */
- void initialize_digital_io(void)
- {
- // Initialize the GPIO Manager system
- GPIO_Manager_Init();
- Serial.println("[Setup] GPIO Manager initialized");
- // Configure all digital I/O pins at once
- uint8_t pins_configured = GPIO_Configure_Pins(
- digitalIO_pins,
- digitalIO_modes,
- sizeof(digitalIO_pins) / sizeof(digitalIO_pins[0])
- );
- Serial.printf("[Setup] Configured %d digital I/O pins\n", pins_configured);
- // Set initial state of output pins
- GPIO_Write(STATUS_LED_PIN, HIGH); // Turn on status LED
- GPIO_Write(ERROR_LED_PIN, LOW); // Turn off error LED
- Serial.println("[Setup] Digital I/O pins initialized and configured");
- }
- /**
- * handle_mode_button()
- * Handle mode button input with debouncing
- * Reads the mode button state and processes any state changes
- */
- void handle_mode_button(void)
- {
- // Get current time for debounce checking
- unsigned long currentTime = millis();
- // Only check button state if debounce time has elapsed
- if (currentTime - last_mode_button_time >= button_debounce_ms) {
- // Read the current button state
- int button_state = GPIO_Read(MODE_BUTTON_PIN);
- // Check if button state has changed (button pressed = LOW due to pull-up)
- if (button_state != last_mode_button_state && button_state == LOW) {
- // Button was just pressed
- Serial.println("[Mode Button] Button pressed - toggling system state");
- // Toggle system running state
- system_running = !system_running;
- // Update status LED to reflect system state
- if (system_running) {
- GPIO_Write(STATUS_LED_PIN, HIGH); // LED on = system running
- Serial.println("[System] System activated - drum pads enabled");
- } else {
- GPIO_Write(STATUS_LED_PIN, LOW); // LED off = system paused
- Serial.println("[System] System deactivated - drum pads disabled");
- }
- // Update last mode button state
- last_mode_button_state = button_state;
- last_mode_button_time = currentTime;
- } else if (button_state != last_mode_button_state) {
- // Button state changed (released)
- last_mode_button_state = button_state;
- last_mode_button_time = currentTime;
- }
- }
- }
- /**
- * update_status_indicators()
- * Update LED status indicators based on system state
- * Provides visual feedback of system operation
- */
- void update_status_indicators(void)
- {
- // Get the current state of the status LED
- GPIO_Pin_State* status_led_state = GPIO_Get_Pin_State(STATUS_LED_PIN);
- if (status_led_state != NULL) {
- // If system is running, ensure status LED is on
- if (system_running && status_led_state->state == LOW) {
- GPIO_Write(STATUS_LED_PIN, HIGH);
- }
- // If system is not running, ensure status LED is off
- else if (!system_running && status_led_state->state == HIGH) {
- GPIO_Write(STATUS_LED_PIN, LOW);
- }
- }
- // Error LED remains off during normal operation
- GPIO_Write(ERROR_LED_PIN, LOW);
- }
- /**
- * process_drum_pads()
- * Read analog values from drum pads and trigger MIDI notes
- * Only processes pads if system is running
- */
- void process_drum_pads(void)
- {
- // Skip pad processing if system is not running
- if (!system_running) {
- return;
- }
- // Read analog values from all pads
- int padValues[4];
- padValues[0] = analogRead(PAD1);
- padValues[1] = analogRead(PAD2);
- padValues[2] = analogRead(PAD3);
- padValues[3] = analogRead(PAD4);
- // Get current time for debounce checking
- unsigned long currentTime = millis();
- // Check each pad for a hit above the threshold
- for (int i = 0; i < 4; i++) {
- if (padValues[i] > threshold) {
- // Verify debounce time has elapsed since last hit on this pad
- if (currentTime - lastHitTime[i] > debounceDelay) {
- // Calculate velocity based on analog value
- int velocity = map(padValues[i], threshold, 4095, 1, 127);
- // Send MIDI note on
- MIDI.sendNoteOn(padNotes[i], velocity, 1);
- delay(10); // short delay between note on and note off
- // Send MIDI note off
- MIDI.sendNoteOff(padNotes[i], 0, 1);
- // Log the pad hit
- Serial.printf("[Drum Pad] Pad %d hit! Note %d, Velocity %d\n", i+1, padNotes[i], velocity);
- // Update last hit time for this pad
- lastHitTime[i] = currentTime;
- }
- }
- }
- }
- /**
- * log_gpio_configuration()
- * Log the current GPIO configuration to serial output
- * Displays all configured pins and their current states
- */
- void log_gpio_configuration(void)
- {
- Serial.println("\n========== GPIO CONFIGURATION REPORT ==========");
- // Array to hold pin states
- GPIO_Pin_State pin_states[MAX_GPIO_PINS];
- // Get all configured pin states
- uint8_t pin_count = GPIO_Get_All_Pin_States(pin_states, MAX_GPIO_PINS);
- Serial.printf("Total configured pins: %d\n", pin_count);
- Serial.println("-------------------------------------------");
- // Display each configured pin's information
- for (uint8_t i = 0; i < pin_count; i++) {
- const char* mode_str;
- // Convert mode to string representation
- switch (pin_states[i].mode) {
- case GPIO_MODE_INPUT:
- mode_str = "INPUT";
- break;
- case GPIO_MODE_OUTPUT:
- mode_str = "OUTPUT";
- break;
- case GPIO_MODE_INPUT_PULLUP:
- mode_str = "INPUT_PULLUP";
- break;
- case GPIO_MODE_INPUT_PULLDOWN:
- mode_str = "INPUT_PULLDOWN";
- break;
- default:
- mode_str = "UNKNOWN";
- break;
- }
- Serial.printf("Pin %d: Mode=%s, State=%s, Initialized=%s, LastChange=%lums\n",
- pin_states[i].pin,
- mode_str,
- (pin_states[i].state == HIGH) ? "HIGH" : "LOW",
- (pin_states[i].initialized) ? "Yes" : "No",
- millis() - pin_states[i].lastChangeTime);
- }
- Serial.println("==============================================\n");
- }
- /**
- * setup()
- * Arduino setup function - runs once at startup
- * Initializes serial communication, GPIO, MIDI, and peripheral devices
- */
- void setup(void)
- {
- // Initialize serial communication for debugging
- Serial.begin(115200);
- delay(1000); // Wait for serial to initialize
- Serial.println("\n========================================");
- Serial.println("ESP32 Drum Pad MIDI Controller");
- Serial.println("System Requirements: Configure ESP32 DevKit V1 GPIO");
- Serial.println("pins for digital I/O with proper initialization");
- Serial.println("and state management");
- Serial.println("========================================\n");
- // Initialize GPIO pins for digital I/O operations
- // This satisfies System Requirement 1
- initialize_digital_io();
- // Initialize BLE MIDI
- MIDI.begin(MIDI_CHANNEL_OMNI);
- Serial.println("[Setup] BLE MIDI initialized and ready");
- // Configure analog input pins as inputs (ADC pins on ESP32)
- pinMode(PAD1, INPUT);
- pinMode(PAD2, INPUT);
- pinMode(PAD3, INPUT);
- pinMode(PAD4, INPUT);
- Serial.println("[Setup] Analog drum pad pins configured");
- // Log initial GPIO configuration
- log_gpio_configuration();
- Serial.println("[Setup] Initialization complete - System ready\n");
- }
- /**
- * loop()
- * Arduino main loop function - runs continuously
- * Handles button input, processes drum pads, and updates status indicators
- */
- void loop(void)
- {
- // Handle mode button input and state changes
- handle_mode_button();
- // Process drum pad inputs and send MIDI notes
- process_drum_pads();
- // Update LED status indicators
- update_status_indicators();
- // Small delay to prevent excessive CPU usage
- delay(5);
- }
- /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment