To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.

Statistics Download as Zip
| Branch: | Tag: | Revision:

root / CollidoscopeTeensy / CollidoscopeTeensy_new.ino @ 11:76344930dd84

History | View | Annotate | Download (9.53 KB)

1 1:b5bcad8e7803 f
2
/******************************************************************************
3 5:75b744078d66 f
 *  Copyright (C) 2015  Ben Bengler (mail@benbengler.com)
4
 *  Copyright (C) 2016  Queen Mary University of London
5
 *  Authors: Ben Bengler, Fiore Martin
6 1:b5bcad8e7803 f
 *
7 5:75b744078d66 f
 *  This file is part of Collidoscope.
8
 *
9
 *  Collidoscope is free software: you can redistribute it and/or modify
10
 *  it under the terms of the GNU General Public License as published by
11
 *  the Free Software Foundation, either version 3 of the License, or
12
 *  (at your option) any later version.
13
 *
14
 *  This program is distributed in the hope that it will be useful,
15
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
 *  GNU General Public License for more details.
18
 *
19
 *  You should have received a copy of the GNU General Public License
20
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
21 1:b5bcad8e7803 f
 *
22 5:75b744078d66 f
 ****************************************************************************
23
 *
24 1:b5bcad8e7803 f
 *  Teensy 2 ++ pinout: https://www.pjrc.com/teensy/card4b.pdf
25
 *
26
 *  ANALOG INPUTS:
27
 *  Wavejet   -> F0 (38) [Horizontal rail 1]
28
 *  Wavejet   -> F1 (39) [Horizontal rail 2]
29
 *  Filter 1  -> F2 (40) [Vertical rail 1]
30
 *  Filter 2  -> F4 (42) [Vertical rail 2]
31
 *
32
 *  DIGITAL INPUTS [INTERRUPTS]:
33
 *  Sel. length 1  -> INT0/INT1 (0, 1)  [Encoder 1]
34
 *  Sel. length 2  -> INT2/INT3 (2, 3)  [Encoder 2]
35
 *  Duration 1     -> INT4/INT5 (36, 37)[Encoder 3]
36
 *  Duration 2     -> INT6/INT7 (18, 19)[Encoder 4]
37
 *
38
 *  DIGITAL INPUTS:
39
 *  Play1 toggle  -> B0 (20)
40
 *  Record1       -> B1 (21)
41
 *  Play2 toggle  -> B3 (22)
42
 *  Record2       -> B4 (24)
43
 *
44
 *  DIGITAL OUTPUTS:
45
 *  Record Button 1 Led -> D4 (4)
46
 *  Record Button 2 Led -> D5 (5)
47 5:75b744078d66 f
 *
48 1:b5bcad8e7803 f
 ******************************************************************************/
49
50
51
#include <Encoder.h>
52
#include <Bounce.h>
53
54
///////////////////////////////////////////////////
55
//MIDI settings
56
const int midi_chan_inst1 = 1; // MIDI channel for Instrument 1
57
const int midi_chan_inst2 = 2; // MIDI channel for Instrument 2
58
59
const int cc_length = 1;    // MIDI cc controlling selection length
60
const int cc_duration = 2;  // MIDI cc controlling duration
61
const int cc_filter = 7;    // MIDI cc controlling LP filter
62
const int cc_play = 4;      // MIDI cc controlling PLAY
63
const int cc_record = 5;    // MIDI cc controlling RECORD
64
//const int cc_reset = 100; // MIDI cc controlling instrument RESET
65
66
///////////////////////////////////////////////////
67
//Default Values:
68
const int  Enc_def = 64; //default selection length
69
int MIDI_led_state = LOW;
70
71
///////////////////////////////////////////////////
72
// Interface Inputs
73
74
//Buttons:
75
76
const int Pin_play1 = 20;       //B0
77
const int Pin_record1 = 21;     //B1
78
const int Pin_play2 = 23;       //B3
79
const int Pin_record2 = 24;     //B4
80
const int Pin_record1_led =  4; //D4
81
const int Pin_record2_led =  5; //D5
82
const int Pin_MIDIled =  6;
83
84
//const int Pin_reset1 = 22;    //B2, not in use
85
//const int Pin_reset2 = 25;    //B5, not in use
86
87
Bounce button1 = Bounce(Pin_play1, 5);
88
Bounce button2 = Bounce(Pin_record1, 5);
89
Bounce button3 = Bounce(Pin_play2, 5);
90
Bounce button4 = Bounce(Pin_record2, 5);
91
92
93
//Encoder
94
Encoder Enc1 (0, 1);  //Encoder for section length on Wavejet 1
95
Encoder Enc2 (2, 3);  //Encoder for section length on Wavejet 2
96
Encoder Enc3 (36, 37);  //Encoder for duration of Wavejet 1 [granularisation effect]
97
Encoder Enc4 (18, 19);  //Encoder for duration of Wavejet 2 [granularisation effect]
98
99
// Variables
100
const int jitter_thresh = 10; //7threshold value for analog INs to suppress sending MIDI due to input jitter
101
const int jitter_thresh_short = 5;
102
103
void setup() {
104
105
pinMode(Pin_play1, INPUT_PULLUP);
106
pinMode(Pin_record1, INPUT_PULLUP);
107
pinMode(Pin_play2, INPUT_PULLUP);
108
pinMode(Pin_record2, INPUT_PULLUP);
109
110
111
pinMode(Pin_MIDIled, OUTPUT);
112
pinMode(Pin_record1_led, OUTPUT);
113
pinMode(Pin_record2_led, OUTPUT);
114
}
115
116
//Store recent values to detect parameter change
117
long Enc1_old = -999;
118
long Enc2_old = -999;
119
long Enc3_old = -999;
120
long Enc4_old = -999;
121
122
uint16_t Jet1_old = 0;
123
int16_t Jet1_old_MIDI = -1;
124
uint16_t Jet2_old = 0;
125
int16_t Jet2_old_MIDI = -1;
126
uint16_t filter1_old = 0;
127
int16_t filter1_old_MIDI = -1;
128
uint16_t filter2_old = 0;
129
int16_t filter2_old_MIDI = -1;
130
131
void loop() {
132
133
  digitalWrite(Pin_MIDIled, LOW);
134
  digitalWrite(Pin_record1_led, HIGH);
135
  digitalWrite(Pin_record2_led, HIGH);
136
  button1.update();
137
  button2.update();
138
  button3.update();
139
  button4.update();
140
141
142 11:76344930dd84 f
  uint16_t Jet1_new = analogRead(0);      //read Wavejet/Rail 1; ADJUST INPUT RANGE ACCORDING TO SENSOR
143
  uint16_t Jet2_new = analogRead(1);      //read Wavejet/Rail 2; ADJUST INPUT RANGE ACCORDING TO SENSOR
144 1:b5bcad8e7803 f
  uint16_t filter1_new = analogRead(2);   //read filter Instrument 1; ADJUST INPUT RANGE ACCORDING TO SENSOR
145
  uint16_t filter2_new = analogRead(4);   //read filter Instrument 2; ADJUST INPUT RANGE ACCORDING TO SENSOR
146
147
148
149
  //Encoder 1 [Controls selection length of wave 1]
150
  long Enc1_new = Enc1.read();
151
  Enc1_new = constrain(Enc1_new, 0, 127); //constrain to 7-bit MIDI range
152
153
  //Dynamic reset of counter to MIDI range
154
  if (Enc1_new <= 0){
155
    Enc1.write(0);
156
  }
157
  else if (Enc1_new >= 127){
158
    Enc1.write(127);
159
  }
160
161
  //Encoder 2 [Controls selection length of wave 2]
162
  long Enc2_new = Enc2.read();
163
  Enc2_new = constrain(Enc2_new, 0, 127); //constrain to 7-bit MIDI range
164
165
  //Dynamic reset of counter to MIDI range
166
  if (Enc2_new <= 0){
167
    Enc2.write(0);
168
  }
169
  else if (Enc2_new >= 127){
170
    Enc2.write(127);
171
  }
172
173
  //Encoder 3 [Controls duration of wave 1]
174
  long Enc3_new = Enc3.read();
175
  Enc3_new = constrain(Enc3_new, 0, 127); //constrain to 7-bit MIDI range
176
177
  //Dynamic reset of counter to MIDI range
178
  if (Enc3_new <= 0){
179
    Enc3.write(0);
180
  }
181
  else if (Enc3_new >= 127){
182
    Enc3.write(127);
183
  }
184
185
  //Encoder 4 [Controls duration of wave 2]
186
  long Enc4_new = Enc4.read();
187
  Enc4_new = constrain(Enc4_new, 0, 127); //constrain to 7-bit MIDI range
188
189
  //Dynamic reset of counter to MIDI range
190
  if (Enc4_new <= 0){
191
    Enc4.write(0);
192
  }
193
  else if (Enc4_new >= 127){
194
    Enc4.write(127);
195
  }
196
197
198
 //Instrument 1 Controls//////////////////////////////////////
199
200 11:76344930dd84 f
 //Loop/Keymode Switch Instrument 1
201 1:b5bcad8e7803 f
 if (button1.risingEdge()) {
202
   usbMIDI.sendControlChange(cc_play, 1, midi_chan_inst1);
203
 }
204
205
 if (button1.fallingEdge()) {
206
   usbMIDI.sendControlChange(cc_play, 0, midi_chan_inst1);
207
 }
208
209
210
 //Record Instrument 1
211
 if (button2.fallingEdge()) {
212
   usbMIDI.sendControlChange(cc_record, 1, midi_chan_inst1);
213
 }
214
215
 //send MIDI Wavejet 1 [Position Instrument 1]
216 11:76344930dd84 f
 //<calibrate>
217 1:b5bcad8e7803 f
 if (Jet1_new > Jet1_old+jitter_thresh || Jet1_new < Jet1_old-jitter_thresh) {
218
219
    int16_t midiVal = constrain( map(Jet1_new, 988, 121, 0, 149), 0, 149 );
220
    if( midiVal != Jet1_old_MIDI ){
221
      Jet1_old_MIDI = midiVal;
222
      usbMIDI.sendPitchBend( midiVal, midi_chan_inst1 );
223
    }
224
225
    Jet1_old = Jet1_new;
226
    digitalWrite(Pin_MIDIled, HIGH);
227
 }
228
229
230
 //send MIDI Filter 1 [Filter Instrument 1]
231 11:76344930dd84 f
 //<calibrate>
232
 if ( filter1_new > filter1_old+jitter_thresh_short || filter1_new < filter1_old-jitter_thresh_short ) {
233 1:b5bcad8e7803 f
234
    int16_t midiVal = constrain( map(filter1_new, 145, 756, 0, 127), 0, 127 );
235
    if( midiVal != filter1_old_MIDI){
236
      //Serial.println( midiVal );
237
      filter1_old_MIDI = midiVal;
238
      usbMIDI.sendControlChange(cc_filter, midiVal, midi_chan_inst1);
239
    }
240
241
    filter1_old = filter1_new;
242
    digitalWrite(Pin_MIDIled, HIGH);
243
  }
244
245
  //send MIDI Encoder 1 [Selection length Instrument 1]
246
  if (Enc1_new != Enc1_old) {
247
    Enc1_old = Enc1_new;
248
    usbMIDI.sendControlChange(cc_length, Enc1_new, midi_chan_inst1);
249
    digitalWrite(Pin_MIDIled, HIGH);
250
  }
251
252
  //send MIDI Encoder 3 [Duration Instrument 1]
253
  if (Enc3_new != Enc3_old) {
254
    Enc3_old = Enc3_new;
255
    usbMIDI.sendControlChange(cc_duration, Enc3_new, midi_chan_inst1);
256
    digitalWrite(Pin_MIDIled, HIGH);
257
  }
258
259
260
  //Instrument 2 Controls//////////////////////////////////////
261
262
  //Loop/Keymode Switch Instrument 2
263
  if (button3.risingEdge()) {
264
    usbMIDI.sendControlChange(cc_play, 1, midi_chan_inst2);
265
  }
266
267
  if (button3.fallingEdge()) {
268
    usbMIDI.sendControlChange(cc_play, 0, midi_chan_inst2);
269
  }
270
271
  //Record Instrument 2
272
  if (button4.fallingEdge()) {
273
    usbMIDI.sendControlChange(cc_record, 1, midi_chan_inst2);
274
  }
275
276
  //send MIDI Wavejet 2 [Position Instrument 2]
277 11:76344930dd84 f
  // <calibrate>
278 1:b5bcad8e7803 f
  if (Jet2_new > Jet2_old+jitter_thresh || Jet2_new < Jet2_old-jitter_thresh) {
279
    int16_t midiVal = constrain( map( Jet2_new, 109, 992, 149, 0 ), 0, 149 );
280
    if( midiVal != Jet2_old_MIDI ){
281
      Jet2_old_MIDI = midiVal;
282
      usbMIDI.sendPitchBend( midiVal, midi_chan_inst2 );
283
    }
284
285
    Jet2_old = Jet2_new;
286
    digitalWrite(Pin_MIDIled, HIGH);
287
  }
288
289 11:76344930dd84 f
  //send MIDI Filter 2 [Filter Instrument 2]
290
  //<calibrate>
291
  if ( filter2_new > filter2_old+jitter_thresh_short || filter2_new < filter2_old-jitter_thresh_short ) {
292 1:b5bcad8e7803 f
    int16_t midiVal = constrain( map(filter2_new, 132, 700, 0, 127), 0, 127 );
293
    if( midiVal != filter2_old_MIDI){
294
      filter2_old_MIDI = midiVal;
295
      usbMIDI.sendControlChange(cc_filter, midiVal, midi_chan_inst2);
296
    }
297
298
    filter2_old = filter2_new;
299
    digitalWrite(Pin_MIDIled, HIGH);
300
  }
301
302
303
  //send MIDI Encoder 2 [Selection length Instrument 2]
304
  if (Enc2_new != Enc2_old) {
305
    Enc2_old = Enc2_new;
306
    usbMIDI.sendControlChange(cc_length, Enc2_new, midi_chan_inst2);
307
    digitalWrite(Pin_MIDIled, HIGH);
308
  }
309
310
  //send MIDI Encoder 4 [Duration Instrument 2]
311
  if (Enc4_new != Enc4_old) {
312
    Enc4_old = Enc4_new;
313
    usbMIDI.sendControlChange(cc_duration, Enc4_new, midi_chan_inst2);
314
    digitalWrite(Pin_MIDIled, HIGH);
315
  }
316
317
}