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 @ 1:b5bcad8e7803

History | View | Annotate | Download (9.38 KB)

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