1#include <MIDIUSB.h>
2#include <ArduinoQueue.h>
3
4
10
11
12const byte KeyboardSize = 30;
13const byte StartNotePin = 2;
14
15const byte NoteOff = B10000000;
16const byte NoteOn = B10010000;
17
18const byte MidiChannel = 3;
19const byte LowestNote = 24;
20
21const byte KeyPin [KeyboardSize] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29};
22
23
24unsigned long KeyReadTime = 0;
25unsigned long DebounceTime = 20;
26unsigned long LastMIDISentTime;
27unsigned long MidiLag = 50;
28
29byte NoteCount = 0;
30byte NoteCountOld = 0;
31byte NotesChanged = 0;
32byte Velocity;
33
34bool KeyState[KeyboardSize] = {true};
35bool NoteState[KeyboardSize] [3] = {false};
36unsigned long KeyLastTime [KeyboardSize] = {0};
37
38
39ArduinoQueue<midiEventPacket_t> MidiChannelNoteQueue(20);
40
41
42void setup () {
43delay(1000);
44
45Serial.begin(31250);
46 for (int i = 0; i < KeyboardSize; i++) {
47 pinMode((StartNotePin+i), INPUT_PULLUP);
48 }
49}
50
51
52void loop () {
53
54KeyRead();
55NoteRead();
56NoteSend();
57TransmitMIDI();
58
59}
60
61
62void KeyRead() {
63KeyReadTime = millis();
64 for (int i = 0; i < KeyboardSize; i++) {
65 KeyState[i] = !digitalRead(KeyPin[i]+StartNotePin);
66 }
67}
68
69
70void NoteRead() {
71NoteCountOld = NoteCount;
72NotesChanged = 0;
73 for (int i = 0; i < KeyboardSize; i++ ) {
74 if ((KeyReadTime - KeyLastTime [i]) > DebounceTime) {
75 KeyLastTime [i] = KeyReadTime;
76 NoteState[i][3] = false;
77 NoteState[i][2] = NoteState[i][1];
78 if (((KeyState[i] == false) && (NoteState[i][1] == true)) || ((KeyState[i] == true) && (NoteState[i][1] == false))){
79 NoteState[i][1] = KeyState[i];
80 NoteState[i][3] = true;
81 NotesChanged++;
82 if (NoteState[i][1] == true) {
83 NoteCount++;
84 }
85 else {NoteCount--;}
86 }
87 }
88 }
89}
90
98
99
100void NoteSend() {
101if (NotesChanged > 0) {
102 for (int i = 0; i < (KeyboardSize); i++) {
103 if (NoteState[i][3] == true){
104 if (NoteState[i][1] == true){
105 sendNoteOn((i+ LowestNote), MidiChannel, 127);
106 }
107 else {
108 sendNoteOff((i+ LowestNote), MidiChannel, 0);}
109 }
110 }
111 }
112}
113
114
115void sendNoteOn (byte Pitch, byte MidiChannel, byte Velocity) {
116 MIDImessage(NoteOn, MidiChannel, Pitch, Velocity);
117}
118
119
120void sendNoteOff (byte Pitch, byte MidiChannel, byte Velocity) {
121 MIDImessage(NoteOff, MidiChannel, Pitch, Velocity);
122}
123
124
125void MIDImessage(int byte1, int byte2, int byte3, int byte4) {
126
127 byte cmd = (byte1 + byte2);
128 Serial.write(cmd);
129 Serial.write(byte3);
130 Serial.write(byte4);
131 midiEventPacket_t midiMsg = {cmd >> 4, cmd, byte3, byte4};
132 MidiChannelNoteQueue.enqueue(midiMsg);
133}
134
135
136void TransmitMIDI() {
137if ((micros() - LastMIDISentTime) > MidiLag) {
138 if (!MidiChannelNoteQueue.isEmpty()) {
139 MidiUSB.sendMIDI(MidiChannelNoteQueue.dequeue());
140 MidiUSB.flush();
141 LastMIDISentTime = micros();
142 }
143 }
144}
145
iloveaang
2 years ago
hey this is assume my dad had a similar project 20 or so years a go. keep up the good work from noah