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

History | View | Annotate | Download (8.8 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 13:69d60ae38d62 f
 *  Authors: Ben Bengler, Fiore Martin, Christopher Paton (christopher.paton@gmail.com)
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
#include <Encoder.h>
51
#include <Bounce.h>
52
53
///////////////////////////////////////////////////
54
//MIDI settings
55
const int midi_chan_inst1 = 1; // MIDI channel for Instrument 1
56
const int midi_chan_inst2 = 2; // MIDI channel for Instrument 2
57
58
const int cc_length = 1;    // MIDI cc controlling selection length
59 15:5241f96479d6 f
const int cc_duration = 2;  // MIDI cc controlling grain duration
60 1:b5bcad8e7803 f
const int cc_filter = 7;    // MIDI cc controlling LP filter
61
const int cc_play = 4;      // MIDI cc controlling PLAY
62
const int cc_record = 5;    // MIDI cc controlling RECORD
63
64
///////////////////////////////////////////////////
65
// Interface Inputs
66
67 13:69d60ae38d62 f
//Buttons
68 1:b5bcad8e7803 f
69
const int Pin_play1 = 20;       //B0
70
const int Pin_record1 = 21;     //B1
71
const int Pin_play2 = 23;       //B3
72
const int Pin_record2 = 24;     //B4
73
const int Pin_record1_led =  4; //D4
74
const int Pin_record2_led =  5; //D5
75
const int Pin_MIDIled =  6;
76
77
Bounce button1 = Bounce(Pin_play1, 5);
78
Bounce button2 = Bounce(Pin_record1, 5);
79
Bounce button3 = Bounce(Pin_play2, 5);
80
Bounce button4 = Bounce(Pin_record2, 5);
81
82
83 13:69d60ae38d62 f
//Encoders
84 1:b5bcad8e7803 f
Encoder Enc1 (0, 1);  //Encoder for section length on Wavejet 1
85
Encoder Enc2 (2, 3);  //Encoder for section length on Wavejet 2
86 13:69d60ae38d62 f
Encoder Enc3 (36, 37); //Encoder for duration of Wavejet 1 [granularisation effect]
87
Encoder Enc4 (18, 19); //Encoder for duration of Wavejet 2 [granularisation effect]
88
89 1:b5bcad8e7803 f
90
// Variables
91 13:69d60ae38d62 f
const int jitter_thresh = 10; // threshold value for analog INs to make up for input jitter
92 1:b5bcad8e7803 f
93
//Store recent values to detect parameter change
94
long Enc1_old = -999;
95
long Enc2_old = -999;
96 13:69d60ae38d62 f
97 14:9d7736aad6a3 f
long Enc3_old = -999;
98
long Enc4_old = -999;
99 1:b5bcad8e7803 f
100
uint16_t Jet1_old = 0;
101
int16_t Jet1_old_MIDI = -1;
102
uint16_t Jet2_old = 0;
103
int16_t Jet2_old_MIDI = -1;
104 13:69d60ae38d62 f
105 1:b5bcad8e7803 f
uint16_t filter1_old = 0;
106
int16_t filter1_old_MIDI = -1;
107
uint16_t filter2_old = 0;
108
int16_t filter2_old_MIDI = -1;
109
110 13:69d60ae38d62 f
uint16_t dur1_old = 0;
111
int16_t dur1_old_MIDI = -1;
112
uint16_t dur2_old = 0;
113
int16_t dur2_old_MIDI = -1;
114 1:b5bcad8e7803 f
115 13:69d60ae38d62 f
116
void setup()
117
{
118
  pinMode(Pin_play1, INPUT_PULLUP);
119
  pinMode(Pin_record1, INPUT_PULLUP);
120
  pinMode(Pin_play2, INPUT_PULLUP);
121
  pinMode(Pin_record2, INPUT_PULLUP);
122
123
124
  pinMode(Pin_MIDIled, OUTPUT);
125
  pinMode(Pin_record1_led, OUTPUT);
126
  pinMode(Pin_record2_led, OUTPUT);
127
}
128
129
void loop()
130
{
131 1:b5bcad8e7803 f
  digitalWrite(Pin_MIDIled, LOW);
132
  digitalWrite(Pin_record1_led, HIGH);
133
  digitalWrite(Pin_record2_led, HIGH);
134 13:69d60ae38d62 f
135 1:b5bcad8e7803 f
  button1.update();
136
  button2.update();
137
  button3.update();
138
  button4.update();
139
140 13:69d60ae38d62 f
  uint16_t Jet1_new = analogRead(0);      //read Wavejet/Rail 1; ADJUST INPUT RANGE ACCORDING TO SENSOR
141
  uint16_t Jet2_new = analogRead(1);      //read Wavejet/Rail 2; ADJUST INPUT RANGE ACCORDING TO SENSOR
142
  uint16_t filter1_new = analogRead(2);   //read filter Instrument 1; ADJUST INPUT RANGE ACCORDING TO SENSOR
143
  uint16_t filter2_new = analogRead(4);   //read filter Instrument 2; ADJUST INPUT RANGE ACCORDING TO SENSOR
144
  uint16_t dur1_new = analogRead(3);
145
  uint16_t dur2_new = analogRead(5);
146 1:b5bcad8e7803 f
147 13:69d60ae38d62 f
  //------ SEND INSTRUMENT 1 MIDI -----------
148 1:b5bcad8e7803 f
149 13:69d60ae38d62 f
  // Play button instrument 1
150
  if (button1.fallingEdge()){
151
   usbMIDI.sendControlChange(cc_play, 1, midi_chan_inst1);
152 1:b5bcad8e7803 f
  }
153 13:69d60ae38d62 f
154
  if (button1.risingEdge()){
155
   usbMIDI.sendControlChange(cc_play, 0, midi_chan_inst1);
156 1:b5bcad8e7803 f
  }
157
158 13:69d60ae38d62 f
159
  // Record button instrument 1
160
  if (button2.fallingEdge()){
161
   usbMIDI.sendControlChange(cc_record, 1, midi_chan_inst1);
162 1:b5bcad8e7803 f
  }
163
164 13:69d60ae38d62 f
  // Selection start position instrument 1
165
  // <calibrate>
166
  if (Jet1_new > Jet1_old+jitter_thresh || Jet1_new < Jet1_old-jitter_thresh){
167 1:b5bcad8e7803 f
168 13:69d60ae38d62 f
    int16_t midiVal = constrain( map(Jet1_new, 994, 121, 0, 149), 0, 149 );
169 1:b5bcad8e7803 f
    if( midiVal != Jet1_old_MIDI ){
170
      Jet1_old_MIDI = midiVal;
171
      usbMIDI.sendPitchBend( midiVal, midi_chan_inst1 );
172
    }
173
174
    Jet1_old = Jet1_new;
175
    digitalWrite(Pin_MIDIled, HIGH);
176 13:69d60ae38d62 f
  }
177 1:b5bcad8e7803 f
178 13:69d60ae38d62 f
  //Filter instrument 1
179
  // <calibrate>
180
  if ( filter1_new != filter1_old ) {
181 1:b5bcad8e7803 f
182 13:69d60ae38d62 f
    int16_t midiVal = constrain( map(filter1_new, 137, 734, 0, 127), 0, 127 );
183 1:b5bcad8e7803 f
    if( midiVal != filter1_old_MIDI){
184
      filter1_old_MIDI = midiVal;
185
      usbMIDI.sendControlChange(cc_filter, midiVal, midi_chan_inst1);
186
    }
187
188
    filter1_old = filter1_new;
189
    digitalWrite(Pin_MIDIled, HIGH);
190
  }
191 13:69d60ae38d62 f
192
  //Selection length instrument 1
193
  long Enc1_new = Enc1.read();
194
  Enc1_new = constrain(Enc1_new, 0, 127); //constrain to 7-bit MIDI range
195
196
  if (Enc1_new <= 0){ //Dynamic reset of counter to MIDI range
197
    Enc1.write(0);
198
  }
199
  else if (Enc1_new >= 127){
200
    Enc1.write(127);
201
  }
202
203 1:b5bcad8e7803 f
  if (Enc1_new != Enc1_old) {
204
    Enc1_old = Enc1_new;
205
    usbMIDI.sendControlChange(cc_length, Enc1_new, midi_chan_inst1);
206
    digitalWrite(Pin_MIDIled, HIGH);
207
  }
208 13:69d60ae38d62 f
209
  //Grain duration instrument 1
210
  long Enc3_new = Enc3.read();
211
  Enc3_new = constrain(Enc3_new, 0, 127); //constrain to 7-bit MIDI range
212 1:b5bcad8e7803 f
213 13:69d60ae38d62 f
  if (Enc3_new <= 0){
214
    Enc3.write(0);
215
  }
216
  else if (Enc3_new >= 127){
217
    Enc3.write(127);
218
  }
219
220 1:b5bcad8e7803 f
  if (Enc3_new != Enc3_old) {
221
    Enc3_old = Enc3_new;
222
    usbMIDI.sendControlChange(cc_duration, Enc3_new, midi_chan_inst1);
223
    digitalWrite(Pin_MIDIled, HIGH);
224
  }
225 13:69d60ae38d62 f
226
  //------ SEND INSTRUMENT 2 MIDI -----------
227 1:b5bcad8e7803 f
228
229 13:69d60ae38d62 f
  //Play button instrument 2
230
  if (button3.fallingEdge()) {
231 1:b5bcad8e7803 f
    usbMIDI.sendControlChange(cc_play, 1, midi_chan_inst2);
232
  }
233
234 13:69d60ae38d62 f
  if (button3.risingEdge()) {
235 1:b5bcad8e7803 f
    usbMIDI.sendControlChange(cc_play, 0, midi_chan_inst2);
236
  }
237
238 13:69d60ae38d62 f
  //Record button instrument 2
239 1:b5bcad8e7803 f
  if (button4.fallingEdge()) {
240
    usbMIDI.sendControlChange(cc_record, 1, midi_chan_inst2);
241
  }
242
243 13:69d60ae38d62 f
  // Selection start position instrument 2
244
  // <calibrate>
245 1:b5bcad8e7803 f
  if (Jet2_new > Jet2_old+jitter_thresh || Jet2_new < Jet2_old-jitter_thresh) {
246 13:69d60ae38d62 f
247
    int16_t midiVal = constrain( map( Jet2_new, 103, 993, 149, 0 ), 0, 149 );
248
249 1:b5bcad8e7803 f
    if( midiVal != Jet2_old_MIDI ){
250
      Jet2_old_MIDI = midiVal;
251
      usbMIDI.sendPitchBend( midiVal, midi_chan_inst2 );
252
    }
253
254
    Jet2_old = Jet2_new;
255
    digitalWrite(Pin_MIDIled, HIGH);
256
  }
257
258 13:69d60ae38d62 f
  //Filter instrument 2
259
  // <calibrate>
260
  if ( filter2_new != filter2_old ) {
261
262
    int16_t midiVal = constrain( map(filter2_new, 125, 684, 0, 127), 0, 127 );
263 1:b5bcad8e7803 f
    if( midiVal != filter2_old_MIDI){
264
      filter2_old_MIDI = midiVal;
265
      usbMIDI.sendControlChange(cc_filter, midiVal, midi_chan_inst2);
266
    }
267
268
    filter2_old = filter2_new;
269
    digitalWrite(Pin_MIDIled, HIGH);
270 13:69d60ae38d62 f
  }
271
272
  //Selection length instrument 2
273
  long Enc2_new = Enc2.read();
274
  Enc2_new = constrain(Enc2_new, 0, 127); //constrain to 7-bit MIDI range
275
276
  if (Enc2_new <= 0){ //Dynamic reset of counter to MIDI range
277
    Enc2.write(0);
278 1:b5bcad8e7803 f
  }
279 13:69d60ae38d62 f
  else if (Enc2_new >= 127){
280
    Enc2.write(127);
281
  }
282
283 1:b5bcad8e7803 f
  if (Enc2_new != Enc2_old) {
284
    Enc2_old = Enc2_new;
285
    usbMIDI.sendControlChange(cc_length, Enc2_new, midi_chan_inst2);
286
    digitalWrite(Pin_MIDIled, HIGH);
287
  }
288 13:69d60ae38d62 f
289
  //Grain duration instrument 2
290
  long Enc4_new = Enc4.read();
291
  Enc4_new = constrain(Enc4_new, 0, 127); //constrain to 7-bit MIDI range
292
293
  if (Enc4_new <= 0){
294
    Enc4.write(0);
295
  }
296
  else if (Enc4_new >= 127){
297
    Enc4.write(127);
298
  }
299 1:b5bcad8e7803 f
300
  if (Enc4_new != Enc4_old) {
301
    Enc4_old = Enc4_new;
302
    usbMIDI.sendControlChange(cc_duration, Enc4_new, midi_chan_inst2);
303
    digitalWrite(Pin_MIDIled, HIGH);
304
  }
305
306
}