Advertisement
pleasedontcode

"Random MIDI Button" rev_01

Jan 2nd, 2024
99
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: "Random MIDI Button"
  13.     - Source Code compiled for: Arduino Nano
  14.     - Source Code created on: 2024-01-02 20:40:18
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* Use the midi library to create a simple midi */
  21.     /* controller that plays a random note on the press */
  22.     /* if the button. When the button. Is pressed again, */
  23.     /* send a note off message for the already played */
  24.     /* note and play another random note. Add a timeout */
  25.     /* after 5s */
  26. /****** END SYSTEM REQUIREMENTS *****/
  27.  
  28. /****** DEFINITION OF LIBRARIES *****/
  29. #include <Arduino.h>
  30. #include <EasyButton.h>
  31. #include <MIDI.h>
  32.  
  33. /****** FUNCTION PROTOTYPES *****/
  34. void setup();
  35. void loop();
  36. void handleButtonPress();
  37.  
  38. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  39. const uint8_t button1_PushButton_PIN_D2 = 2;
  40.  
  41. /****** DEFINITION OF LIBRARY CLASS INSTANCES*****/
  42. EasyButton button1(button1_PushButton_PIN_D2);
  43. MIDI_CREATE_DEFAULT_INSTANCE();
  44.  
  45. // MIDI note range
  46. const int MIDI_NOTE_MIN = 48;
  47. const int MIDI_NOTE_MAX = 72;
  48.  
  49. // Timeout duration
  50. const unsigned long TIMEOUT_DURATION = 5000; // 5 seconds
  51.  
  52. // Variables to track button state and timeout
  53. bool buttonPressed = false;
  54. unsigned long buttonPressTime = 0;
  55. bool notePlaying = false;
  56. int currentNote = 0;
  57. unsigned long timeoutStartTime = 0;
  58.  
  59. void setup()
  60. {
  61.   // Initialize the button object
  62.   button1.begin();
  63.  
  64.   // Set the button callback
  65.   button1.onPressed(handleButtonPress);
  66.  
  67.   // Initialize MIDI
  68.   MIDI.begin();
  69. }
  70.  
  71. void loop()
  72. {
  73.   // Read the button state
  74.   button1.read();
  75.  
  76.   // Check for timeout
  77.   if (notePlaying && millis() - timeoutStartTime >= TIMEOUT_DURATION)
  78.   {
  79.     MIDI.sendNoteOff(currentNote, 0, 1);
  80.     notePlaying = false;
  81.   }
  82. }
  83.  
  84. void handleButtonPress()
  85. {
  86.   // Check if the button was just pressed
  87.   if (button1.isPressed())
  88.   {
  89.     // If a note is already playing, send note off
  90.     if (notePlaying)
  91.     {
  92.       MIDI.sendNoteOff(currentNote, 0, 1);
  93.       notePlaying = false;
  94.     }
  95.  
  96.     // Generate a random note
  97.     currentNote = random(MIDI_NOTE_MIN, MIDI_NOTE_MAX + 1);
  98.  
  99.     // Send note on
  100.     MIDI.sendNoteOn(currentNote, 127, 1);
  101.  
  102.     // Set note playing flag and timeout start time
  103.     notePlaying = true;
  104.     timeoutStartTime = millis();
  105.   }
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement