-
Notifications
You must be signed in to change notification settings - Fork 3
/
newRotaryEncoder_MIDI.ino
211 lines (170 loc) · 5.11 KB
/
newRotaryEncoder_MIDI.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
//teensy++ code for rotary encoder controller
//tomash ghz
//www.tomashg.com
#include "ledRings.h" // library for LED rings from http://code.google.com/p/ledrings/
#include <Encoder.h>
#include <Bounce.h>
/********| BEGIN CONFIGURATION |********/
#define NUM_LED_RINGS 4 // The number of LED Ring boards
#define LED_RING_OE 10 // Output Enable pin of the board
#define LED_RING_SDI 7 // Data pin of the board
#define LED_RING_CLK 8 // Clock pin of the board
#define LED_RING_LE 9 // Latch pin of the board
#define LED_RING_1 0 // This is entirely up to you.
#define LED_RING_2 1 // It's used to refer to the individual led boards by name.
#define LED_RING_3 2 // Use whatever works for your purpose, or none at all :)
#define LED_RING_4 3 // Use whatever works for your purpose, or none at all :)
/*********| END CONFIGURATION |*********/
//debugging will print out in serial instead of midi
boolean debugging=false;
boolean ableton=true; // if enabled updates the LED values internaly since ableton doesnt do that in real time
//channel number for midi messages
int midiChannel=5;
ledRings ring(LED_RING_SDI, LED_RING_CLK, LED_RING_LE, LED_RING_OE, NUM_LED_RINGS);
Encoder knob1(0, 5);
Encoder knob2(1, 14);
Encoder knob3(18, 23);
Encoder knob4(19, 20);
int knobShift[4]={0,0,0,0};
float ledValues[8];
int buttons[4]={6,15,22,21};
Bounce *buttonState[4];
//values for the encoder leds
unsigned int sequence[3][16] = {
{0x0, 0x1, 0x2, 0x4, 0x8, 0x10, 0x20, 0x40, 0x80, 0x100, 0x200, 0x400, 0x800, 0x1000, 0x2000, 0x4000},
{0x0, 0x1, 0x3, 0x7, 0xf, 0x1f, 0x3f, 0x7f, 0xff, 0x1ff, 0x3ff, 0x7ff, 0xfff, 0x1fff, 0x3fff, 0x7fff},
{0x0, 0x7fff, 0x3ffe, 0x1ffc, 0xff8, 0x7f0, 0x3e0, 0x1c0, 0x80, 0x1c0, 0x3e0, 0x7f0, 0xff8, 0x1ffC, 0x3ffe, 0x7fff},
};
long knobPosition[4]={-999,-999,-999,-999};
void setup() {
//DEBUG
if(debugging){
Serial.begin(9600);//open serail port
}
else{
Serial.begin(31250);//open midi
usbMIDI.setHandleControlChange(myCC);
}
//set button pins to input
for(int i=0;i<4;i++){
pinMode(buttons[i], INPUT_PULLUP);
buttonState[i]=new Bounce(buttons[i],10);
}
//digitalWrite(6, LOW);
pinMode(6, OUTPUT);
//play led animation
ledInitialize();
}
void loop(){
long newKnob[4];
//recieve MIDI messages
if(!debugging){
usbMIDI.read();
}
//read the new knob values
newKnob[0]=knob1.read();
newKnob[1]=knob2.read();
newKnob[2]=knob3.read();
newKnob[3]=knob4.read();
for(int i=0; i<4;i++){
//recieve MIDI messages
if(!debugging){
usbMIDI.read();
}
//read buttons
if(buttonState[i]->update()){//state changed
if(buttonState[i]->read()==LOW){//is pressed
}else{ // was released
ring.blinkRing(i,2,40);
//flip the shift state
if(knobShift[i]==0)
knobShift[i]=1;
else
knobShift[i]=0;
}
}
//knob moved
if(newKnob[i]!=knobPosition[i]){
//did it move forward?
if(newKnob[i]>knobPosition[i]){
midiCC(0,i);
}
else{
midiCC(1,i);
}
}
knobPosition[i]=newKnob[i];
}
//recieve MIDI messages
if(!debugging){
usbMIDI.read();
}
//update the led displays
printLeds();
}
void ledInitialize(){
// initial led animation
ring.allRingsOff();
for(int i=0;i<4;i++){
for (byte j=0;j<ring.num_leds_arc;j++){
ring.setRingLed(i, j, HIGH);
delay(25);
//ring.allRingsOff();
}
}
for(int i=3;i>0;i--){
for (byte j=ring.num_leds_arc-1;j>0;j--){
ring.setRingLed(i, j, HIGH);
delay(25);
ring.allRingsOff();
}
}
}
void printLeds(){
//update the LED rings
ring.allRingsOff();
for (int i=0;i<4;i++){
int pos=ledValues[i*2+knobShift[i]];
ring.setRingState(i, sequence[1][pos]);
if(knobShift[i]==1)
ring.setRingBottomLed(i,HIGH);
}
}
void midiCC(int n, int v){
//send a midi cc message
if(n==0){ //turns right
if(debugging){
//Serial.print(v*2+knobShift[v]);
//Serial.println(" >>");
}else{
if (ableton){
usbMIDI.sendControlChange(v*2+knobShift[v], 68, midiChannel); //normaly 65 for traktor, 68 for ableton to have less resolution
ledValues[v*2+knobShift[v]]+=0.212;
}
else
usbMIDI.sendControlChange(v*2+knobShift[v], 65, midiChannel);
}
}else{ //turns left
if(debugging){
//Serial.print(v*2+knobShift[v]);
//Serial.println(" <<");
}else{
if (ableton){
usbMIDI.sendControlChange(v*2+knobShift[v], 60, midiChannel); //normaly 63 for traktor, 60 for ableton to have less resolution
ledValues[v*2+knobShift[v]]-=0.212; //update the leds
}
else
usbMIDI.sendControlChange(v*2+knobShift[v], 63, midiChannel);
}
}
//make sure its in range
ledValues[v*2+knobShift[v]]=constrain(ledValues[v*2+knobShift[v]],0,15);
}
void myCC(byte channel,byte number,byte value){
//event handler for incoming cc messages
if(channel==midiChannel){
if ((number>=0)&&(number<=7)){
ledValues[number%8]=map(value,0,127,0,15);
}
}
}