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_original.ino @ 14:9d7736aad6a3

History | View | Annotate | Download (8.7 KB)

1
/******************************************************************************
2
 *  Copyright (C) 2015  Ben Bengler (mail@benbengler.com)
3
 *  Copyright (C) 2016  Queen Mary University of London 
4
 *  Authors: Ben Bengler, Fiore Martin
5
 *
6
 *  This file is part of Collidoscope.
7
 *
8
 *  Collidoscope is free software: you can redistribute it and/or modify
9
 *  it under the terms of the GNU General Public License as published by
10
 *  the Free Software Foundation, either version 3 of the License, or
11
 *  (at your option) any later version.
12
 *
13
 *  This program is distributed in the hope that it will be useful,
14
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
 *  GNU General Public License for more details.
17
 *
18
 *  You should have received a copy of the GNU General Public License
19
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
 *  
21
 ****************************************************************************
22
 *
23
 *  Teensy 2 ++ pinout: https://www.pjrc.com/teensy/card4b.pdf
24
 *  
25
 *  ANALOG INPUTS:
26
 *  Wavejet   -> F0 (38) [Horizontal rail 1]
27
 *  Wavejet   -> F1 (39) [Horizontal rail 2]
28
 *  Filter 1  -> F2 (40) [Fader with moon/sun 1]
29
 *  Filter 2  -> F4 (42) [Fader with moon/sun 2] 
30
 *  Duration 1-> F3 (41) [Fader with grains 1]
31
 *  Duration 2-> F5 (43) [Fader with grains 2]
32
 *
33
 *  DIGITAL INPUTS [INTERRUPTS]:
34
 *  Sel. length 1  -> INT0/INT1 (0, 1)  [Encoder 1]
35
 *  Sel. length 2  -> INT2/INT3 (2, 3)  [Encoder 2] 
36
 *
37
 *  DIGITAL INPUTS:
38
 *  Play1 toggle  -> B0 (20)
39
 *  Record1       -> B1 (21)
40
 *  Play2 toggle  -> B3 (22)
41
 *  Record2       -> B4 (24)
42
 *  
43
 *  DIGITAL OUTPUTS:
44
 *  Record Button 1 Led -> D4 (4)
45
 *  Record Button 2 Led -> D5 (5)
46
 *
47
 ******************************************************************************/ 
48

    
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
const int cc_duration = 2;  // MIDI cc controlling duration
60
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
///////////////////////////////////////////////////
66
//Default Values:
67
const int  Enc_def = 64; //default selection length  
68
int MIDI_led_state = LOW;
69

    
70
///////////////////////////////////////////////////
71
// Interface Inputs
72

    
73
//Buttons:
74

    
75
const int Pin_play1 = 20;       //B0    
76
const int Pin_record1 = 21;     //B1
77
const int Pin_play2 = 23;       //B3
78
const int Pin_record2 = 24;     //B4
79
const int Pin_record1_led =  4; //D4
80
const int Pin_record2_led =  5; //D5
81
const int Pin_MIDIled =  6;
82

    
83
Bounce button1 = Bounce(Pin_play1, 5); 
84
Bounce button2 = Bounce(Pin_record1, 5); 
85
Bounce button3 = Bounce(Pin_play2, 5); 
86
Bounce button4 = Bounce(Pin_record2, 5); 
87

    
88
 
