-
Notifications
You must be signed in to change notification settings - Fork 7
/
midiMakey2-8
223 lines (194 loc) · 8.28 KB
/
midiMakey2-8
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
//original working code with working sustain and octave switching, and beginnings of rotary encoder code (commented out)
//Sustain working well, octaves slightly fidgety (sometimes jumps multiple octaves at a time)
//Pins 43, 44, 45 removed from pinNumbers array to be used for rotary encoder pins
int bounceThreshold = 0; // a press must last this many loop cycles (about 20ms each) before triggering.
// higher values make it less sensitive, fewer false triggers, higher latency
#define NUM_INPUTS 44
int channel = 1;
// teensy makey pin numbers, pins 43, 44, 45 removed
int pinNumbers[NUM_INPUTS] = {
0,1,2,3,4,5,7,8,9,10,11,12,13,14,15,16,17, // left side starting at USB connector, pin D0, skipping D6
26,25,24,23,22,21,20,19,18,38,39,40,41,42, // right side starting at USB connector, pin B6, skipping gnd and aref
36,37,32,33,34,35,28,29,30,31 // these are "interior" through-holes, in the center of the board
};
int bounceCounter[NUM_INPUTS];
boolean pressed[NUM_INPUTS];
int v = 0;
void setup(){
for (int i=0; i<NUM_INPUTS; i++) {
bounceCounter[i] = 0;
pressed[i] = false;
}
}
void loop() {
for (int i=0; i<NUM_INPUTS-3; i++) { // for each pin
int c = readCapacitivePin(pinNumbers[i]); // check capacitance
Serial.print(c);
// Serial.print("\t");
if (c>1){ // if we detect a touch on the pin
if (!pressed[i]) { // and if we're not already pressed
bounceCounter[i]++; // increment the bounce counter
if(bounceCounter[i] > bounceThreshold){ // if we're over the bounce threshold
if(pinNumbers[i]==0){ // i==_ value specifies pin for SUSTAIN control
usbMIDI.sendControlChange(64,127,177); //send on message to SUSTAIN CC
pressed[i] = true; // remember it was pressed
//bounceCounter[i]=0;
}
else if(pinNumbers[i]==1){ // i==_ value specifies pin for OCTAVE +
if(v<30){ //contrains octave range to +3 max
v+=12; //adds 12 to all subsequent note values
}
}
else if(pinNumbers[i]==2){ // i==_ value specifies pin for OCTAVE -
if(v>-30){ //contrains octave range to -3 max
v-=12; //sutracts 12 from all subsequent note values
}
}
else{ //if no hard coded values are recognized
usbMIDI.sendNoteOn(60+i+v,127,channel); // send a MIDI note on
pressed[i] = true; // remember it was pressed
Serial.print(v);
bounceCounter[i]=0; // reset the bounce counter
}
}
}
}
else { // if we don't a detect touch on the pin
if (pressed[i]) { // if this key was pressed before
if(pinNumbers[i]==0){ // if the sustain key was pressed
usbMIDI.sendControlChange(64,0,177); //turn it off
pressed[i] = false; //it is no longer pressed
bounceCounter[i] = 0;}
else{
usbMIDI.sendNoteOff(60+i+v,127,channel); // send a MIDI note off
pressed[i] = false; // remember we are not pressed
bounceCounter[i] = 0; // reset the bounce counter
}
}
}
}
//Serial.println(" ");
//Rotary encoder
//if current value is different that last value
//if this value is 1 more or 2 less than last value, CC++
//if this value is 1 less or 2 more than last value, CC--
/*
int last = 0;
int velocity = 64; //testing with PAN, defaults at 64
for(int j = 43; j < 46; j++){ //ROTARY ENCODER LOOP, 43 44 45 are top right pins on teensy
int c = readCapacitivePin(pinNumbers[j]);
if (c>1)
bounceCounter[j]++; // increment the bounce counter
if(bounceCounter[j] > bounceThreshold){ //test against thresh
if(j!=last){ //only detects changes (replaces bool pressed)
if(j-1==last || j+2==last){ //indicates 'clockwise' movement
velocity+=15;
usbMIDI.sendControlChange(42,velocity,177); //send CC with new velocity
last = j; //remember last pin triggered
}
else if(j+1==last || j-2==last){ //indicates counterclockwise motion
velocity-=15;
usbMIDI.sendControlChange(64,velocity,177); //send CC with new velocity
last = j;
*/
}
}
}
}
}
// CapacitiveSensor tutorial from http://www.arduino.cc/playground/Code/CapacitiveSensor
// readCapacitivePin
// Input: Arduino pin number
// Output: A number, from 0 to 17 expressing
// how much capacitance is on the pin
// When you touch the pin, or whatever you have
// attached to it, the number will get higher
uint8_t readCapacitivePin(int pinToMeasure) {
// Variables used to translate from Arduino to AVR pin naming
volatile uint8_t* port;
volatile uint8_t* ddr;
volatile uint8_t* pin;
// Here we translate the input pin number from
// Arduino pin number to the AVR PORT, PIN, DDR,
// and which bit of those registers we care about.
byte bitmask;
port = portOutputRegister(digitalPinToPort(pinToMeasure));
ddr = portModeRegister(digitalPinToPort(pinToMeasure));
bitmask = digitalPinToBitMask(pinToMeasure);
pin = portInputRegister(digitalPinToPort(pinToMeasure));
// Discharge the pin first by setting it low and output
*port &= ~(bitmask);
*ddr |= bitmask;
delay(1);
// Make the pin an input with the internal pull-up on
*ddr &= ~(bitmask);
*port |= bitmask;
// Now see how long the pin to get pulled up. This manual unrolling of the loop
// decreases the number of hardware cycles between each read of the pin,
// thus increasing sensitivity.
uint8_t cycles = 17;
if (*pin & bitmask) {
cycles = 0;
}
else if (*pin & bitmask) {
cycles = 1;
}
else if (*pin & bitmask) {
cycles = 2;
}
else if (*pin & bitmask) {
cycles = 3;
}
else if (*pin & bitmask) {
cycles = 4;
}
else if (*pin & bitmask) {
cycles = 5;
}
else if (*pin & bitmask) {
cycles = 6;
}
else if (*pin & bitmask) {
cycles = 7;
}
else if (*pin & bitmask) {
cycles = 8;
}
else if (*pin & bitmask) {
cycles = 9;
}
else if (*pin & bitmask) {
cycles = 10;
}
else if (*pin & bitmask) {
cycles = 11;
}
else if (*pin & bitmask) {
cycles = 12;
}
else if (*pin & bitmask) {
cycles = 13;
}
else if (*pin & bitmask) {
cycles = 14;
}
else if (*pin & bitmask) {
cycles = 15;
}
else if (*pin & bitmask) {
cycles = 16;
}
// Discharge the pin again by setting it low and output
// It's important to leave the pins low if you want to
// be able to touch more than 1 sensor at a time - if
// the sensor is left pulled high, when you touch
// two sensors, your body will transfer the charge between
// sensors.
*port &= ~(bitmask);
*ddr |= bitmask;
return cycles;
}
//Rotary encoder
//if current value is different that last value
//if this value is 1 more or 2 less than last value, CC++
//if this value is 1 less or 2 more than last value, CC--