-
Notifications
You must be signed in to change notification settings - Fork 0
/
masterProg_v5.pde
476 lines (429 loc) · 12 KB
/
masterProg_v5.pde
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
/*
* Arduino Drum Controller MIDI Out
*
* Sends noteOns / noteOffs for conductive thread x's of position sensor
* on djembe controller, reads FSRs and sends controller data 70-77,
* reads piezo value
*
* algorithm for conversion of sensor data into BASS / TONE / SLAP sounds
*
* Latest Revision 5/2/2011
* Karen Wickert
*/
// define max and min digital inputs of the array of x's on the arduino
int minDigIn = 2;
int maxDigIn = 53;
// define max and min analog inputs of the FSRs
int minAnIn = 0;
int maxAnIn = 7;
// define analog input of the piezo
int piezoIn = 8;
// define digital inputs used by conductive x's
int inPin[54]; //initialized to max number of possible digital input pins
// MIDI note numbers
char note[54]; //initialized to max number of digital inputs
// state of digital input pin
int state[54]; //initialized to max number of digital inputs
// current state of digital input pin
int curState[54]; //initialized to max number of digital inputs
int isOn[54]; // bool arr of whether a note 'x' is currently pressed
// define analog inputs used by FSRs
int anPin[8]; //initialized to max number of FSRs
// controller numbers
char ctrl[8]; //initialized to max number of FSRs
// state of analog input pin
int value[8]; //initialized to max number of FSRs
// current state of analog input pin
int curValue[8]; //initialized to max number of FSRs
//define threshold value of FSR noise
int noiseFloor = 1;
// initialize temp values for FSR rounding
float tval = 0;
float tval2 = 0;
int tval3 = 0;
float remainder = 0;
int intCurVal = 0;
// initialize piezo value and related
int triggered = false;
int piezoVal = 0;
int _2piezoVal = 0;
int lastPiezoVal = 0;
int _2lastPiezoVal = 0;
int diff = 0;
int _2diff = 0;
int triggerThreshold = 2;
// initializing algorithm-related variables
int onCount = 0;
int numFSRhit = 0;
int FSRsum = 0;
int avgFSRval = 0;
float tavg = 0;
int tavg2 = 0;
float avgRemainder = 0;
int r = 0;
// TODO WITH PIEZO:
// (1) if piezo triggered, AND NOT FSR(s) triggered --> side of drum hit event caused
// use this data to tell what kind of djembe hit has been attempted;
// if side-hit first, then FSR(s) hit, then SLAP
// if side-hit basically coincident with FSR / surface hit,
// IF area = relatively small (only fingers), then TONE
// ELSE IF area = relatively large (whole hand / palm), then BASS
// Acts as a gating event for the rest of the events ; filters out noise
// Controls the range of noteout value/number sent out
// TODO WITH X's:
// Area mapping -> use to distinguish between hit types (tap / slap / bass / tone / side only / etc)
// Control the range of noteout value/number sent out
// TODO WITH FSRs:
// Velocity mapping -> control the velocity out of the final output note(s) triggered
void debug(String x)
{
//Serial.print(x);
return;
}
void initArrays()
{
// assigns values to all currently-used digital inputs for x's
for(int i = maxDigIn; i > (minDigIn-1); i--)
{
// assigns each inPin to its associated number
inPin[i] = i;
// assigns each note
// note: a value of 60 is Middle C in the MIDI spec
note[i] = i+35;
// initialize state and current state of each digital input pin
state[i] = LOW;
curState[i] = LOW;
}
// assigns values to all analog inputs used for FSRs
for(int i = maxAnIn - minAnIn; i >= 0; i--)
{
// assigns each anPin to its associated number
anPin[i] = i;
// assigns each anPin to one of the controller numbers 70-77
// These controllers are UNDEFINED in the MIDI spec
ctrl[i] = i+69;
// initialize state and current value of each analog input pin;
// range = (0-1023) for each of the FSRs
value[i] = 0;
curValue[i] = 0;
}
}
// function to send a MIDI noteOn (or effectively noteOff, if velocity==0)
// via serial / MIDI connection
void noteOn(char cmd, char data1, char data2)
{
Serial.print(cmd, BYTE);
Serial.print(data1, BYTE);
Serial.print(data2, BYTE);
}
// function to send a MIDI controller value
// via serial / MIDI connection
void ctrlOn(char cmd, char data1, char data2)
{
Serial.print(cmd, BYTE);
Serial.print(data1, BYTE);
Serial.print(data2, BYTE);
}
void setup()
{
// assigns values to all currently-used digital inputs for x's
for(int i = maxDigIn; i > (minDigIn-1); i--)
{
// set the states of the I/O pins - all are used as inputs, here
pinMode(inPin[i], INPUT);
}
initArrays();
// set MIDI baud rate as per MIDI spec
Serial.begin(31250);
//Serial.begin(9600); // for testing
// initial read to avoid triggering false note when program started
_2piezoVal = analogRead(piezoIn);
piezoVal = analogRead(piezoIn);
}
void loop()
{
// checks prev and current piezo val, calculates difference
_2lastPiezoVal = _2piezoVal;
lastPiezoVal = piezoVal;
_2piezoVal = piezoVal;
piezoVal = analogRead(piezoIn);
_2diff = _2piezoVal - _2lastPiezoVal;
diff = piezoVal - lastPiezoVal;
if(( abs(_2diff) < triggerThreshold ) && ( abs(diff) >= triggerThreshold ))
{
// piezo triggered
triggered = true;
}
if(triggered == true)
{
// reset values
triggered = false;
numFSRhit = 0;
FSRsum = 0;
avgFSRval = 0;
onCount = 0;
debug(111111);
debug('\n');
// go through state checks for each inPin (for each x)
for(int i = maxDigIn; i > (minDigIn-1); i--)
{
curState[i] = digitalRead(inPin[i]);
// if currently conducting and previous state was not conducting, noteOn!
if( curState[i] == HIGH && state[i] == LOW ) // x connected to foil, thus conducting +5v
{
// noteOn on channel 1 (0x90), note value, middle velocity (0x45):
//noteOn(0x90, note[i], 0x45);
//delay(100);
isOn[i] = true;
onCount++;
}
// if currently not conducting and previous state was conducting, noteOff!
if( curState[i] == LOW && state[i] == HIGH ) // x separated from foil, thus insulated
{
// noteOn on channel 1 (0x90), note value, silent velocity (0x00):
//noteOn(0x90, note[i], 0x00);
isOn[i] = false;
}
state[i] = curState[i]; // update stored state of that inPin after the state check
}
debug('\n');
debug(onCount);
debug('\n');
// go through value checks for each anPin (FSR)
for(int i = maxAnIn - minAnIn; i >= 0; i--)
{
curValue[i] = analogRead(anPin[i]);
if(curValue[i] > noiseFloor)
{
/* send MIDI controller data on channel 1 (0xB0),
controller number (1-8), controller value (0-127)
curValue is divided to change the range of the FSR
from 0-1023 to 0-127 to be compatible with the 0-127
range of MIDI controllers; rounding ensures that the
value is assi
gned properly, ie max value (at 1023) is
127 and no greater.
*/
tval = (float)curValue[i] / 1023;
tval2 = tval*127;
tval3 = (int)tval2;
remainder = tval2 - tval3;
if(remainder >= 0.5){
tval3+=1;
}
intCurVal = tval3;
//ctrlOn(0xB0, ctrl[i], intCurVal);
FSRsum += intCurVal;
numFSRhit++;
}
value[i] = curValue[i]; // update stored value of that anPin after the value check
// rounding function, completes operation:
avgFSRval = FSRsum/numFSRhit;
tavg = (float)avgFSRval / numFSRhit;
tavg2 = (int)tavg;
avgRemainder = tavg - tavg2;
if(avgRemainder >= 0.5){
tavg2+=1;
}
avgFSRval = tavg2;
}
debug(121212);
debug('\n');
// SLAP
if((onCount >= 0) && (onCount <= 7))
{
debug(4444444);
debug('\n');
// trigger a PP (pianissimo) sample
if((avgFSRval >= 1) && (avgFSRval < 43))
{
r = random(0,5);
switch(r)
{
case 0:
debug(440000);
noteOn(0x90, 0x42, avgFSRval);
break;
case 1:
debug(440001);
noteOn(0x90, 0x43, avgFSRval);
break;
case 2:
debug(440002);
noteOn(0x90, 0x44, avgFSRval);
break;
case 3:
debug(440003);
noteOn(0x90, 0x45, avgFSRval);
break;
case 4:
debug(440004);
noteOn(0x90, 0x46, avgFSRval);
break;
}
}
// trigger a MF (mezzoforte) sample
else if((avgFSRval >= 43) && (avgFSRval < 85))
{
r = random(0,5);
switch(r)
{
case 0:
debug(444000);
noteOn(0x90, 0x47, avgFSRval);
break;
case 1:
debug(444001);
noteOn(0x90, 0x48, avgFSRval);
break;
case 2:
debug(444002);
noteOn(0x90, 0x49, avgFSRval);
break;
case 3:
debug(444003);
noteOn(0x90, 0x4A, avgFSRval);
break;
case 4:
debug(444004);
noteOn(0x90, 0x4B, avgFSRval);
break;
}
}
// trigger a FF (fortissimo) sample
else if((avgFSRval >= 85) && (avgFSRval <= 127))
{
r = random(0,5);
switch(r)
{
case 0:
debug(444400);
noteOn(0x90, 0x4C, avgFSRval);
break;
case 1:
debug(444401);
noteOn(0x90, 0x4D, avgFSRval);
break;
case 2:
debug(444402);
noteOn(0x90, 0x4E, avgFSRval);
break;
case 3:
debug(444403);
noteOn(0x90, 0x4F, avgFSRval);
break;
case 4:
debug(444404);
noteOn(0x90, 0x50, avgFSRval);
break;
}
}
}
// BASS
else if(onCount > 7)
{
debug(222222);
debug('\n');
// trigger a PP (pianissimo) sample
if((avgFSRval >= 1) && (avgFSRval < 43))
{
r = random(0,5);
debug('r: ');
debug(r);
debug('\n');
switch(r)
{
case 0:
debug(220000);
noteOn(0x90, 0x24, avgFSRval);
break;
case 1:
debug(220001);
noteOn(0x90, 0x25, avgFSRval);
break;
case 2:
debug(220002);
noteOn(0x90, 0x26, avgFSRval);
break;
case 3:
debug(220003);
noteOn(0x90, 0x27, avgFSRval);
break;
case 4:
debug(220004);
noteOn(0x90, 0x28, avgFSRval);
break;
}
}
// trigger a MF (mezzoforte) sample
else if((avgFSRval >= 43) && (avgFSRval < 85))
{
r = random(0,5);
debug('r: ');
debug(r);
debug('\n');
switch(r)
{
case 0:
debug(222000);
noteOn(0x90, 0x29, avgFSRval);
break;
case 1:
debug(222001);
noteOn(0x90, 0x2A, avgFSRval);
break;
case 2:
debug(222002);
noteOn(0x90, 0x2B, avgFSRval);
break;
case 3:
debug(222003);
noteOn(0x90, 0x2C, avgFSRval);
break;
case 4:
debug(222004);
noteOn(0x90, 0x2D, avgFSRval);
break;
}
}
// trigger a FF (fortissimo) sample
else if((avgFSRval >= 85) && (avgFSRval <= 127))
{
r = random(0,5);
debug('r: ');
debug(r);
debug('\n');
switch(r)
{
case 0:
debug(222200);
noteOn(0x90, 0x2E, avgFSRval);
break;
case 1:
debug(222201);
noteOn(0x90, 0x2F, avgFSRval);
break;
case 2:
debug(222202);
noteOn(0x90, 0x30, avgFSRval);
break;
case 3:
debug(222203);
noteOn(0x90, 0x31, avgFSRval);
break;
case 4:
debug(222204);
noteOn(0x90, 0x32, avgFSRval);
break;
}
}
}
// TONE
else if(avgFSRval == 0)
{
// wait until you get an FSR value in, then play tone sample
debug(3333333);
debug('\n');
}
}
}