Skip to content

Instantly share code, notes, and snippets.

Last active Nov 19, 2022
What would you like to do?
#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
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++) {
// 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
// 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) {
} else {
void noteOn(byte pitch) {
MidiUSB.sendMIDI({0x09, 0x90, pitch, 127});
void noteOff(byte pitch) {
MidiUSB.sendMIDI({0x08, 0x80, pitch, 0});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment