Advertisement
Guest User

Untitled

a guest
Apr 18th, 2025
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.49 KB | None | 0 0
  1. // Programmed for Inland Arduino Nano clone, Microcenter Inland Nano requires bootloader set to ATmega328p (old bootloader)
  2. // Functional on typical Arduino Nano
  3. // Ported from PJRC Teensy 4.1
  4.  
  5. #include <digitalWriteFast.h> // Include library for fast digital read/writes (requires download through Library Manager)
  6.  
  7. const int buttonPin = 2; // Set button pin to pin 2
  8. int lastState = HIGH; // Set "last button state" for first loop() comparison
  9. int currentState; // Declare "current button state" variable
  10.  
  11. const byte midiChannel = 0; // 0 = Channel 1
  12. const byte ccNumber = 18; // BUS1 FX3 is CC#18
  13.  
  14. void sendMIDI_CC(byte controlNumber, byte value) {
  15. byte status = 0xB0 | (midiChannel & 0x0F); // 0xB0 = Control Change on channel 1
  16. Serial.write(status);
  17. Serial.write(controlNumber); // Write CC number
  18. Serial.write(value); // Write CC value
  19. }
  20.  
  21. void setup() {
  22. pinMode(buttonPin, INPUT_PULLUP); // Setup button pin
  23. Serial.begin(31250); // MIDI baud rate
  24. }
  25.  
  26. void loop() {
  27. currentState = digitalReadFast(buttonPin);
  28.  
  29. if (currentState != lastState) {
  30. delay(5); // debounce code to prevent flooding MIDI commands from an iffy switch
  31. currentState = digitalReadFast(buttonPin);
  32.  
  33. if (currentState != lastState) {
  34. if (currentState == LOW) {
  35. sendMIDI_CC(ccNumber, 127); // Button pressed
  36. } else {
  37. sendMIDI_CC(ccNumber, 0); // Button released
  38. }
  39.  
  40. lastState = currentState; // Reset button state
  41. }
  42. }
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement