Skip to content

Instantly share code, notes, and snippets.

@reefab
Last active June 22, 2019 20:56
Show Gist options
  • Save reefab/a6f9935188435a8633688d4ea84ac2ca to your computer and use it in GitHub Desktop.
Save reefab/a6f9935188435a8633688d4ea84ac2ca to your computer and use it in GitHub Desktop.
#include <Wire.h>
#include <Adafruit_Trellis.h>
#include "MIDIUSB.h"
#define LED 13 // Pin for heartbeat LED (shows code is working)
#define CHANNEL 1 // MIDI channel number
Adafruit_Trellis trellis;
uint8_t heart = 0; // Heartbeat LED counter
unsigned long prevReadTime = 0L; // Keypad polling timer
uint8_t mod;
uint8_t vel;
uint8_t fxc;
uint8_t rate;
uint8_t note[] = {
60, 61, 62, 63,
56, 57, 58, 59,
52, 53, 54, 55,
48, 49, 50, 51
};
void setup() {
pinMode(LED, OUTPUT);
trellis.begin(0x70); // Pass I2C address
#ifdef __AVR__
// Default Arduino I2C speed is 100 KHz, but the HT16K33 supports
// 400 KHz. We can force this for faster read & refresh, but may
// break compatibility with other I2C devices...so be prepared to
// comment this out, or save & restore value as needed.
TWBR = 12;
#endif
trellis.clear();
trellis.writeDisplay();
mod = map(analogRead(0), 0, 1023, 0, 127);
vel = map(analogRead(1), 0, 1023, 0, 127);
fxc = map(analogRead(2), 0, 1023, 0, 127);
rate = map(analogRead(3),0, 1023, 0, 127);
controlChange(CHANNEL, 1, mod);
controlChange(CHANNEL, 11, vel);
controlChange(CHANNEL, 12, fxc);
controlChange(CHANNEL, 13, rate);
}
void noteOn(byte channel, byte pitch, byte velocity) {
midiEventPacket_t noteOn = {0x09, 0x90 | channel, pitch, velocity};
MidiUSB.sendMIDI(noteOn);
MidiUSB.flush();
}
void noteOff(byte channel, byte pitch, byte velocity) {
midiEventPacket_t noteOff = {0x08, 0x80 | channel, pitch, velocity};
MidiUSB.sendMIDI(noteOff);
MidiUSB.flush();
}
void controlChange(byte channel, byte control, byte value) {
midiEventPacket_t event = {0x0B, 0xB0 | channel, control, value};
MidiUSB.sendMIDI(event);
MidiUSB.flush();
}
void loop() {
unsigned long t = millis();
if((t - prevReadTime) >= 20L) { // 20ms = min Trellis poll time
if(trellis.readSwitches()) { // Button state change?
for(uint8_t i=0; i<16; i++) { // For each button...
if(trellis.justPressed(i)) {
noteOn(CHANNEL, note[i], 127);
trellis.setLED(i);
} else if(trellis.justReleased(i)) {
noteOff(CHANNEL, note[i], 0);
trellis.clrLED(i);
}
}
trellis.writeDisplay();
}
uint8_t newModulation = map(analogRead(0), 0, 1023, 0, 127);
if(mod != newModulation) {
mod = newModulation;
controlChange(CHANNEL, 1, mod);
}
uint8_t newVelocity = map(analogRead(1), 0, 1023, 0, 127);
if(vel != newVelocity) {
vel = newVelocity;
controlChange(CHANNEL, 11, vel);
}
uint8_t newEffect = map(analogRead(2), 0, 1023, 0, 127);
if(fxc != newEffect) {
fxc = newEffect;
controlChange(CHANNEL, 12, fxc);
}
uint8_t newRate = map(analogRead(3), 0, 1023, 0, 127);
if(rate !=newRate) {
rate = newRate;
controlChange(CHANNEL, 13, rate);
}
prevReadTime = t;
digitalWrite(LED, ++heart & 32); // Blink = alive
}
}
@reefab
Copy link
Author

reefab commented Jun 22, 2019

Updated code for this: https://learn.adafruit.com/mini-untztrument-3d-printed-midi-controller with more modern libs replacing the original third party ones.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment