Last active
April 21, 2024 03:30
-
-
Save lean8086/7de896c3b8f29d15e092e17ad177d055 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "MIDIUSB.h" | |
const byte TOTAL_BUTTONS = 16; | |
// All the Arduino pins used for buttons, in order. | |
const byte BUTTONS_PIN[TOTAL_BUTTONS] = {2,3,4,5,6,7,8,9,10,11,12,A0,A1,A2,A3,A4}; | |
// Every pitch corresponding to every Arduino pin. Each note has an associated numeric pitch (frequency scale). | |
// See https://github.com/arduino/tutorials/blob/master/ArduinoZeroMidi/PitchToNote.h | |
const byte BUTTONS_PITCH[TOTAL_BUTTONS] = {36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51}; | |
// Current state of the pressed buttons. | |
byte currentRead[TOTAL_BUTTONS]; | |
// Temporary input reads to check against current state. | |
byte tempRead; | |
// The setup function runs once when you press reset or power the board | |
void setup() { | |
// Initialize all the pins as a pull-up input. | |
for (byte i = 0; i < TOTAL_BUTTONS; i++) { | |
pinMode(BUTTONS_PIN[i], INPUT_PULLUP); | |
} | |
} | |
// The loop function runs over and over again forever | |
void loop() { | |
for (byte i = 0; i < TOTAL_BUTTONS; i++) { | |
// Get the digital state from the button pin. | |
// In pull-up inputs the button logic is inverted (HIGH is not pressed, LOW is pressed). | |
byte buttonState = digitalRead(BUTTONS_PIN[i]); | |
// Temporarily store the digital state. | |
tempRead = buttonState; | |
// Continue only if the last state is different to the current state. | |
if (currentRead[i] != tempRead) { | |
// See https://docs.arduino.cc/built-in-examples/digital/Debounce/ | |
delay(2); | |
// Get the pitch mapped to the pressed button. | |
byte pitch = BUTTONS_PITCH[i]; | |
// Save the new input state. | |
currentRead[i] = tempRead; | |
// Execute note on or noted off depending on the button state. | |
if (buttonState == LOW) { | |
noteOn(pitch); | |
} else { | |
noteOff(pitch); | |
} | |
} | |
} | |
} | |
void noteOn(byte pitch) { | |
MidiUSB.sendMIDI({0x09, 0x90, pitch, 127}); | |
MidiUSB.flush(); | |
} | |
void noteOff(byte pitch) { | |
MidiUSB.sendMIDI({0x08, 0x80, pitch, 0}); | |
MidiUSB.flush(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
To use a matrix button, you need to add a diode and utilize the matrix button method to enable simultaneous pressing of all buttons at the same time: