-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTeensy_MPR212_NuevoTactil.ino
269 lines (219 loc) · 8.93 KB
/
Teensy_MPR212_NuevoTactil.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
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
/*********************************************************
This is a library for the MPR121 12-channel Capacitive touch sensor
Designed specifically to work with the MPR121 Breakout in the Adafruit shop
----> https://www.adafruit.com/products/
These sensors use I2C communicate, at least 2 pins are required
to interface
Adafruit invests time and resources providing this open source code,
please support Adafruit and open-source hardware by purchasing
products from Adafruit!
Written by Limor Fried/Ladyada for Adafruit Industries.
BSD license, all text above must be included in any redistribution
**********************************************************/
#include <Wire.h>
#include "Adafruit_MPR121.h"
#include <MIDI.h>
MIDI_CREATE_INSTANCE(HardwareSerial, Serial2, MIDI);
#ifndef _BV
#define _BV(bit) (1 << (bit))
#endif
#ifndef _BV2
#define _BV2(bit) (1 << (bit))
#endif
#ifndef _BV3
#define _BV3(bit) (1 << (bit))
#endif
#ifndef _BV4
#define _BV4(bit) (1 << (bit))
#endif
// You can have up to 4 on one i2c bus but one is enough for testing!
Adafruit_MPR121 cap = Adafruit_MPR121();
Adafruit_MPR121 cap2 = Adafruit_MPR121();
Adafruit_MPR121 cap3 = Adafruit_MPR121();
Adafruit_MPR121 cap4 = Adafruit_MPR121();
// Keeps track of the last pins touched
// so we know when buttons are 'released'
uint16_t lasttouched = 0;
uint16_t currtouched = 0;
uint16_t lasttouched2 = 0;
uint16_t currtouched2 = 0;
uint16_t lasttouched3 = 0;
uint16_t currtouched3 = 0;
uint16_t lasttouched4 = 0;
uint16_t currtouched4 = 0;
int notes[48] = {65, 67, 68, 70, 64, 66, 67, 69, 58, 60, 61, 63, // Orden correcto en filas.
70, 68, 66, 65, 69, 67, 65, 64, 62, 60, 59, 57, // OK
84, 82, 80, 79, 77, 75, 73, 72, 76, 74, 72, 71, // 79, 80, 82, 84, 73, 74, 75, 76, 77, 78, 79, 80,
83, 81, 79, 78, 77, 75, 73, 72, 76, 74, 72, 71}; // OK
int channel = 3;
/// POTS
const int N_POTS = 1; //* total numbers of pots (slide & rotary)
const int POT_ARDUINO_PIN[N_POTS] = {A6}; //* pins of each pot connected straight to the Arduino
int potCState[N_POTS] = { 0 }; // Current state of the pot
int potPState[N_POTS] = { 0 }; // Previous state of the pot
int potVar = 0; // Difference between the current and previous state of the pot
int pbCState = 352;
int pbPState = 352;
int pbVar = 0;
int midiCState[N_POTS] = { 0 }; // Current state of the midi value
int midiPState[N_POTS] = { 0 }; // Previous state of the midi value
const int TIMEOUT = 300; //* Amount of time the potentiometer will be read after it exceeds the varThreshold
const int varThreshold = 100; //* Threshold for the potentiometer signal variation
boolean potMoving = true; // If the potentiometer is moving
boolean pbMoving = true;
unsigned long PTime[N_POTS] = { 0 }; // Previously stored time
unsigned long timer[N_POTS] = { 0 }; // Stores the time that has elapsed since the timer was reset
unsigned long PbTime = 0; // Previously stored time pitch bend
unsigned long pbtimer = 0; // Stores the time that has elapsed since the timer was reset pitch bend
byte cc[N_POTS] = {1}; // 1 para MODWHEEL 7 volumen
byte midiCh[N_POTS] = {channel}; // 0x10, 1
void setup() {
//Serial.begin(115200);
MIDI.begin(MIDI_CHANNEL_OMNI);
/*
while (!Serial) { // needed to keep leonardo/micro from starting too fast!
delay(10);
}
*/
Serial.println("Adafruit MPR121 Capacitive Touch sensor test");
// Default address is 0x5A, if tied to 3.3V its 0x5B
// If tied to SDA its 0x5C and if SCL then 0x5D
if (!cap.begin(0x5A)) {
Serial.println("MPR121 1 not found, check wiring?");
while (1);
}
Serial.println("MPR121 found!");
if (!cap2.begin(0x5B)) {
Serial.println("MPR121 2 not found, check wiring?");
while (1);
}
Serial.println("MPR121 2 found!");
if (!cap3.begin(0x5C)) {
Serial.println("MPR121 3 not found, check wiring?");
while (1);
}
Serial.println("MPR121 3 found!");
if (!cap4.begin(0x5D)) {
Serial.println("MPR121 4 not found, check wiring?");
while (1);
}
Serial.println("MPR121 4 found!");
}
void loop() {
// Get the currently touched pads
currtouched = cap.touched();
currtouched2 = cap2.touched();
currtouched3 = cap3.touched();
currtouched4 = cap4.touched();
for (uint8_t i = 0; i < 12; i++) {
// it if *is* touched and *wasnt* touched before, alert!
if ((currtouched & _BV(i)) && !(lasttouched & _BV(i)) ) {
//Serial.print(i); Serial.println(" touched");
usbMIDI.sendNoteOn(notes[i], 127, channel);
MIDI.sendNoteOn(notes[i], 127, channel);
}
// if it *was* touched and now *isnt*, alert!
if (!(currtouched & _BV(i)) && (lasttouched & _BV(i)) ) {
//Serial.print(i); Serial.println(" released");
usbMIDI.sendNoteOn(notes[i], 0, channel);
MIDI.sendNoteOn(notes[i], 0, channel);
}
}
for (uint8_t i = 0; i < 12; i++) {
// it if *is* touched and *wasnt* touched before, alert!
if ((currtouched2 & _BV2(i)) && !(lasttouched2 & _BV2(i)) ) {
//Serial.print(i); Serial.println(" touched");
usbMIDI.sendNoteOn(notes[i+12], 127, channel);
MIDI.sendNoteOn(notes[i+12], 127, channel);
}
// if it *was* touched and now *isnt*, alert!
if (!(currtouched2 & _BV2(i)) && (lasttouched2 & _BV2(i)) ) {
//Serial.print(i); Serial.println(" released");
usbMIDI.sendNoteOn(notes[i+12], 0, channel);
MIDI.sendNoteOn(notes[i+12], 0, channel);
}
}
for (uint8_t i = 0; i < 12; i++) {
// it if *is* touched and *wasnt* touched before, alert!
if ((currtouched3 & _BV3(i)) && !(lasttouched3 & _BV3(i)) ) {
//Serial.print(i); Serial.println(" touched");
usbMIDI.sendNoteOn(notes[i+24], 127, channel);
MIDI.sendNoteOn(notes[i+24], 127, channel);
}
// if it *was* touched and now *isnt*, alert!
if (!(currtouched3 & _BV3(i)) && (lasttouched3 & _BV3(i)) ) {
//Serial.print(i); Serial.println(" released");
usbMIDI.sendNoteOn(notes[i+24], 0, channel);
MIDI.sendNoteOn(notes[i+24], 0, channel);
}
}
for (uint8_t i = 0; i < 12; i++) {
// it if *is* touched and *wasnt* touched before, alert!
if ((currtouched4 & _BV4(i)) && !(lasttouched4 & _BV4(i)) ) {
//Serial.print(i); Serial.println(" touched");
usbMIDI.sendNoteOn(notes[i+36], 127, channel);
MIDI.sendNoteOn(notes[i+36], 127, channel);
}
// if it *was* touched and now *isnt*, alert!
if (!(currtouched4 & _BV4(i)) && (lasttouched4 & _BV4(i)) ) {
//Serial.print(i); Serial.println(" released");
usbMIDI.sendNoteOn(notes[i+36], 0, channel);
MIDI.sendNoteOn(notes[i+36], 0, channel);
}
}
// reset our state
lasttouched = currtouched;
lasttouched2 = currtouched2;
lasttouched3 = currtouched3;
lasttouched4 = currtouched4;
potentiometers();
// comment out this line for detailed data from the sensor!
return;
// debugging info, whatddddd
Serial.print("\t\t\t\t\t\t\t\t\t\t\t\t\t 0x"); Serial.println(cap.touched(), HEX);
Serial.print("Filt: ");
for (uint8_t i = 0; i < 12; i++) {
Serial.print(cap.filteredData(i)); Serial.print("\t");
}
Serial.println();
Serial.print("Base: ");
for (uint8_t i = 0; i < 12; i++) {
Serial.print(cap.baselineData(i)); Serial.print("\t");
}
Serial.println();
// put a delay so it isn't overwhelming
delay(100);
}
void potentiometers() {
for (int i = 0; i < N_POTS; i++) { // Loops through all the potentiometers
potCState[i] = analogRead(POT_ARDUINO_PIN[i]); // reads the pins from arduino
midiCState[i] = map(potCState[i], 0, 1023/4, 127, 0); //4095 Maps the reading of the potCState to a value usable in midi
potVar = abs(potCState[i] - potPState[i]); // Calculates the absolute value between the difference between the current and previous state of the pot
if (potVar > varThreshold) { // Opens the gate if the potentiometer variation is greater than the threshold
PTime[i] = millis(); // Stores the previous time
}
timer[i] = millis() - PTime[i]; // Resets the timer 11000 - 11000 = 0ms
if (timer[i] < TIMEOUT) { // If the timer is less than the maximum allowed time it means that the potentiometer is still moving
potMoving = true;
} else {
potMoving = false;
}
if (potMoving == true) { // If the potentiometer is still moving, send the change control
if (midiPState[i] != midiCState[i]) {
if (midiCState[i] < 0) {
midiCState[i] = 0;
}
if (midiCState[i] > 127) {
midiCState[i] = 127;
}
usbMIDI.sendControlChange(cc[i], midiCState[i], midiCh[i]);
MIDI.sendControlChange(cc[i], midiCState[i], midiCh[i]);
potPState[i] = potCState[i]; // Stores the current reading of the potentiometer to compare with the next
midiPState[i] = midiCState[i];
//Serial.print(midiCState[i]);
//Serial.print(" - ");
//Serial.println(potCState[i]);
}
}
}
}