Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <midi_serialization.h>
- #include <usbmidi.h>
- void sendCC(uint8_t channel, uint8_t control, uint8_t value) {
- USBMIDI.write(0xB0 | (channel & 0xf));
- USBMIDI.write(control & 0x7f);
- USBMIDI.write(value & 0x7f);
- }
- void sendNote(uint8_t channel, uint8_t note, uint8_t velocity) {
- USBMIDI.write((velocity != 0 ? 0x90 : 0x80) | (channel & 0xf));
- USBMIDI.write(note & 0x7f);
- USBMIDI.write(velocity & 0x7f);
- }
- #include <Keypad.h>
- const byte ROWS = 4; // four rows
- const byte COLS = 4; // four columns
- char keys[ROWS][COLS] = {
- {(char)36, (char)37, (char)38, (char)39},
- {(char)40, (char)41, (char)42, (char)43},
- {(char)44, (char)45, (char)46, (char)47},
- {(char)48, (char)49, (char)50, (char)51}
- };
- byte rowPins[ROWS] = {2, 3, 4, 5}; // connect to the row pinouts of the keypad
- byte colPins[COLS] = {6, 7, 8, 9}; // connect to the column pinouts of the keypad
- Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
- #include <LedControl.h>
- LedControl lc = LedControl(11,13,10,1); //DIN, SCK, LOAD,1xMAX7219
- byte state = 0;
- byte channel = 0;
- byte note;
- boolean noteDown = LOW;
- void onLED () {
- if ((note >= 36) && (note <= 99)) {
- int col = (note - 36) / 8;
- int row = (note - 36) % 8;
- lc.setLed(0, col, row, true);
- }
- }
- void offLED ()
- {
- if ((note >= 36) && (note <= 99)) {
- int col = (note - 36) / 8;
- int row = (note - 36) % 8;
- lc.setLed(0, col, row, false);
- }
- }
- void setup() {
- pinMode(13, OUTPUT);
- lc.shutdown(0, false);
- lc.setIntensity(0, 8);
- lc.clearDisplay(0);
- digitalWrite(13, LOW);
- }
- void loop() {
- //Handle USB communication
- USBMIDI.poll();
- while (USBMIDI.available()) {
- // We must read entire available data, so in case we receive incoming
- // MIDI data, the host wouldn't get stuck.
- byte incomingByte = USBMIDI.read();
- switch (state) {
- case 0:
- // look for as status-byte, our channel, note on
- if (incomingByte == ( 0x90 | channel)) { // read only one channel
- noteDown = HIGH;
- state = 1;
- }
- // look for as status-byte, our channel, note off
- if (incomingByte == (0x80 | channel)) { // read only one channel
- noteDown = LOW;
- state = 1;
- }
- // look for any after touch, or program message
- if ((incomingByte & 0xE0) == 0xC0) {
- state = 4; // just wait for the data
- }
- // look for any control or polyphonic after touch
- if ((incomingByte & 0xE0) == 0xA0) {
- state = 3; // just wait for two bytes of data
- }
- // look for any pitch wheel or Channel Mode data
- if ((incomingByte & 0xF0) == 0xA0 || (incomingByte & 0xF0) == 0xB0) {
- state = 3; // just wait for two bytes of data
- }
- break;
- case 1:
- // get the note to play or stop
- if (incomingByte < 128) {
- note = incomingByte;
- state = 2;
- }
- else {
- state = 0; // reset state machine as this should be a note number
- }
- break;
- case 2:
- // get the velocity
- if (incomingByte < 128) {
- doNote(note, incomingByte, noteDown);
- }
- state = 0; // reset state machine to start
- break;
- case 3: // first of two bytes to discard
- state = 4; // next byte to discard
- break;
- case 4: // data to discard
- state = 0; // reset state machine
- }
- }
- if (kpd.getKeys())
- {
- for (int i = 0; i < LIST_MAX; i++) // Scan the whole key list.
- {
- if ( kpd.key[i].stateChanged ) // Only find keys that have changed state.
- {
- switch (kpd.key[i].kstate) { // Report active key state : IDLE, PRESSED, HOLD, or RELEASED
- case PRESSED:
- sendNote(0, kpd.key[i].kchar, 127);
- break;
- case RELEASED:
- sendNote(0, kpd.key[i].kchar, 0);
- }
- }
- }
- }
- // Flush the output.
- USBMIDI.flush();
- }
- void doNote(byte note, byte velocity, int down) {
- // if velocity = 0 on a 'Note ON1 command, treat it as a note off
- if ((down == HIGH) && (velocity == 0)) {
- down = LOW;
- }
- // send out this note message
- if (down == LOW) offLED();
- else onLED();
- }
Add Comment
Please, Sign In to add comment