Skip to content

Instantly share code, notes, and snippets.

@sidmani
Created January 9, 2018 00:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sidmani/3bbccc5c1641ed6d6a38d967c52761f9 to your computer and use it in GitHub Desktop.
Save sidmani/3bbccc5c1641ed6d6a38d967c52761f9 to your computer and use it in GitHub Desktop.
MIDI keyboard with Teensyduino
/*
* MIT License
* Copyright (c) 2018 Sid Mani
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
const int row[9] = {26,0,1,2,3,10,11,12,13}; //positive in
const int col[6] = {4,5,6,7,8,9}; //out
int active[9][6] = {
{0,0,0,0,0,0},
{0,0,0,0,0,0},
{0,0,0,0,0,0},
{0,0,0,0,0,0},
{0,0,0,0,0,0},
{0,0,0,0,0,0},
{0,0,0,0,0,0},
{0,0,0,0,0,0},
{0,0,0,0,0,0}};
const int offset = 36;
const int channel = 1;
void setup()
{
for (int i = 0; i<9; i++)
{
pinMode(row[i], OUTPUT);
}
for (int i = 0; i<6; i++)
{
pinMode(col[i], INPUT);
}
}
void loop()
{
for (int r = 0; r<9; r++)
{
digitalWrite(row[r], HIGH);
for (int c = 0; c<6; c++)
{
int curr = digitalRead(col[c]);
if (active[r][c] != curr)
{
if (curr == 1)
{
usbMIDI.sendNoteOn(offset + r * 6 + c, 99, channel);
}
else
{
usbMIDI.sendNoteOff(offset + r * 6 + c, 99, channel);
}
active[r][c] = curr;
}
}
digitalWrite(row[r], LOW);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment