Advertisement
pleasedontcode

**Button Control** rev_02

Jun 9th, 2025
318
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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: **Button Control**
  13.     - Source Code NOT compiled for: Arduino Pro Mini 5V
  14.     - Source Code created on: 2025-06-09 20:51:16
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* Utilizes MIDIUSB library to create a MIDI */
  21.     /* controller that can send note and control change */
  22.     /* messages, allowing integration with various MIDI- */
  23.     /* compatible software and hardware. */
  24. /****** END SYSTEM REQUIREMENTS *****/
  25.  
  26.  
  27. /********* User code review feedback **********
  28. #### Feedback 1 ####
  29. - Compilation error: 'MIDI' was not declared in this scope
  30. ********* User code review feedback **********/
  31.  
  32. /* START CODE */
  33.  
  34. /****** DEFINITION OF LIBRARIES *****/
  35. #include <MIDIUSB.h>    //https://github.com/arduino-libraries/MIDIUSB
  36. #include <Adafruit_NeoPixel.h> // NeoPixel library
  37.  
  38. /****** FUNCTION PROTOTYPES *****/
  39. void setup(void);
  40. void loop(void);
  41.  
  42. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  43. #define NUM_BUTTONS 1 // number of buttons on the controller
  44. #define BUTTON_PIN 2 // pin connected to the buttons
  45. #define NUM_PIXELS 1 // number of NeoPixels on the controller
  46. #define PIXEL_PIN 5 // pin connected to the NeoPixels
  47.  
  48. Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUM_PIXELS, PIXEL_PIN, NEO_GRB + NEO_KHZ800); // initialize NeoPixel object
  49. int buttonState[NUM_BUTTONS]; // array to store button states
  50. int lastButtonState[NUM_BUTTONS]; // array to store last button states
  51.  
  52. void setup(void)
  53. {
  54.     // put your setup code here, to run once:
  55.     MidiUSB.begin(); // initialize MIDI communication
  56.     pinMode(BUTTON_PIN, INPUT_PULLUP); // set button pin as input with internal pull-up resistor
  57.     pixels.begin(); // initialize NeoPixels
  58. }
  59.  
  60. void loop(void)
  61. {
  62.     // put your main code here, to run repeatedly:
  63.     for (int i = 0; i < NUM_BUTTONS; i++) { // loop through all buttons
  64.         buttonState[i] = digitalRead(BUTTON_PIN + i); // read button state
  65.         if (buttonState[i] != lastButtonState[i]) { // if button state has changed
  66.             if (buttonState[i] == LOW) { // if button is pressed
  67.                 pixels.setPixelColor(i, pixels.Color(255, 255, 255)); // set NeoPixel color to white
  68.                 MidiUSB.sendNoteOn(i + 60, 127, 1); // send MIDI note on message with note number and velocity
  69.             } else { // if button is released
  70.                 pixels.setPixelColor(i, pixels.Color(0, 0, 0)); // turn off NeoPixel
  71.                 MidiUSB.sendNoteOff(i + 60, 0, 1); // send MIDI note off message with note number and velocity
  72.             }
  73.             pixels.show(); // update NeoPixels
  74.         }
  75.         lastButtonState[i] = buttonState[i]; // store current button state as last button state
  76.     }
  77. }
  78.  
  79. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement