-
Notifications
You must be signed in to change notification settings - Fork 775
/
Copy pathCapTouchMIDIDrums.ino
122 lines (107 loc) · 3.09 KB
/
CapTouchMIDIDrums.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
// SPDX-FileCopyrightText: 2020 Carter Nelson for Adafruit Industries
//
// SPDX-License-Identifier: MIT
////////////////////////////////////////////////////////////////////////////
// Circuit Playground Capacitive Touch USB MIDI Drums
//
// Send MIDI percussion commands over USB.
// https://www.arduino.cc/en/Reference/MIDIUSB
// https://en.wikipedia.org/wiki/General_MIDI#Percussion
//
// Author: Carter Nelson
// MIT License (https://opensource.org/licenses/MIT)
#include <Adafruit_CircuitPlayground.h>
#include "MIDIUSB.h"
#define CAP_THRESHOLD 50
#define DEBOUNCE 100
uint8_t pads[] = {3, 2, 0, 1, 12, 6, 9, 10};
uint8_t numberOfPads = sizeof(pads)/sizeof(uint8_t);
////////////////////////////////////////////////////////////////////////////
void takeAction(uint8_t pad) {
Serial.print("PAD "); Serial.print(pad); Serial.print(". Sending MIDI: ");
switch (pad) {
case 3:
Serial.println("36");
drumHit(36);
break;
case 2:
Serial.println("37");
drumHit(37);
break;
case 0:
Serial.println("38");
drumHit(38);
break;
case 1:
Serial.println("39");
drumHit(39);
break;
case 12:
Serial.println("40");
drumHit(40);
break;
case 6:
Serial.println("41");
drumHit(41);
break;
case 9:
Serial.println("42");
drumHit(42);
break;
case 10:
Serial.println("43");
drumHit(43);
break;
default:
Serial.println("THIS SHOULD NEVER HAPPEN.");
}
}
////////////////////////////////////////////////////////////////////////////
boolean capButton(uint8_t pad) {
// Check if capacitive touch exceeds threshold.
if (CircuitPlayground.readCap(pad) > CAP_THRESHOLD) {
return true;
} else {
return false;
}
}
////////////////////////////////////////////////////////////////////////////
void noteOn(byte channel, byte pitch, byte velocity) {
// Send a MIDI Note On command.
midiEventPacket_t noteOn = {0x09, 0x90 | channel, pitch, velocity};
MidiUSB.sendMIDI(noteOn);
}
////////////////////////////////////////////////////////////////////////////
void noteOff(byte channel, byte pitch, byte velocity) {
// Send a MIDI Note Off command.
midiEventPacket_t noteOff = {0x08, 0x80 | channel, pitch, velocity};
MidiUSB.sendMIDI(noteOff);
}
////////////////////////////////////////////////////////////////////////////
void drumHit(byte drum) {
// Send Note On/Off command for given drum (note) number.
noteOn(9, drum, 125);
MidiUSB.flush();
noteOff(9, drum, 0);
MidiUSB.flush();
}
////////////////////////////////////////////////////////////////////////////
void setup() {
// Initialize serial.
Serial.begin(9600);
// Initialize Circuit Playground library.
CircuitPlayground.begin();
}
////////////////////////////////////////////////////////////////////////////
void loop() {
// Loop over every pad.
for (int i=0; i<numberOfPads; i++) {
// Check if pad is touched.
if (capButton(pads[i])) {
// Do something.
takeAction(pads[i]);
// But not too often.
delay(DEBOUNCE);
}
}
}