Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "MIDIUSB.h"
- #define debug SERIAL_PORT_MONITOR
- #define ledPin A2
- #define Keys 32
- #define debugStart
- //comment out to disable delay and printing during setup,
- //the purpose is to show the pins for matrix through l2c
- const int rows[] = {5, 4, 3, 2, 1, 0, 21, 20};
- const int cols[] = {6, 8, 10, 12, 7, 9, 11, 13};
- const int rowCount = 8;
- const int colCount = 8;
- //Loop count and delay variables
- //Loopcount
- unsigned long startTime;
- unsigned long loopCount = 0;
- unsigned long looptimer = 0;
- //Delay
- int printTime = 30;
- int printDelay = 600;
- unsigned long time_now = 0;
- //Debounce variables
- int debounceSampleMax = 40; // number of millis/samples to consider before declaring a debounced input
- int debounceRead[colCount][rowCount]; // the current value read from the input pin
- int debounceState[colCount][rowCount]; // the debounced input value
- int debounceSampleCount[colCount][rowCount]; // how many times we have seen new value
- long debounceTime[colCount][rowCount]; // the last time the output pin was sampled
- //
- //Normal rows: {8,9,10,11,12,13,14,15};
- //Inverted rows:{15,14,13,12,11,10,9,8};
- const int keynote[colCount][rowCount] = {
- {1,9,17,25,33,41,49,57},
- {2,10,18,26,34,42,50,58},
- {3,11,19,27,35,43,51,59},
- {4,12,20,28,36,44,52,60},
- {5,13,21,29,37,45,53,61},
- {6,14,22,30,38,46,54,62},
- {7,15,23,31,39,47,55,63},
- {8,16,24,32,40,48,56,64}
- };
- int firstKey=36;
- int num_keys = Keys;
- int channel=0;
- int keys[colCount][rowCount];
- int keys_prev[colCount][rowCount];
- int noteIndex[48];
- int noteCount;
- void setup() {
- debug.begin(115200);
- // use default address 0
- // pinMode(0, INPUT);
- // pinMode(0, HIGH); // turn on a 100K pullup internally
- #ifdef debugStart
- delay(2500);
- debug.println("*****************START*******************");
- #endif
- for (int x = 0; x < rowCount; x++) {
- #ifdef debugStart
- debug.print(x + 1); debug.print("/"); debug.print(rowCount);
- debug.print(": "); debug.print("Pin #"); debug.print(rows[x]); debug.println(" as input");
- debug.println("************************************");
- delay(250);
- #endif
- pinMode(rows[x], INPUT);
- delay(100);
- }
- for (int x = 0; x < colCount; x++) {
- #ifdef debugStart
- debug.print(x + 1);
- debug.print("/");
- debug.print(colCount);
- debug.print(": ");
- debug.print("Pin #"); debug.print(cols[x]); debug.println(" as input-pullup");
- debug.println("************************************");
- delay(250);
- #endif
- delay(100);
- pinMode(cols[x], INPUT_PULLUP);
- }
- }
- void scanMatrix()
- {
- for (int rowIndex=0; rowIndex < rowCount; rowIndex++) {
- for (int colIndex=0; colIndex < colCount; colIndex++) {
- int rx=keys[colIndex][rowIndex];
- debounceMatrix(colIndex, rowIndex, rx);
- }
- }
- }
- void readMatrix() {
- for (int colIndex = 0; colIndex < colCount; colIndex++) {
- // col: set to output to low
- int curCol = cols[colIndex];
- pinMode(curCol, OUTPUT);
- digitalWrite(curCol, LOW);
- // row: interate through the rows
- for (int rowIndex = 0; rowIndex < rowCount; rowIndex++) {
- int rowCol = rows[rowIndex];
- pinMode(rowCol, INPUT_PULLUP);
- keys[colIndex][rowIndex] = digitalRead(rowCol);
- //int rx=digitalRead(rowCol);
- pinMode(rowCol, INPUT);
- // debounceMatrix(colIndex, rowIndex, rx);
- }
- // disable the column
- pinMode(curCol, INPUT);
- }
- }
- void debounceMatrix(int colIndex, int rowIndex , int rx)
- {
- // if (rowCol != keys_prev[colIndex][rowIndex]) //If the rowCol (state) is different from last time scanned
- // {
- // If we have gone on to the next millisecond
- if(millis() != debounceTime[colIndex][rowIndex])
- {
- keys[colIndex][rowIndex] = rx;
- if(rx == keys_prev[colIndex][rowIndex] && debounceSampleCount[colIndex][rowIndex] > 0)
- {
- debounceSampleCount[colIndex][rowIndex]--;
- //debounceSampleCount[colIndex][rowIndex]=debounceSampleCount[colIndex][rowIndex]-1;
- }
- if(rx != keys_prev[colIndex][rowIndex])
- {
- debounceSampleCount[colIndex][rowIndex]++;
- //debounceSampleCount[colIndex][rowIndex]=debounceSampleCount[colIndex][rowIndex]+1;
- }
- // If the Input has shown the same value for long enough let's switch it
- if(debounceSampleCount[colIndex][rowIndex] >= debounceSampleMax)
- {
- debounceSampleCount[colIndex][rowIndex] = 0;
- keys_prev[colIndex][rowIndex] = rx;
- showMatrix(colIndex, rowIndex, rx);
- // debug.print("KEY STATE IS DIFFERENT: "); debug.print("current="); debug.print(rx); debug.print("//"); //ebug.print("last known="); debug.println(keys_prev[colIndex][rowIndex]);
- }
- debounceTime[colIndex][rowIndex] = millis();
- } // keys_prev[colIndex][rowIndex] = rowCol; //save the new state
- }
- void showMatrix(int colIndex, int rowIndex, int rx)
- {
- debug.print("column[");
- debug.print(colIndex);
- debug.print("] row[");
- debug.print(rowIndex);
- debug.print("]");
- debug.print(" = ");
- debug.println(rx);
- }
- void noteOn(int channel, int pitch, int velocity) {
- midiEventPacket_t noteOn = {0x09, 0x90 | channel, pitch, velocity};
- MidiUSB.sendMIDI(noteOn);
- MidiUSB.flush();
- debug.print("MIDI | NOTE ON: ");
- debug.print(pitch);
- debug.print(" / ");
- debug.println(pitch, HEX);
- }
- void noteOff(int channel, int pitch, int velocity) {
- midiEventPacket_t noteOff = {0x08, 0x80 | channel, pitch, velocity};
- MidiUSB.sendMIDI(noteOff);
- MidiUSB.flush();
- debug.print("MIDI | NOTE OFF: ");
- debug.print(pitch);
- debug.print(" / ");
- debug.println(pitch, HEX);
- }
- void loop() {
- readMatrix();
- scanMatrix();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement