pleasedontcode

# Drum Controller rev_08

Feb 22nd, 2026
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 11.01 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: # Drum Controller
  13.     - Source Code NOT compiled for: ESP32 DevKit V1
  14.     - Source Code created on: 2026-02-22 22:31:22
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* **Improvement 1**: "Configure ESP32 DevKit V1 GPIO */
  21.     /* pins for digital I/O operations with proper */
  22.     /* initialization and state management." */
  23. /****** END SYSTEM REQUIREMENTS *****/
  24.  
  25.  
  26. /****** DEFINITION OF LIBRARIES *****/
  27. #include <Arduino.h>
  28. #include <BLEMIDI_Transport.h>
  29. #include <hardware/BLEMIDI_ESP32_NimBLE.h>
  30. #include "GPIO_Manager.h"
  31.  
  32. BLEMIDI_CREATE_INSTANCE("ESP32Drums", MIDI);
  33.  
  34. // Analog input pins for ESP32 DevKit V1
  35. // Note: Pins 34 and 35 are input-only ADC pins
  36. #define PAD1 32  // ADC4
  37. #define PAD2 33  // ADC5
  38. #define PAD3 34  // ADC6 (input-only)
  39. #define PAD4 35  // ADC7 (input-only)
  40.  
  41. // Digital I/O pins for control signals and status indicators
  42. #define STATUS_LED_PIN 25      // Output pin for status LED
  43. #define ERROR_LED_PIN 26       // Output pin for error indicator LED
  44. #define MODE_BUTTON_PIN 27     // Input pin for mode selection button
  45.  
  46. // Array to store the digital I/O pins for initialization
  47. static const uint8_t digitalIO_pins[] = {
  48.     STATUS_LED_PIN,
  49.     ERROR_LED_PIN,
  50.     MODE_BUTTON_PIN
  51. };
  52.  
  53. // Array to store the modes for corresponding digital I/O pins
  54. static const GPIO_Mode digitalIO_modes[] = {
  55.     GPIO_MODE_OUTPUT,       // STATUS_LED_PIN - Output for LED
  56.     GPIO_MODE_OUTPUT,       // ERROR_LED_PIN - Output for LED
  57.     GPIO_MODE_INPUT_PULLUP  // MODE_BUTTON_PIN - Input with pull-up
  58. };
  59.  
  60. // Assign MIDI notes for each pad
  61. int padNotes[4] = {36, 38, 42, 46}; // Kick, Snare, Hi-Hat, Tom
  62.  
  63. // Threshold for hit detection
  64. int threshold = 200;
  65.  
  66. // Debounce timing (in milliseconds)
  67. unsigned long lastHitTime[4] = {0, 0, 0, 0};
  68. const unsigned long debounceDelay = 100; // Minimum time between pad hits
  69.  
  70. // Status tracking variables
  71. volatile uint8_t system_running = 1;    // Flag to track if system is running
  72. volatile uint8_t last_mode_button_state = HIGH;  // Track mode button state
  73. unsigned long last_mode_button_time = 0;  // Track mode button debounce time
  74. const unsigned long button_debounce_ms = 50;  // Button debounce time in ms
  75.  
  76. /****** FUNCTION PROTOTYPES *****/
  77. void setup(void);
  78. void loop(void);
  79. void initialize_digital_io(void);
  80. void handle_mode_button(void);
  81. void update_status_indicators(void);
  82. void process_drum_pads(void);
  83. void log_gpio_configuration(void);
  84.  
  85. /**
  86.  * initialize_digital_io()
  87.  * Initialize all digital I/O pins using GPIO Manager
  88.  * Configures output pins for status indicators and input pins for user controls
  89.  */
  90. void initialize_digital_io(void)
  91. {
  92.     // Initialize the GPIO Manager system
  93.     GPIO_Manager_Init();
  94.     Serial.println("[Setup] GPIO Manager initialized");
  95.  
  96.     // Configure all digital I/O pins at once
  97.     uint8_t pins_configured = GPIO_Configure_Pins(
  98.         digitalIO_pins,
  99.         digitalIO_modes,
  100.         sizeof(digitalIO_pins) / sizeof(digitalIO_pins[0])
  101.     );
  102.  
  103.     Serial.printf("[Setup] Configured %d digital I/O pins\n", pins_configured);
  104.  
  105.     // Set initial state of output pins
  106.     GPIO_Write(STATUS_LED_PIN, HIGH);      // Turn on status LED
  107.     GPIO_Write(ERROR_LED_PIN, LOW);        // Turn off error LED
  108.  
  109.     Serial.println("[Setup] Digital I/O pins initialized and configured");
  110. }
  111.  
  112. /**
  113.  * handle_mode_button()
  114.  * Handle mode button input with debouncing
  115.  * Reads the mode button state and processes any state changes
  116.  */
  117. void handle_mode_button(void)
  118. {
  119.     // Get current time for debounce checking
  120.     unsigned long currentTime = millis();
  121.  
  122.     // Only check button state if debounce time has elapsed
  123.     if (currentTime - last_mode_button_time >= button_debounce_ms) {
  124.         // Read the current button state
  125.         int button_state = GPIO_Read(MODE_BUTTON_PIN);
  126.  
  127.         // Check if button state has changed (button pressed = LOW due to pull-up)
  128.         if (button_state != last_mode_button_state && button_state == LOW) {
  129.             // Button was just pressed
  130.             Serial.println("[Mode Button] Button pressed - toggling system state");
  131.  
  132.             // Toggle system running state
  133.             system_running = !system_running;
  134.  
  135.             // Update status LED to reflect system state
  136.             if (system_running) {
  137.                 GPIO_Write(STATUS_LED_PIN, HIGH);  // LED on = system running
  138.                 Serial.println("[System] System activated - drum pads enabled");
  139.             } else {
  140.                 GPIO_Write(STATUS_LED_PIN, LOW);   // LED off = system paused
  141.                 Serial.println("[System] System deactivated - drum pads disabled");
  142.             }
  143.  
  144.             // Update last mode button state
  145.             last_mode_button_state = button_state;
  146.             last_mode_button_time = currentTime;
  147.         } else if (button_state != last_mode_button_state) {
  148.             // Button state changed (released)
  149.             last_mode_button_state = button_state;
  150.             last_mode_button_time = currentTime;
  151.         }
  152.     }
  153. }
  154.  
  155. /**
  156.  * update_status_indicators()
  157.  * Update LED status indicators based on system state
  158.  * Provides visual feedback of system operation
  159.  */
  160. void update_status_indicators(void)
  161. {
  162.     // Get the current state of the status LED
  163.     GPIO_Pin_State* status_led_state = GPIO_Get_Pin_State(STATUS_LED_PIN);
  164.  
  165.     if (status_led_state != NULL) {
  166.         // If system is running, ensure status LED is on
  167.         if (system_running && status_led_state->state == LOW) {
  168.             GPIO_Write(STATUS_LED_PIN, HIGH);
  169.         }
  170.         // If system is not running, ensure status LED is off
  171.         else if (!system_running && status_led_state->state == HIGH) {
  172.             GPIO_Write(STATUS_LED_PIN, LOW);
  173.         }
  174.     }
  175.  
  176.     // Error LED remains off during normal operation
  177.     GPIO_Write(ERROR_LED_PIN, LOW);
  178. }
  179.  
  180. /**
  181.  * process_drum_pads()
  182.  * Read analog values from drum pads and trigger MIDI notes
  183.  * Only processes pads if system is running
  184.  */
  185. void process_drum_pads(void)
  186. {
  187.     // Skip pad processing if system is not running
  188.     if (!system_running) {
  189.         return;
  190.     }
  191.  
  192.     // Read analog values from all pads
  193.     int padValues[4];
  194.     padValues[0] = analogRead(PAD1);
  195.     padValues[1] = analogRead(PAD2);
  196.     padValues[2] = analogRead(PAD3);
  197.     padValues[3] = analogRead(PAD4);
  198.  
  199.     // Get current time for debounce checking
  200.     unsigned long currentTime = millis();
  201.  
  202.     // Check each pad for a hit above the threshold
  203.     for (int i = 0; i < 4; i++) {
  204.         if (padValues[i] > threshold) {
  205.             // Verify debounce time has elapsed since last hit on this pad
  206.             if (currentTime - lastHitTime[i] > debounceDelay) {
  207.                 // Calculate velocity based on analog value
  208.                 int velocity = map(padValues[i], threshold, 4095, 1, 127);
  209.  
  210.                 // Send MIDI note on
  211.                 MIDI.sendNoteOn(padNotes[i], velocity, 1);
  212.                 delay(10); // short delay between note on and note off
  213.  
  214.                 // Send MIDI note off
  215.                 MIDI.sendNoteOff(padNotes[i], 0, 1);
  216.  
  217.                 // Log the pad hit
  218.                 Serial.printf("[Drum Pad] Pad %d hit! Note %d, Velocity %d\n", i+1, padNotes[i], velocity);
  219.  
  220.                 // Update last hit time for this pad
  221.                 lastHitTime[i] = currentTime;
  222.             }
  223.         }
  224.     }
  225. }
  226.  
  227. /**
  228.  * log_gpio_configuration()
  229.  * Log the current GPIO configuration to serial output
  230.  * Displays all configured pins and their current states
  231.  */
  232. void log_gpio_configuration(void)
  233. {
  234.     Serial.println("\n========== GPIO CONFIGURATION REPORT ==========");
  235.  
  236.     // Array to hold pin states
  237.     GPIO_Pin_State pin_states[MAX_GPIO_PINS];
  238.  
  239.     // Get all configured pin states
  240.     uint8_t pin_count = GPIO_Get_All_Pin_States(pin_states, MAX_GPIO_PINS);
  241.  
  242.     Serial.printf("Total configured pins: %d\n", pin_count);
  243.     Serial.println("-------------------------------------------");
  244.  
  245.     // Display each configured pin's information
  246.     for (uint8_t i = 0; i < pin_count; i++) {
  247.         const char* mode_str;
  248.  
  249.         // Convert mode to string representation
  250.         switch (pin_states[i].mode) {
  251.             case GPIO_MODE_INPUT:
  252.                 mode_str = "INPUT";
  253.                 break;
  254.             case GPIO_MODE_OUTPUT:
  255.                 mode_str = "OUTPUT";
  256.                 break;
  257.             case GPIO_MODE_INPUT_PULLUP:
  258.                 mode_str = "INPUT_PULLUP";
  259.                 break;
  260.             case GPIO_MODE_INPUT_PULLDOWN:
  261.                 mode_str = "INPUT_PULLDOWN";
  262.                 break;
  263.             default:
  264.                 mode_str = "UNKNOWN";
  265.                 break;
  266.         }
  267.  
  268.         Serial.printf("Pin %d: Mode=%s, State=%s, Initialized=%s, LastChange=%lums\n",
  269.                      pin_states[i].pin,
  270.                      mode_str,
  271.                      (pin_states[i].state == HIGH) ? "HIGH" : "LOW",
  272.                      (pin_states[i].initialized) ? "Yes" : "No",
  273.                      millis() - pin_states[i].lastChangeTime);
  274.     }
  275.  
  276.     Serial.println("==============================================\n");
  277. }
  278.  
  279. /**
  280.  * setup()
  281.  * Arduino setup function - runs once at startup
  282.  * Initializes serial communication, GPIO, MIDI, and peripheral devices
  283.  */
  284. void setup(void)
  285. {
  286.     // Initialize serial communication for debugging
  287.     Serial.begin(115200);
  288.     delay(1000); // Wait for serial to initialize
  289.  
  290.     Serial.println("\n========================================");
  291.     Serial.println("ESP32 Drum Pad MIDI Controller");
  292.     Serial.println("System Requirements: Configure ESP32 DevKit V1 GPIO");
  293.     Serial.println("pins for digital I/O with proper initialization");
  294.     Serial.println("and state management");
  295.     Serial.println("========================================\n");
  296.  
  297.     // Initialize GPIO pins for digital I/O operations
  298.     // This satisfies System Requirement 1
  299.     initialize_digital_io();
  300.  
  301.     // Initialize BLE MIDI
  302.     MIDI.begin(MIDI_CHANNEL_OMNI);
  303.     Serial.println("[Setup] BLE MIDI initialized and ready");
  304.  
  305.     // Configure analog input pins as inputs (ADC pins on ESP32)
  306.     pinMode(PAD1, INPUT);
  307.     pinMode(PAD2, INPUT);
  308.     pinMode(PAD3, INPUT);
  309.     pinMode(PAD4, INPUT);
  310.     Serial.println("[Setup] Analog drum pad pins configured");
  311.  
  312.     // Log initial GPIO configuration
  313.     log_gpio_configuration();
  314.  
  315.     Serial.println("[Setup] Initialization complete - System ready\n");
  316. }
  317.  
  318. /**
  319.  * loop()
  320.  * Arduino main loop function - runs continuously
  321.  * Handles button input, processes drum pads, and updates status indicators
  322.  */
  323. void loop(void)
  324. {
  325.     // Handle mode button input and state changes
  326.     handle_mode_button();
  327.  
  328.     // Process drum pad inputs and send MIDI notes
  329.     process_drum_pads();
  330.  
  331.     // Update LED status indicators
  332.     update_status_indicators();
  333.  
  334.     // Small delay to prevent excessive CPU usage
  335.     delay(5);
  336. }
  337.  
  338. /* END CODE */
  339.  
Advertisement
Add Comment
Please, Sign In to add comment