89
//Encoder
90
Encoder Enc1 (0, 1);  //Encoder for section length on Wavejet 1
91
Encoder Enc2 (2, 3);  //Encoder for section length on Wavejet 2
92

    
93

    
94
// Variables
95
const int jitter_thresh = 10; // threshold value for analog INs to suppress sending MIDI due to input jitter 
96

    
97
void setup() {  
98

    
99
pinMode(Pin_play1, INPUT_PULLUP); 
100
pinMode(Pin_record1, INPUT_PULLUP);
101
pinMode(Pin_play2, INPUT_PULLUP);
102
pinMode(Pin_record2, INPUT_PULLUP);
103

    
104

    
105
pinMode(Pin_MIDIled, OUTPUT); 
106
pinMode(Pin_record1_led, OUTPUT);
107
pinMode(Pin_record2_led, OUTPUT);
108
}
109

    
110
//Store recent values to detect parameter change
111
long Enc1_old = -999;
112
long Enc2_old = -999;
113

    
114
uint16_t Jet1_old = 0;
115
int16_t Jet1_old_MIDI = -1;
116
uint16_t Jet2_old = 0;
117
int16_t Jet2_old_MIDI = -1;
118

    
119
uint16_t filter1_old = 0;
120
int16_t filter1_old_MIDI = -1;
121
uint16_t filter2_old = 0;
122
int16_t filter2_old_MIDI = -1;
123

    
124
uint16_t dur1_old = 0;
125
int16_t dur1_old_MIDI = -1;
126
uint16_t dur2_old = 0;
127
int16_t dur2_old_MIDI = -1;
128

    
129
void loop() {
130

    
131
  digitalWrite(Pin_MIDIled, LOW);
132
  digitalWrite(Pin_record1_led, HIGH);
133
  digitalWrite(Pin_record2_led, HIGH);
134
  button1.update();
135
  button2.update();
136
  button3.update();
137
  button4.update();
138

    
139
  
140
  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;    
143
  uint16_t filter2_new = analogRead(4);   //read filter Instrument 2;
144
  uint16_t dur1_new = analogRead(3); 
145
  uint16_t dur2_new = analogRead(5);
146

    
147
  
148
  //Encoder 1 [Controls selection length of wave 1]
149
  long Enc1_new = Enc1.read();
150
  Enc1_new = constrain(Enc1_new, 0, 127); //constrain to 7-bit MIDI range
151
  
152
  //Dynamic reset of counter to MIDI range 
153
  if (Enc1_new <= 0){
154
    Enc1.write(0); 
155
  }
156
  else if (Enc1_new >= 127){
157
    Enc1.write(127); 
158
  }
159
     
160
  //Encoder 2 [Controls selection length of wave 2]
161
  long Enc2_new = Enc2.read();
162
  Enc2_new = constrain(Enc2_new, 0, 127); //constrain to 7-bit MIDI range
163
  
164
  //Dynamic reset of counter to MIDI range 
165
  if (Enc2_new <= 0){
166
    Enc2.write(0); 
167
  }
168
  else if (Enc2_new >= 127){
169
    Enc2.write(127); 
170
  }
171

    
172
 //Instrument 1 Controls//////////////////////////////////////  
173
 
174
 //Loop/Keymode Switch Instrument 1 
175
   
176
 if (button1.risingEdge()) {   
177
   usbMIDI.sendControlChange(cc_play, 1, midi_chan_inst1);
178
 }
179
   
180
 if (button1.fallingEdge()) {   
181
   usbMIDI.sendControlChange(cc_play, 0, midi_chan_inst1);
182
 }
183
     
184
   
185
 //Record Instrument 1
186
 if (button2.fallingEdge()) {   
187
   usbMIDI.sendControlChange(cc_record, 1, midi_chan_inst1);
188
 }
189

    
190
 //send MIDI Wavejet 1 [Position Instrument 1]
191
 //<calibrate>
192
 if (Jet1_new > Jet1_old+jitter_thresh || Jet1_new < Jet1_old-jitter_thresh) {
193
    int16_t midiVal = constrain( map(Jet1_new, 18, 800, 0, 149), 0, 149 );
194
    if( midiVal != Jet1_old_MIDI ){
195
      Jet1_old_MIDI = midiVal;
196
      usbMIDI.sendPitchBend( midiVal, midi_chan_inst1 );
197
    }
198

    
199
    Jet1_old = Jet1_new;
200
    digitalWrite(Pin_MIDIled, HIGH);
201
 }
202

    
203

    
204
 //send MIDI Filter 1 [Filter Instrument 1]
205
 if ( filter1_new != filter1_old ) {
206
    int16_t midiVal = constrain( map(filter1_new, 0, 1024, 0, 127), 0, 127 );
207
    if( midiVal != filter1_old_MIDI){
208
      //Serial.println( midiVal );
209
      filter1_old_MIDI = midiVal;
210
      usbMIDI.sendControlChange(cc_filter, midiVal, midi_chan_inst1);
211
    }
212
    
213
    filter1_old = filter1_new;
214
    digitalWrite(Pin_MIDIled, HIGH);
215
  }
216

    
217
  if ( dur1_new != dur1_old ) {
218
 
219
    int16_t midiVal = constrain( map(dur1_new, 0, 1024, 0, 127), 0, 127 );
220
    if( midiVal != dur1_old_MIDI){
221
      dur1_old_MIDI = midiVal;
222
      usbMIDI.sendControlChange(cc_duration, midiVal, midi_chan_inst1);
223
    }
224
    
225
    dur1_old = dur1_new;
226
    digitalWrite(Pin_MIDIled, HIGH);
227
  }
228

    
229
  //send MIDI Encoder 1 [Selection length Instrument 1]
230
  if (Enc1_new != Enc1_old) {
231
    Enc1_old = Enc1_new;
232
    usbMIDI.sendControlChange(cc_length, Enc1_new, midi_chan_inst1);
233
    digitalWrite(Pin_MIDIled, HIGH);
234
  }
235

    
236
  
237
  
238
  //Instrument 2 Controls//////////////////////////////////////  
239
  
240
  //Loop/Keymode Switch Instrument 2
241
   
242
  if (button3.risingEdge()) {   
243
    usbMIDI.sendControlChange(cc_play, 1, midi_chan_inst2);
244
  }
245
  
246
  if (button3.fallingEdge()) {   
247
    usbMIDI.sendControlChange(cc_play, 0, midi_chan_inst2);
248
  }
249
  
250
  //Record Instrument 2
251
  if (button4.fallingEdge()) {   
252
    usbMIDI.sendControlChange(cc_record, 1, midi_chan_inst2);
253
  }
254

    
255
  //send MIDI Wavejet 2 [Position Instrument 2]
256
  //<calibrate>
257
  if (Jet2_new > Jet2_old+jitter_thresh || Jet2_new < Jet2_old-jitter_thresh) {
258
    int16_t midiVal = constrain( map( Jet2_new, 15, 780, 0, 149 ), 0, 149 );
259
    if( midiVal != Jet2_old_MIDI ){
260
      Jet2_old_MIDI = midiVal;
261
      usbMIDI.sendPitchBend( midiVal, midi_chan_inst2 );
262
    }
263

    
264
    Jet2_old = Jet2_new;
265
    digitalWrite(Pin_MIDIled, HIGH);
266
  }
267
  
268
   if ( filter2_new != filter2_old ) {
269
    int16_t midiVal = constrain( map(filter2_new, 0, 1024, 0, 127), 0, 127 );
270
    if( midiVal != filter2_old_MIDI){
271
      //Serial.println( midiVal );
272
      filter2_old_MIDI = midiVal;
273
      usbMIDI.sendControlChange(cc_filter, midiVal, midi_chan_inst2);
274
    }
275
    
276
    filter2_old = filter2_new;
277
    digitalWrite(Pin_MIDIled, HIGH);
278
   }
279

    
280
   if ( dur2_new != dur2_old ) {
281
    int16_t midiVal = constrain( map(dur2_new, 0, 1024, 0, 127), 0, 127 );
282
    if( midiVal != dur2_old_MIDI){
283
      //Serial.println( midiVal );
284
      dur2_old_MIDI = midiVal;
285
      usbMIDI.sendControlChange(cc_duration, midiVal, midi_chan_inst2);
286
    }
287
    
288
    dur2_old = dur2_new;
289
    digitalWrite(Pin_MIDIled, HIGH);
290
   }
291
   
292
  
293
  //send MIDI Encoder 2 [Selection length Instrument 2]
294
  if (Enc2_new != Enc2_old) {
295
    Enc2_old = Enc2_new;
296
    usbMIDI.sendControlChange(cc_length, Enc2_new, midi_chan_inst2);
297
    digitalWrite(Pin_MIDIled, HIGH);
298
  }
299
    
300
}
301