-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathccv4.ino
445 lines (392 loc) · 11.9 KB
/
ccv4.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
bool debug = true;
/*
Color Control Voltage 3.0
This device converts any coloured picture/photo/painting into 3 channels (RGB) 0-5V eurorack synthesizer compatible.
Creative Commons License
Color Control Voltage by Pantala Labs is licensed
under a Creative Commons Attribution 4.0 International License.
Gibran Curtiss Salomão. MAY/2017 - CC-BY
*/
#include <Wire.h>
#include <Adafruit_MCP4725.h>
Adafruit_MCP4725 dac;
#define DAC_RESOLUTION (8)
#include <Arduino.h>
#include <AccelStepper.h>
#include <AnalogInput.h>
#include <Trigger.h>
#include <Counter.h>
#include <EventDebounce.h>
#include <Switch.h>
#define MAXPATTERNS 19
#define MAXstepCounter 16
#include "patterns.h"
extern boolean patterns[MAXPATTERNS][MAXstepCounter];
//==========================PINS=======================================
//digital pins
#define CLOCKINPIN 2 //interruption pin - optiwheel
EventDebounce triggerInterval(50);
//TRIGGER out
Trigger gateR(43); //clock out red channel
Trigger gateG(45); //clock out green channel
Trigger gateB(47); //clock out blue channel
Trigger gateM(49); //clock out master channel pin
Trigger gateSlv(51); //slave clock divider
//COLOR sensor
#define S0PIN 31 //S0 color sensor
#define S1PIN 33 //S1 color sensor
#define S3PIN 29 //S3 color sensor
#define S2PIN 27 //S2 color sensor
#define OUTCOLORPIN 25 //out color sensor
//DAC select
#define DACRPIN_SEL1 14 //red color DAC array select
#define DACGPIN_SEL2 15 //green color DAC array select
#define DACBPIN_SEL3 16 //blue color DAC array select
//NEMA
#define DIR 35 //nema direction
#define STEP 37 //nema steps
//CLOCK source
Switch swIntClockSrc(53);
//analog pins
AnalogInput SAMPLESPEED(12); //sample speed pot pin , if internal clock source
AnalogInput MAINCLKDIV(14); //arm speed pot pin++++++++++++++++++++++deactivated
AnalogInput MOTORSPEED(15); //nema speed pot pin
//octave quantity for color channels -5 to 0 to 5
AnalogInput OCTAVEQUANTITYR(3);
AnalogInput OCTAVEQUANTITYG(4);
AnalogInput OCTAVEQUANTITYB(5);
//ocatve shift for color channels -12 to +12
AnalogInput NOTESHIFTR(6);
AnalogInput NOTESHIFTG(7);
AnalogInput NOTESHIFTB(8);
//clock divider for color channels
AnalogInput CLOCKDIVIDERR(9);
AnalogInput CLOCKDIVIDERG(10);
AnalogInput CLOCKDIVIDERB(11);
//=================================================================
//step motor
#define MINMOTORRPM 230 //slower
#define MAXMOTORRPM 400 //faster
uint16_t MOTORSPEEDValue = 0;
AccelStepper mystepper(1, STEP, DIR);
//colors
uint16_t red = 0;
uint16_t green = 0;
uint16_t blue = 0;
#define MINCOLOR 15
#define MAXCOLOR 210
//internal sample speed
#define MINSAMPLEINTERVAL 50 //min clock interval ms
#define MAXSAMPLEINTERVAL 4000 //max clock interval ms
uint16_t sampleSpeedInterval = 1000; //sample speed
//octave quantity final processed value -5 to 5
int16_t OCTAVEQUANTITYRValue = 0;
int16_t OCTAVEQUANTITYGValue = 0;
int16_t OCTAVEQUANTITYBValue = 0;
//notes shift final value -12 to 12
int16_t NOTESHIFTRValue = 0;
int16_t NOTESHIFTGValue = 0;
int16_t NOTESHIFTBValue = 0;
int16_t MAINCLKDIVValue = 0;
//midi note final calculated value
uint16_t midiNoteR = -1;
uint16_t midiNoteG = -1;
uint16_t midiNoteB = -1;
//tick timers and triggers
Counter stepCounterM(15); //tick counter
Counter stepCounterR(15); //tick counter
Counter stepCounterG(15); //tick counter
Counter stepCounterB(15); //tick counter
boolean startNextStep = false; //flag to read new colors each interruption
unsigned long lastTick = 0; //last timer interruption
unsigned long nextTick = 0; //next timer interruption
Counter potPriority(11); //tick counter
//clock divider final processed value
uint16_t CLOCKDIVIDERRValue = 0;
uint16_t CLOCKDIVIDERGValue = 0;
uint16_t CLOCKDIVIDERBValue = 0;
void setup()
{
if (debug)
Serial.begin(9600);
//color sensor
pinMode(S0PIN, OUTPUT);
pinMode(S1PIN, OUTPUT);
pinMode(S2PIN, OUTPUT);
pinMode(S3PIN, OUTPUT);
pinMode(OUTCOLORPIN, INPUT);
digitalWrite(S0PIN, HIGH);
digitalWrite(S1PIN, LOW);
pinMode(CLOCKINPIN, INPUT);
pinMode(DACRPIN_SEL1, OUTPUT);
pinMode(DACGPIN_SEL2, OUTPUT);
pinMode(DACBPIN_SEL3, OUTPUT);
digitalWrite(DACRPIN_SEL1, LOW);
digitalWrite(DACGPIN_SEL2, LOW);
digitalWrite(DACBPIN_SEL3, LOW);
dac.begin(0x63);
//NEMA
pinMode(STEP, OUTPUT);
pinMode(DIR, OUTPUT);
// digitalWrite(DIR, HIGH); // move motor in one direction
mystepper.setMaxSpeed(MAXMOTORRPM);
mystepper.setAcceleration(MAXMOTORRPM / 2);
mystepper.setSpeed(MAXMOTORRPM / 2);
}
void loop()
{
swIntClockSrc.readPin();
if (swIntClockSrc.switchOn())
stepCounterM.reset();
mystepper.runSpeed();
readPot();
closeTriggers();
checkIfNextStepTime();
//move to next step
if (startNextStep)
{
startNextStep = false;
stepCounterM.advance();
stepCounterR.advance();
stepCounterG.advance();
stepCounterB.advance();
lastTick = millis();
if (swIntClockSrc.active())
nextTick = lastTick + sampleSpeedInterval;
readColors();
updateDacs();
openTriggers();
}
}
void checkIfNextStepTime()
{
//if internal timer, and time to next step
if (swIntClockSrc.active() && (millis() >= nextTick))
startNextStep = true;
else if (!swIntClockSrc.active() && digitalRead(CLOCKINPIN) && triggerInterval.debounced())
{
triggerInterval.debounce();
startNextStep = true;
}
}
void readPot()
{
int16_t value;
//if internal timer , calculate the sample speed interval
if (swIntClockSrc.active())
sampleSpeedInterval = map(SAMPLESPEED.readPin(), 0, 1023, MAXSAMPLEINTERVAL, MINSAMPLEINTERVAL);
//read inputs , one pot at time
switch (potPriority.advance())
{
case 0:
//read motor speed pot and updates nema
value = map(MOTORSPEED.readPin(), 0, 1023, MINMOTORRPM, MAXMOTORRPM);
if (value != MOTORSPEEDValue)
{
mystepper.setSpeed(value);
MOTORSPEEDValue = value;
}
break;
case 1:
value = map(CLOCKDIVIDERR.readPin(), 0, 1023, 0, MAXPATTERNS);
if (value != CLOCKDIVIDERRValue)
{
stepCounterR.reset();
CLOCKDIVIDERRValue = value;
}
break;
case 2:
value = map(CLOCKDIVIDERG.readPin(), 0, 1023, 0, MAXPATTERNS);
if (value != CLOCKDIVIDERGValue)
{
stepCounterG.reset();
CLOCKDIVIDERGValue = value;
}
break;
case 3:
value = map(CLOCKDIVIDERB.readPin(), 0, 1023, 0, MAXPATTERNS);
if (value != CLOCKDIVIDERBValue)
{
stepCounterB.reset();
CLOCKDIVIDERBValue = value;
}
break;
//octaves quantity -5 to 5
case 4:
OCTAVEQUANTITYRValue = constrain(map(OCTAVEQUANTITYR.readPin(), 0, 1023, -5, 6), -5, 5);
// if (OCTAVEQUANTITYRValue == 0)
// value = 1;
break;
case 6:
OCTAVEQUANTITYGValue = constrain(map(OCTAVEQUANTITYG.readPin(), 0, 1023, -5, 6), -5, 5);
// if (OCTAVEQUANTITYGValue == 0)
// OCTAVEQUANTITYGValue = 1;
break;
case 7:
OCTAVEQUANTITYBValue = constrain(map(OCTAVEQUANTITYB.readPin(), 0, 1023, -5, 6), -5, 5);
// if (OCTAVEQUANTITYBValue == 0)
// OCTAVEQUANTITYBValue = 1;
break;
case 8:
NOTESHIFTRValue = constrain(map(NOTESHIFTR.readPin(), 0, 1023, -12, 13), -12, 12);
break;
case 9:
NOTESHIFTGValue = constrain(map(NOTESHIFTG.readPin(), 0, 1023, -12, 13), -12, 12);
break;
case 10:
NOTESHIFTBValue = constrain(map(NOTESHIFTB.readPin(), 0, 1023, -12, 13), -12, 12);
break;
case 11:
MAINCLKDIVValue = map(MAINCLKDIV.readPin(), 0, 1023, 0, MAXPATTERNS);
break;
}
}
void openTriggers()
{
gateM.start();
if (patterns[CLOCKDIVIDERRValue][stepCounterR.getValue()])
gateR.start();
if (patterns[CLOCKDIVIDERGValue][stepCounterG.getValue()])
gateG.start();
if (patterns[CLOCKDIVIDERBValue][stepCounterB.getValue()])
gateB.start();
if (patterns[MAINCLKDIVValue][stepCounterM.getValue()])
gateSlv.start();
}
void closeTriggers()
{
gateM.compute();
gateR.compute();
gateG.compute();
gateB.compute();
gateSlv.compute();
}
void updateDacs()
{
int16_t midiNote;
int16_t voltageEquivalent;
//red dac
if (patterns[CLOCKDIVIDERRValue][stepCounterR.getValue()])
{
//stretch to (5 octaves) * 4 range
midiNote = map(red, MINCOLOR, MAXCOLOR, 1, 240);
//red scale invert
if (OCTAVEQUANTITYRValue > 0)
red = 240 - red;
//shrink to user selected octaves quantity
midiNote = map(red, 1, 240, 1, 12 * abs(OCTAVEQUANTITYRValue));
//shift user selected notes/octaves quantity
midiNote = midiNote + NOTESHIFTRValue;
//assure 1 to 60 range midi note
midiNoteR = constrain(midiNote, 1, 60);
voltageEquivalent = map(midiNoteR, 0, 60, 0, 4095);
digitalWrite(DACRPIN_SEL1, HIGH);
dac.setVoltage(voltageEquivalent, false);
digitalWrite(DACRPIN_SEL1, LOW);
}
//green dac
if (patterns[CLOCKDIVIDERGValue][stepCounterG.getValue()])
{
//stretch to (5 octaves) * 4 range
midiNote = map(green, MINCOLOR, MAXCOLOR, 1, 240);
//green scale invert
if (OCTAVEQUANTITYGValue > 0)
green = 240 - green;
//shrink to user selected octaves quantity
midiNote = map(green, 1, 240, 1, 12 * abs(OCTAVEQUANTITYGValue));
//shift user selected notes/octaves quantity
midiNote = midiNote + NOTESHIFTGValue;
//assure 1 to 60 range midi note
midiNoteG = constrain(midiNote, 1, 60);
voltageEquivalent = map(midiNoteG, 0, 60, 0, 4095);
digitalWrite(DACGPIN_SEL2, HIGH);
dac.setVoltage(voltageEquivalent, false);
digitalWrite(DACGPIN_SEL2, LOW);
}
//blue dac
if (patterns[CLOCKDIVIDERBValue][stepCounterB.getValue()])
{
//stretch to (5 octaves) * 4 range
midiNote = map(blue, MINCOLOR, MAXCOLOR, 1, 240);
//blue scale invert
if (OCTAVEQUANTITYBValue > 0)
blue = 240 - blue;
//shrink to user selected octaves quantity
midiNote = map(blue, 1, 240, 1, 12 * abs(OCTAVEQUANTITYBValue));
//shift user selected notes/octaves quantity
midiNote = midiNote + NOTESHIFTBValue;
//assure 1 to 60 range midi note
midiNoteB = constrain(midiNote, 1, 60);
voltageEquivalent = map(midiNoteB, 0, 60, 0, 4095);
digitalWrite(DACBPIN_SEL3, HIGH);
dac.setVoltage(voltageEquivalent, false);
digitalWrite(DACBPIN_SEL3, LOW);
}
}
void readColors()
{
//read sensor colors
//disable this if sensor not connected
digitalWrite(S2PIN, LOW);
digitalWrite(S3PIN, LOW);
red = pulseIn(OUTCOLORPIN, digitalRead(OUTCOLORPIN) == HIGH ? LOW : HIGH);
digitalWrite(S3PIN, HIGH);
blue = pulseIn(OUTCOLORPIN, digitalRead(OUTCOLORPIN) == HIGH ? LOW : HIGH);
digitalWrite(S2PIN, HIGH);
green = pulseIn(OUTCOLORPIN, digitalRead(OUTCOLORPIN) == HIGH ? LOW : HIGH);
red = constrain(red, MINCOLOR, MAXCOLOR);
green = constrain(green, MINCOLOR, MAXCOLOR);
blue = constrain(blue, MINCOLOR, MAXCOLOR);
printColors();
}
void printAllControls()
{
Serial.print(OCTAVEQUANTITYRValue);
Serial.print(", ");
Serial.print(OCTAVEQUANTITYGValue);
Serial.print(", ");
Serial.print(OCTAVEQUANTITYBValue);
Serial.println(".");
Serial.print(NOTESHIFTRValue);
Serial.print(", ");
Serial.print(NOTESHIFTGValue);
Serial.print(", ");
Serial.print(NOTESHIFTBValue);
Serial.println(".");
Serial.print(CLOCKDIVIDERRValue);
Serial.print(", ");
Serial.print(CLOCKDIVIDERGValue);
Serial.print(", ");
Serial.print(CLOCKDIVIDERBValue);
Serial.println(".");
Serial.print(swIntClockSrc.active());
Serial.print(", ");
Serial.print(sampleSpeedInterval);
Serial.println(".");
Serial.print(midiNoteR);
Serial.print(", ");
Serial.print(midiNoteG);
Serial.print(", ");
Serial.print(midiNoteB);
Serial.println(".");
Serial.println("---------------------------");
}
void printMidiNotes()
{
Serial.print(midiNoteR);
Serial.print(" , ");
Serial.print(midiNoteG);
Serial.print(" , ");
Serial.print(midiNoteB);
Serial.println(" ;");
}
void printColors()
{
Serial.print("red:");
Serial.print(red);
Serial.print(" ,green:");
Serial.print(green);
Serial.print(" ,blue:");
Serial.print(blue);
Serial.println(" ;");
}