Skip to content

Instantly share code, notes, and snippets.

@bil-bas
Last active October 6, 2025 13:52
Show Gist options
  • Select an option

  • Save bil-bas/e84b9403a0f2dfebdbf66a56f8403cda to your computer and use it in GitHub Desktop.

Select an option

Save bil-bas/e84b9403a0f2dfebdbf66a56f8403cda to your computer and use it in GitHub Desktop.
Capacitive touch, polyphonic "stylophone" midi keyboard
// TEENSY 3.2
#include <MIDI.h>
const NUM_BLACK_KEYS = 8;
const NUM_WHITE_KEYS = 12;
const MP_0 = 2;
const MP_1 = 3;
const MP_2 = 4;
const MP_3 = 5;
const TOUCH_WHITE = 7;
const TOUCH_BLACK = 8;
const THRESHOLD = 200;
boolean key_state[NUM_BLACK_KEYS + MUM_WHITE_KEYS];
// Create and bind the MIDI interface to the default hardware Serial port
MIDI_CREATE_DEFAULT_INSTANCE();
void setup()
{
MIDI.begin(MIDI_CHANNEL_OMNI); // Listen to all incoming messages
}
void loop() {
loop_keys(TOUCH_BLACK, NUM_BLACK_KEYS);
loop_keys(TOUCH_WHITE, NUM_WHITE_KEYS);
}
void loop_keys(int touch_pin, int num_keys) {
for (int i = 0; i < num_keys; i++) {
digitalWrite(MP_0, bitRead(i, 0));
digitalWrite(MP_1, bitRead(i, 1));
digitalWrite(MP_2, bitRead(i, 2));
digitalWrite(MP_3, bitRead(i, 3));
}
int current = constrain(touchRead(touch_pin), 1000, 5000, 0, 127), 0, 127);
if (current >= THRESHOLD) {
if (key_state[i] == 0) {
key_states[i] = 1;
MIDI.sendNoteOn(60 + i, 127, 1);
}
} else {
if (key_state[i] == 1) {
key_states[i] = 0;
MIDI.sendNoteOn(60 + i, 0, 1);
}
}
MIDI.read();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment