To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.
root / TeensyCode / collidoscope_single_knob.ino @ 0:02467299402e
History | View | Annotate | Download (8.87 KB)
| 1 | 0:02467299402e | 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 | |||
| 85 | |||
| 86 | // Variables |
||
| 87 | const int jitter_thresh = 10; //7threshold value for analog INs to suppress sending MIDI due to input jitter |
||
| 88 | |||
| 89 | void setup() {
|
||
| 90 | |||
| 91 | pinMode(Pin_play1, INPUT_PULLUP); |
||
| 92 | pinMode(Pin_record1, INPUT_PULLUP); |
||
| 93 | pinMode(Pin_play2, INPUT_PULLUP); |
||
| 94 | pinMode(Pin_record2, INPUT_PULLUP); |
||
| 95 | |||
| 96 | |||
| 97 | pinMode(Pin_MIDIled, OUTPUT); |
||
| 98 | pinMode(Pin_record1_led, OUTPUT); |
||
| 99 | pinMode(Pin_record2_led, OUTPUT); |
||
| 100 | } |
||
| 101 | |||
| 102 | //Store recent values to detect parameter change |
||
| 103 | long Enc1_old = -999; |
||
| 104 | long Enc2_old = -999; |
||
| 105 | |||
| 106 | uint16_t Jet1_old = 0; |
||
| 107 | int16_t Jet1_old_MIDI = -1; |
||
| 108 | uint16_t Jet2_old = 0; |
||
| 109 | int16_t Jet2_old_MIDI = -1; |
||
| 110 | |||
| 111 | uint16_t filter1_old = 0; |
||
| 112 | int16_t filter1_old_MIDI = -1; |
||
| 113 | uint16_t filter2_old = 0; |
||
| 114 | int16_t filter2_old_MIDI = -1; |
||
| 115 | |||
| 116 | uint16_t dur1_old = 0; |
||
| 117 | int16_t dur1_old_MIDI = -1; |
||
| 118 | uint16_t dur2_old = 0; |
||
| 119 | int16_t dur2_old_MIDI = -1; |
||
| 120 | |||
| 121 | void loop() {
|
||
| 122 | |||
| 123 | digitalWrite(Pin_MIDIled, LOW); |
||
| 124 | digitalWrite(Pin_record1_led, HIGH); |
||
| 125 | digitalWrite(Pin_record2_led, HIGH); |
||
| 126 | button1.update(); |
||
| 127 | button2.update(); |
||
| 128 | button3.update(); |
||
| 129 | button4.update(); |
||
| 130 | |||
| 131 | |||
| 132 | uint16_t Jet1_new = analogRead(0); //read Wavejet/Rail 1 |
||
| 133 | uint16_t Jet2_new = analogRead(1); //read Wavejet/Rail 2 |
||
| 134 | uint16_t filter1_new = analogRead(2); //read filter Instrument 1; ADJUST INPUT RANGE ACCORDING TO SENSOR |
||
| 135 | uint16_t filter2_new = analogRead(4); //read filter Instrument 2; ADJUST INPUT RANGE ACCORDING TO SENSOR |
||
| 136 | uint16_t dur1_new = analogRead(3); |
||
| 137 | uint16_t dur2_new = analogRead(5); |
||
| 138 | |||
| 139 | |||
| 140 | //Encoder 1 [Controls selection length of wave 1] |
||
| 141 | long Enc1_new = Enc1.read(); |
||
| 142 | Enc1_new = constrain(Enc1_new, 0, 127); //constrain to 7-bit MIDI range |
||
| 143 | |||
| 144 | //Dynamic reset of counter to MIDI range |
||
| 145 | if (Enc1_new <= 0){
|
||
| 146 | Enc1.write(0); |
||
| 147 | } |
||
| 148 | else if (Enc1_new >= 127){
|
||
| 149 | Enc1.write(127); |
||
| 150 | } |
||
| 151 | |||
| 152 | //Encoder 2 [Controls selection length of wave 2] |
||
| 153 | long Enc2_new = Enc2.read(); |
||
| 154 | Enc2_new = constrain(Enc2_new, 0, 127); //constrain to 7-bit MIDI range |
||
| 155 | |||
| 156 | //Dynamic reset of counter to MIDI range |
||
| 157 | if (Enc2_new <= 0){
|
||
| 158 | Enc2.write(0); |
||
| 159 | } |
||
| 160 | else if (Enc2_new >= 127){
|
||
| 161 | Enc2.write(127); |
||
| 162 | } |
||
| 163 | |||
| 164 | //Instrument 1 Controls////////////////////////////////////// |
||
| 165 | |||
| 166 | //Loop/Keymode Switch Instrument 1 |
||
| 167 | |||
| 168 | if (button1.risingEdge()) {
|
||
| 169 | //Serial.println("Loop mode");
|
||
| 170 | usbMIDI.sendControlChange(cc_play, 1, midi_chan_inst1); |
||
| 171 | } |
||
| 172 | |||
| 173 | if (button1.fallingEdge()) {
|
||
| 174 | //Serial.println("Keyboardmode mode");
|
||
| 175 | usbMIDI.sendControlChange(cc_play, 0, midi_chan_inst1); |
||
| 176 | } |
||
| 177 | |||
| 178 | |||
| 179 | //Record Instrument 1 |
||
| 180 | if (button2.fallingEdge()) {
|
||
| 181 | //Serial.println("RECORD! Instrument 1");
|
||
| 182 | usbMIDI.sendControlChange(cc_record, 1, midi_chan_inst1); |
||
| 183 | } |
||
| 184 | |||
| 185 | //send MIDI Wavejet 1 [Position Instrument 1] |
||
| 186 | |||
| 187 | if (Jet1_new > Jet1_old+jitter_thresh || Jet1_new < Jet1_old-jitter_thresh) {
|
||
| 188 | |||
| 189 | int16_t midiVal = constrain( map(Jet1_new, 24, 926, 0, 149), 0, 149 ); |
||
| 190 | // int16_t midiVal = constrain( map( Jet1_new, 23, 928, 0, 149 ), 0, 149 ); old collidoscope |
||
| 191 | if( midiVal != Jet1_old_MIDI ){
|
||
| 192 | Jet1_old_MIDI = midiVal; |
||
| 193 | usbMIDI.sendPitchBend( midiVal, midi_chan_inst1 ); |
||
| 194 | } |
||
| 195 | |||
| 196 | Jet1_old = Jet1_new; |
||
| 197 | digitalWrite(Pin_MIDIled, HIGH); |
||
| 198 | } |
||
| 199 | |||
| 200 | |||
| 201 | //send MIDI Filter 1 [Filter Instrument 1] |
||
| 202 | |||
| 203 | if ( filter1_new != filter1_old ) { // maybe adding jitter threshold needed, see Jet1_new
|
||
| 204 | |||
| 205 | int16_t midiVal = constrain( map(filter1_new, 0, 1024, 0, 127), 0, 127 ); |
||
| 206 | if( midiVal != filter1_old_MIDI){
|
||
| 207 | //Serial.println( midiVal ); |
||
| 208 | filter1_old_MIDI = midiVal; |
||
| 209 | usbMIDI.sendControlChange(cc_filter, midiVal, midi_chan_inst1); |
||
| 210 | } |
||
| 211 | |||
| 212 | filter1_old = filter1_new; |
||
| 213 | digitalWrite(Pin_MIDIled, HIGH); |
||
| 214 | } |
||
| 215 | |||
| 216 | if ( dur1_new != dur1_old ) { // maybe adding jitter threshold needed, see Jet1_new
|
||
| 217 | |||
| 218 | int16_t midiVal = constrain( map(dur1_new, 0, 1024, 0, 127), 0, 127 ); |
||
| 219 | if( midiVal != dur1_old_MIDI){
|
||
| 220 | //Serial.println( midiVal ); |
||
| 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 | //Serial.println("Encoder 1: ");
|
||
| 233 | //Serial.println(Enc1_new); |
||
| 234 | usbMIDI.sendControlChange(cc_length, Enc1_new, midi_chan_inst1); |
||
| 235 | digitalWrite(Pin_MIDIled, HIGH); |
||
| 236 | } |
||
| 237 | |||
| 238 | |||
| 239 | |||
| 240 | //Instrument 2 Controls////////////////////////////////////// |
||
| 241 | |||
| 242 | //Loop/Keymode Switch Instrument 2 |
||
| 243 | |||
| 244 | if (button3.risingEdge()) {
|
||
| 245 | //Serial.println("Loop mode");
|
||
| 246 | usbMIDI.sendControlChange(cc_play, 1, midi_chan_inst2); |
||
| 247 | } |
||
| 248 | |||
| 249 | if (button3.fallingEdge()) {
|
||
| 250 | //Serial.println("Keyboardmode mode");
|
||
| 251 | usbMIDI.sendControlChange(cc_play, 0, midi_chan_inst2); |
||
| 252 | } |
||
| 253 | |||
| 254 | //Record Instrument 2 |
||
| 255 | if (button4.fallingEdge()) {
|
||
| 256 | //Serial.println("RECORD! Instrument 2");
|
||
| 257 | usbMIDI.sendControlChange(cc_record, 1, midi_chan_inst2); |
||
| 258 | } |
||
| 259 | |||
| 260 | //send MIDI Wavejet 2 [Position Instrument 2] |
||
| 261 | |||
| 262 | if (Jet2_new > Jet2_old+jitter_thresh || Jet2_new < Jet2_old-jitter_thresh) {
|
||
| 263 | //Serial.println("RECORD! Instrument 2");
|
||
| 264 | int16_t midiVal = constrain( map( Jet2_new, 925, 18, 149, 0 ), 0, 149 ); |
||
| 265 | if( midiVal != Jet2_old_MIDI ){
|
||
| 266 | Jet2_old_MIDI = midiVal; |
||
| 267 | usbMIDI.sendPitchBend( midiVal, midi_chan_inst2 ); |
||
| 268 | } |
||
| 269 | |||
| 270 | Jet2_old = Jet2_new; |
||
| 271 | digitalWrite(Pin_MIDIled, HIGH); |
||
| 272 | } |
||
| 273 | |||
| 274 | //Serial.println(filter2_new); |
||
| 275 | if ( filter2_new != filter2_old ) { // maybe adding jitter threshold needed, see Jet1_new
|
||
| 276 | int16_t midiVal = constrain( map(filter2_new, 0, 1024, 0, 127), 0, 127 ); |
||
| 277 | if( midiVal != filter2_old_MIDI){
|
||
| 278 | //Serial.println( midiVal ); |
||
| 279 | filter2_old_MIDI = midiVal; |
||
| 280 | usbMIDI.sendControlChange(cc_filter, midiVal, midi_chan_inst2); |
||
| 281 | } |
||
| 282 | |||
| 283 | filter2_old = filter2_new; |
||
| 284 | digitalWrite(Pin_MIDIled, HIGH); |
||
| 285 | } |
||
| 286 | |||
| 287 | if ( dur2_new != dur2_old ) { // maybe adding jitter threshold needed, see Jet1_new
|
||
| 288 | int16_t midiVal = constrain( map(dur2_new, 0, 1024, 0, 127), 0, 127 ); |
||
| 289 | if( midiVal != dur2_old_MIDI){
|
||
| 290 | //Serial.println( midiVal ); |
||
| 291 | dur2_old_MIDI = midiVal; |
||
| 292 | usbMIDI.sendControlChange(cc_duration, midiVal, midi_chan_inst2); |
||
| 293 | } |
||
| 294 | |||
| 295 | dur2_old = dur2_new; |
||
| 296 | digitalWrite(Pin_MIDIled, HIGH); |
||
| 297 | } |
||
| 298 | |||
| 299 | |||
| 300 | //send MIDI Encoder 2 [Selection length Instrument 2] |
||
| 301 | if (Enc2_new != Enc2_old) {
|
||
| 302 | Enc2_old = Enc2_new; |
||
| 303 | //Serial.println("Encoder 2: ");
|
||
| 304 | //Serial.println(Enc2_new); |
||
| 305 | usbMIDI.sendControlChange(cc_length, Enc2_new, midi_chan_inst2); |
||
| 306 | digitalWrite(Pin_MIDIled, HIGH); |
||
| 307 | } |
||
| 308 | |||
| 309 | } |