-
Notifications
You must be signed in to change notification settings - Fork 0
/
SMidi.ino
122 lines (111 loc) · 3.74 KB
/
SMidi.ino
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
// SMidi Arduino source
//
// Project home:https://hackaday.io/project/25561
// Author: https://hackaday.io/daren
// Creative Commons License exists for this work. You may copy and alter the content
// of this file for private use only, and distribute it only with the associated
// Blooming Rose content. This license must be included with the file and content.
// This work is licensed under the Creative Commons Attribution 2.0 Generic License. To view a copy of this license
// visit http://creativecommons.org/licenses/by/2.0/ or send a letter to Creative Commons, PO Box 1866, Mountain View, CA 94042, USA.
#include <ResponsiveAnalogRead.h>
const int pinCount = 16;
const int velocityThreshold = 65;
const int afterThreshold = 75;
const int velocityTime = 55;
const int afterTime = 200;
const int defaultMax = 150;
const float chargeTime = 1; //rise time for external capacitors in us.
ResponsiveAnalogRead pinReader[pinCount] = {
ResponsiveAnalogRead(A0,true),
ResponsiveAnalogRead(A1,true),
ResponsiveAnalogRead(A2,true),
ResponsiveAnalogRead(A3,true),
ResponsiveAnalogRead(A4,true),
ResponsiveAnalogRead(A5,true),
ResponsiveAnalogRead(A6,true),
ResponsiveAnalogRead(A7,true),
ResponsiveAnalogRead(A8,true),
ResponsiveAnalogRead(A9,true),
ResponsiveAnalogRead(A15,true),
ResponsiveAnalogRead(A16,true),
ResponsiveAnalogRead(A17,true),
ResponsiveAnalogRead(A18,true),
ResponsiveAnalogRead(A19,true),
ResponsiveAnalogRead(A20,true)
};
int pin[] = {A0,A1,A2,A3,A4,A5,A6,A7,A8,A9,A15,A16,A17,A18,A19,A20};
int note[] = {60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76}; //MIDI notes
const int midiMin = 10;
int pinMin[pinCount];
int pinMax[pinCount];
int pinTimer[pinCount];
int velocityMask[pinCount];
void setup() {
//Serial.begin (32500);
Serial.begin (115200);
analogReadResolution(10);
//analogReadAveraging(4);
// Set baseline capacitance for offset. Read 20, average, set.
for (int c = 0; c < 20; c++ ) {
for (int i = 0; i < pinCount; i++) {
pinMode(pin[i],INPUT_PULLUP);
delayMicroseconds(chargeTime);
pinReader[i].update();
pinMode(pin[i],OUTPUT);
digitalWrite(pin[i],LOW);
delayMicroseconds(20);
}
}
for (int i = 0; i < pinCount; i++) {
pinMin[i] = pinReader[i].getValue();
pinMax[i] = defaultMax;
}
}
void loop () {
for (int i = 0; i < pinCount; i++) {
pinMode(pin[i],INPUT_PULLUP);
delayMicroseconds(chargeTime);
pinReader[i].update();
int value = pinMin[i] - pinReader[i].getValue();
pinMode(pin[i],OUTPUT);
digitalWrite(pin[i],LOW);
if (value > velocityThreshold) {
pinTimer[i] ++;
if ( value > pinMax[i] ) {
pinMax[i] = value;
}
if (!(velocityMask[i] & (1 << i)) && (pinTimer[i] == velocityTime)) {
velocityMask[i] |= (1 << i);
pinTimer [i] = 0;
int velocity = map (value, 0, pinMax[i], midiMin, 127);
usbMIDI.sendNoteOn (note[i], velocity, 1);
Serial.print("NoteOn: ");
Serial.print(note[i]);
Serial.print(" : ");
Serial.println(velocity);
}
if (pinTimer [i] == afterTime) {
pinTimer [i] = 0;
if (value > afterThreshold) {
int aftertouch = map (value, 0, pinMax[i], midiMin, 127);
usbMIDI.sendPolyPressure (note[i], aftertouch, 1);
Serial.print("PolyPressure: ");
Serial.print(note[i]);
Serial.print(" : ");
Serial.println(aftertouch);
}
}
}
else {
if (velocityMask[i] & (1 << i)) {
velocityMask[i] &= ~ (1 << i);
usbMIDI.sendNoteOff (note[i], 0, 1);
Serial.print("NoteOff: ");
Serial.print(note[i]);
Serial.print(" : ");
Serial.println("0");
pinTimer [i] = 0;
}
}
}
}