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 @ 5:75b744078d66

History | View | Annotate | Download (9.11 KB)

1 1:b5bcad8e7803 f
/******************************************************************************
2 5:75b744078d66 f
 *  Copyright (C) 2015  Ben Bengler (mail@benbengler.com)
3
 *  Copyright (C) 2016  Queen Mary University of London
4
 *  Authors: Ben Bengler, Fiore Martin
5 1:b5bcad8e7803 f
 *
6 5:75b744078d66 f
 *  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 1:b5bcad8e7803 f
 *
21 5:75b744078d66 f
 ****************************************************************************
22
 *
23 1:b5bcad8e7803 f
 *  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 5:75b744078d66 f
 *  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 1:b5bcad8e7803 f
 *
33
 *  DIGITAL INPUTS [INTERRUPTS]:
34
 *  Sel. length 1  -> INT0/INT1 (0, 1)  [Encoder 1]
35 5:75b744078d66 f
 *  Sel. length 2  -> INT2/INT3 (2, 3)  [Encoder 2]
36 1:b5bcad8e7803 f
 *
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 5:75b744078d66 f
 *
47 1:b5bcad8e7803 f
 ******************************************************************************/
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 5:75b744078d66 f
64 1:b5bcad8e7803 f
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
//const int Pin_reset1 = 22;    //B2, not in use
84
//const int Pin_reset2 = 25;    //B5, not in use
85
86
Bounce button1 = Bounce(Pin_play1, 5);
87
Bounce button2 = Bounce(Pin_record1, 5);
88
Bounce button3 = Bounce(Pin_play2, 5);
89
Bounce button4 = Bounce(Pin_record2, 5);
90
91
92
//Encoder
93
Encoder Enc1 (0, 1);  //Encoder for section length on Wavejet 1
94
Encoder Enc2 (2, 3);  //Encoder for section length on Wavejet 2
95
96
97
// Variables
98
const int jitter_thresh = 10; //7threshold value for analog INs to suppress sending MIDI due to input jitter
99
100
void setup() {
101
102
pinMode(Pin_play1, INPUT_PULLUP);
103
pinMode(Pin_record1, INPUT_PULLUP);
104
pinMode(Pin_play2, INPUT_PULLUP);
105
pinMode(Pin_record2, INPUT_PULLUP);
106
107
108
pinMode(Pin_MIDIled, OUTPUT);
109
pinMode(Pin_record1_led, OUTPUT);
110
pinMode(Pin_record2_led, OUTPUT);
111
}
112
113
//Store recent values to detect parameter change
114
long Enc1_old = -999;
115
long Enc2_old = -999;
116
117
uint16_t Jet1_old = 0;
118
int16_t Jet1_old_MIDI = -1;
119
uint16_t Jet2_old = 0;
120
int16_t Jet2_old_MIDI = -1;
121
122
uint16_t filter1_old = 0;
123
int16_t filter1_old_MIDI = -1;
124
uint16_t filter2_old = 0;
125
int16_t filter2_old_MIDI = -1;
126
127
uint16_t dur1_old = 0;
128
int16_t dur1_old_MIDI = -1;
129
uint16_t dur2_old = 0;
130
int16_t dur2_old_MIDI = -1;
131
132
void loop() {
133
134
  digitalWrite(Pin_MIDIled, LOW);
135
  digitalWrite(Pin_record1_led, HIGH);
136
  digitalWrite(Pin_record2_led, HIGH);
137
  button1.update();
138
  button2.update();
139
  button3.update();
140
  button4.update();
141
142
143
  uint16_t Jet1_new = analogRead(0); //read Wavejet/Rail 1
144
  uint16_t Jet2_new = analogRead(1); //read Wavejet/Rail 2
145
  uint16_t filter1_new = analogRead(2);   //read filter Instrument 1; ADJUST INPUT RANGE ACCORDING TO SENSOR
146
  uint16_t filter2_new = analogRead(4);   //read filter Instrument 2; ADJUST INPUT RANGE ACCORDING TO SENSOR
147
  uint16_t dur1_new = analogRead(3);
148
  uint16_t dur2_new = analogRead(5);
149
150
151
  //Encoder 1 [Controls selection length of wave 1]
152
  long Enc1_new = Enc1.read();
153
  Enc1_new = constrain(Enc1_new, 0, 127); //constrain to 7-bit MIDI range
154
155
  //Dynamic reset of counter to MIDI range
156
  if (Enc1_new <= 0){
157
    Enc1.write(0);
158
  }
159
  else if (Enc1_new >= 127){
160
    Enc1.write(127);
161
  }
162
163
  //Encoder 2 [Controls selection length of wave 2]
164
  long Enc2_new = Enc2.read();
165
  Enc2_new = constrain(Enc2_new, 0, 127); //constrain to 7-bit MIDI range
166
167
  //Dynamic reset of counter to MIDI range
168
  if (Enc2_new <= 0){
169
    Enc2.write(0);
170
  }
171
  else if (Enc2_new >= 127){
172
    Enc2.write(127);
173
  }
174
175
 //Instrument 1 Controls//////////////////////////////////////
176
177
 //Loop/Keymode Switch Instrument 1
178
179
 if (button1.risingEdge()) {
180
   usbMIDI.sendControlChange(cc_play, 1, midi_chan_inst1);
181
 }
182
183
 if (button1.fallingEdge()) {
184
   usbMIDI.sendControlChange(cc_play, 0, midi_chan_inst1);
185
 }
186
187
188
 //Record Instrument 1
189
 if (button2.fallingEdge()) {
190
   usbMIDI.sendControlChange(cc_record, 1, midi_chan_inst1);
191
 }
192
193
 //send MIDI Wavejet 1 [Position Instrument 1]
194 5:75b744078d66 f
   //Serial.print("W1>"); Serial.println(Jet1_new);
195 1:b5bcad8e7803 f
 if (Jet1_new > Jet1_old+jitter_thresh || Jet1_new < Jet1_old-jitter_thresh) {
196 5:75b744078d66 f
    //int16_t midiVal = constrain( map(Jet1_new, 24, 926, 0, 149), 0, 149 );
197
    int16_t midiVal = constrain( map(Jet1_new, 18, 800, 0, 149), 0, 149 );
198 1:b5bcad8e7803 f
    if( midiVal != Jet1_old_MIDI ){
199
      Jet1_old_MIDI = midiVal;
200
      usbMIDI.sendPitchBend( midiVal, midi_chan_inst1 );
201
    }
202
203
    Jet1_old = Jet1_new;
204
    digitalWrite(Pin_MIDIled, HIGH);
205
 }
206
207
208
 //send MIDI Filter 1 [Filter Instrument 1]
209 5:75b744078d66 f
 if ( filter1_new != filter1_old ) {
210 1:b5bcad8e7803 f
211
    int16_t midiVal = constrain( map(filter1_new, 0, 1024, 0, 127), 0, 127 );
212
    if( midiVal != filter1_old_MIDI){
213
      //Serial.println( midiVal );
214
      filter1_old_MIDI = midiVal;
215
      usbMIDI.sendControlChange(cc_filter, midiVal, midi_chan_inst1);
216
    }
217
218
    filter1_old = filter1_new;
219
    digitalWrite(Pin_MIDIled, HIGH);
220
  }
221
222 5:75b744078d66 f
  if ( dur1_new != dur1_old ) {
223 1:b5bcad8e7803 f
224
    int16_t midiVal = constrain( map(dur1_new, 0, 1024, 0, 127), 0, 127 );
225
    if( midiVal != dur1_old_MIDI){
226
      //Serial.println( midiVal );
227
      dur1_old_MIDI = midiVal;
228
      usbMIDI.sendControlChange(cc_duration, midiVal, midi_chan_inst1);
229
    }
230
231
    dur1_old = dur1_new;
232
    digitalWrite(Pin_MIDIled, HIGH);
233
  }
234
235
  //send MIDI Encoder 1 [Selection length Instrument 1]
236
  if (Enc1_new != Enc1_old) {
237
    Enc1_old = Enc1_new;
238
    //Serial.println("Encoder 1: ");
239
    //Serial.println(Enc1_new);
240
    usbMIDI.sendControlChange(cc_length, Enc1_new, midi_chan_inst1);
241
    digitalWrite(Pin_MIDIled, HIGH);
242
  }
243
244
245
246
  //Instrument 2 Controls//////////////////////////////////////
247
248
  //Loop/Keymode Switch Instrument 2
249
250
  if (button3.risingEdge()) {
251
    usbMIDI.sendControlChange(cc_play, 1, midi_chan_inst2);
252
  }
253
254
  if (button3.fallingEdge()) {
255
    usbMIDI.sendControlChange(cc_play, 0, midi_chan_inst2);
256
  }
257
258
  //Record Instrument 2
259
  if (button4.fallingEdge()) {
260
    usbMIDI.sendControlChange(cc_record, 1, midi_chan_inst2);
261
  }
262
263
  //send MIDI Wavejet 2 [Position Instrument 2]
264 5:75b744078d66 f
  // Serial.print("W2>"); Serial.println(Jet2_new);
265 1:b5bcad8e7803 f
  if (Jet2_new > Jet2_old+jitter_thresh || Jet2_new < Jet2_old-jitter_thresh) {
266 5:75b744078d66 f
    //int16_t midiVal = constrain( map( Jet2_new, 925, 18, 149, 0 ), 0, 149 );
267
    int16_t midiVal = constrain( map( Jet2_new, 15, 780, 0, 149 ), 0, 149 );
268 1:b5bcad8e7803 f
    if( midiVal != Jet2_old_MIDI ){
269
      Jet2_old_MIDI = midiVal;
270
      usbMIDI.sendPitchBend( midiVal, midi_chan_inst2 );
271
    }
272
273
    Jet2_old = Jet2_new;
274
    digitalWrite(Pin_MIDIled, HIGH);
275
  }
276
277 5:75b744078d66 f
   if ( filter2_new != filter2_old ) {
278 1:b5bcad8e7803 f
    int16_t midiVal = constrain( map(filter2_new, 0, 1024, 0, 127), 0, 127 );
279
    if( midiVal != filter2_old_MIDI){
280
      //Serial.println( midiVal );
281
      filter2_old_MIDI = midiVal;
282
      usbMIDI.sendControlChange(cc_filter, midiVal, midi_chan_inst2);
283
    }
284
285
    filter2_old = filter2_new;
286
    digitalWrite(Pin_MIDIled, HIGH);
287
   }
288
289 5:75b744078d66 f
   if ( dur2_new != dur2_old ) {
290 1:b5bcad8e7803 f
    int16_t midiVal = constrain( map(dur2_new, 0, 1024, 0, 127), 0, 127 );
291
    if( midiVal != dur2_old_MIDI){
292
      //Serial.println( midiVal );
293
      dur2_old_MIDI = midiVal;
294
      usbMIDI.sendControlChange(cc_duration, midiVal, midi_chan_inst2);
295
    }
296
297
    dur2_old = dur2_new;
298
    digitalWrite(Pin_MIDIled, HIGH);
299
   }
300
301
302
  //send MIDI Encoder 2 [Selection length Instrument 2]
303
  if (Enc2_new != Enc2_old) {
304
    Enc2_old = Enc2_new;
305
    usbMIDI.sendControlChange(cc_length, Enc2_new, midi_chan_inst2);
306
    digitalWrite(Pin_MIDIled, HIGH);
307
  }
308
309
}