-
Notifications
You must be signed in to change notification settings - Fork 0
/
LaserHarp.ino
1012 lines (877 loc) · 29.2 KB
/
LaserHarp.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
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//TODO// Objectify those strings, yeah. With absolute precision.
#include <Streaming.h>
#include "SPI.h"
#include "Adafruit_WS2801.h"
/*++++++++++++++++++++++++++++++++++++++++++++++++++
Constraints
//*************** LASER SWITCH PINS ***************/
#define DEBUG false
#define DEBUG_STATS false
#define NUMBER_STRINGS 7
#define NUMBER_PHOTOCELLS 7
#define NUMBER_RANGE_FINDERS 2
/*++++++++++++++++++++++++++++++++++++++++++++++++++
PINS PINS PINS
//*************** LASER SWITCH PINS ***************/
#define PINS_LASER_ONE 28
#define PINS_LASER_TWO 30
#define PINS_LASER_THREE 32
#define PINS_LASER_FOUR 28
#define PINS_LASER_FIVE 30
#define PINS_LASER_SIX 32
#define PINS_LASER_SEVEN 28
//*************** STRING RANGE FINDERS ***************//
#define PINS_RANGE_TRIG_ONE 23 // Trigger Pin
#define PINS_RANGE_TRIG_TWO 25 // Trigger Pin
#define PINS_RANGE_ECHO_ONE 22
#define PINS_RANGE_ECHO_TWO 24
//*************** USERRANGE TRIG & ECHO ***************//
#define PINS_USER_RANGE_TRIG 27
#define PINS_USER_RANGE_ECHO 26
//*************** PHOTOCELLS ***************//
#define PINS_PC_ONE 8
#define PINS_PC_TWO 9
#define PINS_PC_THREE 10
#define PINS_PC_FOUR 8
#define PINS_PC_FIVE 9
#define PINS_PC_SIX 10
#define PINS_PC_SEVEN 10
#define PINS_PC_AMBIENT 10
//*************** KNOB PINS ***************//
#define PINS_KNOB_OCTAVE 0
#define PINS_KNOB_NOTE 1
#define PINS_KNOB_SCALE 2
//*************** LIGHT PINS ***************//
#define PINS_LIGHTS_DATA 0
#define PINS_LIGHTS_CLOCK 1
//*************** MODE BUTTON PIN ***************//
#define PINS_MODE 11
/*++++++++++++++++++++++++++++++++++++++++++++++++++
MIDI
//*************** LIMITS ***************/
#define MIDI_NOTE_LOW 1
#define MIDI_NOTE_HIGH 115
//*************** MIDI VALUES ***************/
#define OFF 0
#define ON 1
#define WAIT 2
#define CONTINUOUS 11
#define NOTEON 0x90
#define NOTEOFF 0x80
/*++++++++++++++++++++++++++++++++++++++++++++++++++
RANGE
//*************** LIMITS ***************/
#define THRESHOLD_PING_RANGE 50
/*++++++++++++++++++++++++++++++++++++++++++++++++++
INTERACTIVITY
//*************** LIMITS ***************/
#define IDLE_THRESHOLD 30000
#define RANGE_MAX 114 //CHANGE!
#define RANGE_MIN 1 //CHANGE!
#define LASER_PC_THRESH 600 //CHANGE!
#define TOTAL_MODES 3
#define DEFAULT_BASE_NOTE 60
#define DEFAULT_SCALE 0
//*************** DATA CACHE ***************//
int stringRange[NUMBER_STRINGS];
int stringNote[NUMBER_STRINGS];
int PCValues[NUMBER_STRINGS];
int rangeRawValue[NUMBER_STRINGS];
int rangeMidiValue[NUMBER_STRINGS];
int midiStage[NUMBER_STRINGS];
boolean stringState[NUMBER_STRINGS];
boolean holding[NUMBER_STRINGS];
int knobRawValue[3];
//*************** User Tracking ***************//
long lastInteraction;
int lastButtonState;
boolean isIdle;
boolean active = false;
boolean booted = false;
//*************** Stepper for range tweens ***************//
boolean stepping[NUMBER_STRINGS];
//*************** Midi Variables ***************//
int action = ON; //1 =note off ; 2=note on ; 3= wait
byte incomingByte;
byte note;
byte velocity;
int pot;
//*************** Pins ***************//
uint8_t pinsPC[7];
uint8_t pinsKnob[3];
//*************** Music Math ***************//
int baseNote = DEFAULT_BASE_NOTE;
int harpScale = DEFAULT_SCALE;
//*************** Ambient Light Sensor (532nm sensitive) ***************//
int PCThreshAdjusted, subtractAmbience;
//*************** Switch Modes! ***************//
int mode = 0; // 0 = momentary, 2 = on/off (sampler)
//*************** Debugging ***************//
long debugLast = millis();
int debugEvery = 7*1000;
void setup() {
setupStepper();
setupPhotocells();
setupRange();
setScale();
setupMidi();
Serial.begin(31250); //start serial with midi baudrate 31250
setupKnobs();
interrupts();
// setupLasers();
}
void update(){}
void loop() {
if(booted==false) { startupLasers(); booted==true; }
pollKnobs(); //Check for octave, key, scale, mode values
pollSensors(); //Check photocells and find range.
sendMidi(); //Send midi signal.
if(DEBUG) debugReport();
}
/**************************************
**
LASERS
with Sharks.
**
***************************************/
int totalLasers = NUMBER_STRINGS;
long cooldownLast = millis();
int cooldownEvery = 60*10;
int cooldownPeriod = 60;
long startupLast = millis();
boolean coolingDown = false;
boolean laserState[NUMBER_STRINGS];
int lasers[NUMBER_STRINGS];
void startupLasers(){
lasers[0] = PINS_LASER_ONE;
lasers[1] = PINS_LASER_TWO;
lasers[2] = PINS_LASER_THREE;
lasers[3] = PINS_LASER_FOUR;
lasers[4] = PINS_LASER_FIVE;
lasers[5] = PINS_LASER_SIX;
lasers[6] = PINS_LASER_SEVEN;
for(int l = 0;l < totalLasers; l++) {
pinMode(lasers[l], OUTPUT);
}
lasersOn(500);
}
void shutdownLasers(){
}
//*************** LASERS: TURN A LASER ON ***************//
void laserOn(int laser){
if(DEBUG) LOG(1, "(ON) Laser", laser);
digitalWrite(lasers[laser], HIGH);
laserState[laser] = true;
}
//*************** LASERS: TURN A LASER OFF ***************//
void laserOff(int laser){
if(DEBUG) LOG(1, "(OFF) Laser", laser);
digitalWrite(lasers[laser], LOW);
laserState[laser] = false;
}
//*************** LASERS: TURN ALL ON ***************//
void lasersOn(){
for(int l = 0; l < totalLasers; l++) {
laserOn(l);
}
lasersCooldownSet("startupLast", millis());
}
//*************** LASERS: TURN ALL ON WITH DELAY ***************//
void lasersOn(int d){
for(int l = 0; l < totalLasers; l++) {
laserOn(l);
delay(d);
}
lasersCooldownSet("startupLast", millis());
}
//*************** LASERS: TURN ALL OFF ***************//
void lasersOff(){
for(int l = 0; l < totalLasers; l++) {
laserOff(l);
}
lasersCooldownSet("cooldownLast", millis());
}
//*************** LASERS: TURN ALL OFF WITH DELAY ***************//
void lasersOff(int d){
for(int l = 0; l < totalLasers; l++) {
laserOff(l);
delay(d);
}
lasersCooldownSet("cooldownLast", millis());
}
//*************** CHECK LASER COOLDOWN CYCLE ***************//
void lasersCheckCooldown(){
long now = millis();
if(now-startupLast > cooldownEvery && !coolingDown) {
lasersCooldown();
} else if(now-cooldownLast > cooldownPeriod && coolingDown) {
lasersCooled();
} else {
//do nothing
}
}
void lasersCooldownSet(const char* type, long millis) {
if(type == "cooldownLast") cooldownLast = millis;
if(type == "startupLast") startupLast = millis;
}
void lasersCooldown(){
//blink 3 times then cooldown
for(int blink=0;blink<3;blink++){
lasersOff( 20 ); delay(250);
lasersOn( 10 ); delay(500);
}
if(DEBUG) LOG("Cooling Down", "Lasers", 7);
lasersOff(500); delay(400);
cooldownLast = millis();
}
void lasersCooled(){
lasersOn( 250 );
if(DEBUG) LOG("Cooling Finished", "Lasers", 7);
}
/**************************************
**
Photocells
The tiny big section for most desired feature
**
***************************************/
void setupPhotocells(){
pinsPC[0] = PINS_PC_ONE;
pinsPC[1] = PINS_PC_TWO;
pinsPC[2] = PINS_PC_THREE;
pinsPC[3] = PINS_PC_FOUR;
pinsPC[4] = PINS_PC_FIVE;
pinsPC[5] = PINS_PC_SIX;
pinsPC[6] = PINS_PC_SEVEN;
}
/**************************************
**
RANGE FINDER
The tiny big section for most desired feature
**
***************************************/
//*************** RANGE FINDERS: SETUP PINS ***************//
// SIMPLIFY!, memory hog...
uint8_t pinsRangeTrigger[NUMBER_STRINGS];
uint8_t pinsRangeEcho[NUMBER_STRINGS];
//*************** RANGE FINDERS: SETUP PINS FOR INPUT AND OUTPUT ***************//
long rangeMillisSince[NUMBER_STRINGS], rangeMillisLast[NUMBER_STRINGS];
/* This setups up the range finders */
void setupRange(){
for(int s=0;s<NUMBER_STRINGS;s++){
if(s<4) {
pinsRangeTrigger[s] = uint8_t(PINS_RANGE_TRIG_ONE);
pinsRangeEcho[s] = uint8_t(PINS_RANGE_ECHO_ONE);
} else {
pinsRangeTrigger[s] = uint8_t(PINS_RANGE_TRIG_TWO);
pinsRangeEcho[s] = uint8_t(PINS_RANGE_ECHO_TWO);
}
pinMode(pinsRangeTrigger[s], OUTPUT);
pinMode(pinsRangeEcho[s], INPUT);
rangeMillisLast[s] = millis();
}
}
/* This pings the range finders, which sends an ultra sonic wave, pauses and then recieves it microseconds later */
void pollRange(int s){
rangeMillisSince[s] = millis() - rangeMillisLast[s];
if(rangeMillisSince[s] > THRESHOLD_PING_RANGE) {
if(DEBUG) LOG(pinsRangeTrigger[s], "(PING) Range for String/Laser ", s);
digitalWrite(pinsRangeTrigger[s], LOW);
delayMicroseconds(2);
digitalWrite(pinsRangeTrigger[s], HIGH);
delayMicroseconds(10);
digitalWrite(pinsRangeTrigger[s], LOW);
long duration = pulseIn(pinsRangeEcho[s], HIGH);
rangeRawValue[s] = microsecondsToCentimeters(duration);
if(DEBUG) LOG(pinsRangeEcho[s], "(PING) Range for String/Laser ", s);
rangeMillisLast[s] = millis();
}
rangeMidiValue[s] = convertDistanceToMidi(rangeRawValue[s]);
}
int getStringRange(int s){
return limitRange(rangeRawValue[s]);
}
int convertDistanceToMidi(int distance){
return NORMALIZE(distance, 1, RANGE_MAX, MIDI_NOTE_LOW, MIDI_NOTE_HIGH);
}
int limitRange(int range){
return (range > RANGE_MAX) ? RANGE_MAX : range;
}
long microsecondsToCentimeters(long microseconds)
{
// The speed of sound is 340 m/s or 29 microseconds per centimeter.
// The ping travels out and back, so to find the distance of the
// object we take half of the distance travelled.
return microseconds / 29 / 2;
}
/**************************************
**
SAFETY
Without them the lasers are just pretty (and debateably dangerous)
**
***************************************/
int stringTrigDur[NUMBER_STRINGS];
int stringActivatedTime[NUMBER_STRINGS];
long safetyOn[NUMBER_STRINGS];
int stringOn;
#define SAFETY_LASER_DURATION 10000
void safetySecond(int s, boolean reset){
long now = millis();
if(stringState[s] && laserState[s]) {
stringTrigDur[s] = now-stringActivatedTime[s];
} else if(!stringState[s]) {
stringTrigDur[s] = 0;
}
if(safetyOn[s]) {
if(now-safetyOn[s]>1*1000) { //Put it on after 1 second
if(now-safetyOn[s]>1*1000+5 && stringState[s] && laserState[s]) laserOff(s); //Shortly after, check if there is something still obstructing the laser.
else laserOn(s); //First... turn the laser on
if(now-safetyOn[s]>5*1000+100 && !stringState[s] && laserState[s]){ //If the obstructure is no longer there, it continues as normal.
laserOn(s);
safetyOn[s]=0;
}
//Otherwise, it will try it again and again and again until the obstruction has been removed.
}
}
if(stringTrigDur[s] > SAFETY_LASER_DURATION) { //Something has blocked the laser for 10 seconds, turn on the Safety
laserOff(s);
safetyOn[s] = now;
}
}
/**************************************
**
MODE
Without them the lasers are just pretty (and debateably dangerous)
**
***************************************/
void setupMode(){
pinMode(PINS_MODE, INPUT);
}
void pollMode(){
int buttonState = digitalRead(PINS_MODE);
// compare the buttonState to its previous state
if (buttonState != lastButtonState) {
// if the state has changed, increment the counter
if (buttonState == HIGH) {
// setMode();
//DO!
}
//Ohterwise do nothing.
}
// save the current state as the last state,
//for next time through the loop
lastButtonState = buttonState;
}
//*************** Midi Config ***************//
void Midi_Send(byte cmd, byte data1, byte data2) {
Serial.write(cmd);
Serial.write(data1);
Serial.write(data2);
if(DEBUG) {
LOG(cmd, "Sending MIDI cmd");
LOG(data1, "Sending MIDI data1");
LOG(data2, "Sending MIDI data2");
}
}
void sendMidi(){
switch( mode ){
case 1: //toggle
for(int s = 0; s < NUMBER_STRINGS; s++){
if(stringState[s]) {
if(DEBUG) LOG(s, "String Triggered (Toggle Mode)");
if(!midiStage[s]) { //is presently off
Midi_Send(NOTEON, stringNote[s], rangeMidiValue[s]);
Midi_Send(0xB0, 22+s, stringRange[s]);
midiStage[s] = 1; //on
stringState[s] = false;
if(DEBUG) LOG(stringNote[s], "(toggle) Sending Note ON", s);
} else { //is presently on
Midi_Send(NOTEOFF, stringNote[s], rangeMidiValue[s]);
midiStage[s] = 0; //off
stringState[s] = false;
if(DEBUG) LOG(stringNote[s], "(toggle) Sending Note OFF", s);
}
} else {
midiStage[s] = 2; //wait
if(DEBUG) LOG(stringNote[s], "(toggle) Waiting...", s);
}
}
break;
default: //momentary
for(int s = 0; s < NUMBER_STRINGS; s++){
if(stringState[s] && midiStage[s]==0) {
Midi_Send(NOTEON, stringNote[s], rangeMidiValue[s]);
Midi_Send(0xB0, 22+s, rangeMidiValue[s]);
midiStage[s] = ON;
stringState[s] = false;
if(DEBUG) LOG(stringNote[s], "(Momentary) Sending Note ON", s);
} else if( !stringState[s] && midiStage[s]) { //is not trigged but current stage is ON or WAIT.
Midi_Send(NOTEOFF, stringNote[s], rangeMidiValue[s]);
midiStage[s] = OFF;
if(DEBUG) LOG(stringNote[s], "(Momentary) Sending Note OFF", s);
} else if(stringState[s] && midiStage[s] == ON) {
midiStage[s] = WAIT;
Midi_Send(0xB0, 22+s, rangeMidiValue[s]);
if(DEBUG) LOG(stringNote[s], "(Momentary) Waiting...", s);
} else if(stringState[s] && midisStage[s] == WAIT) {
Midi_Send(0xB0, 22+s, rangeMidiValue[s]);
}
}
}
}
void setupMidi(){
for(int s=0;s>NUMBER_STRINGS;s++){ stringState[s] = false; }
}
void midiLoopback(){ }
long lastPoll = millis();
void pollSensors(){
if(DEBUG) LOG(millis()-lastPoll, "(Polling Sensors) Cycling every");
for(int s=0;s<NUMBER_STRINGS;s++) {
if( pollString(s) ) { //does a bunch of math, sets some stuff and returns true/false for each string.
pollRange(s); //This will create latency, so it's only running every 50 milliseconds. 38 microseconds * 7 is a fraction of a Milli, but still.
pollAmbientLight();
safetySecond(s, false);
// if(stringState[s])
} else {
safetySecond(s, true);
}
}
lastPoll = millis();
//
}
boolean pollString(int s){
PCValues[s] = analogRead(pinsPC[s]);
PCThreshAdjusted = LASER_PC_THRESH-subtractAmbience; //subtract ambience from Threshold (which is taken in complete darkness)
stringState[s] = ( PCValues[s] < PCThreshAdjusted );
if(!stringState[s]) { stringActivatedTime[s] = 0; stepStop(s); } //It's not triggered.
else if(!stringActivatedTime[s] && stringState[s]){ //It's just been activated, for the very first time (as long as we know.)
if(DEBUG) LOG(s, "(State) Triggered");
long now = millis();
stringActivatedTime[s] = now;
lastInteraction = now;
}
return stringState[s];
}
int knobValue[3];
int knobRawValueCache[3];
int knobPollEvery = 100;
int knobPollLast = millis();
void setupKnobs(){
pinMode(PINS_KNOB_OCTAVE, OUTPUT);
pinMode(PINS_KNOB_NOTE, OUTPUT);
pinMode(PINS_KNOB_SCALE, OUTPUT);
setupKnobLights();
}
void pollKnobs(){
long now = millis();
if(now-knobPollLast>knobPollEvery) {
boolean tripped = false;
knobRawValue[0] = analogRead(PINS_KNOB_OCTAVE);
knobRawValue[1] = analogRead(PINS_KNOB_NOTE);
knobRawValue[2] = analogRead(PINS_KNOB_SCALE);
int n = (knobRawValue[0] != knobRawValueCache[0]);
int o = (knobRawValue[1] != knobRawValueCache[1]);
int s = (knobRawValue[2] != knobRawValueCache[2]);
tripped = (n || o || s);
if(tripped) {
if(DEBUG) LOG(3, "(Knobs) Value has changed");
int note = (n) ? NORMALIZE(knobRawValue[0], 0, 1024, 1, 12) : knobValue[0];
int octave = (o) ? NORMALIZE(knobRawValue[1], 0, 1024, 0, 7) : knobValue[1];
int scale = (s) ? NORMALIZE(knobRawValue[2], 0, 1024, 0, 12) : knobValue[2];
setBaseNote(octave*12+note);
setHarpScale(scale);
knobValue[0] = note;
knobValue[1] = octave;
knobValue[2] = scale;
if(DEBUG) LOG(note, "(Knob 1) Note:");
if(DEBUG) LOG(octave, "(Knob 2) Octave:");
if(DEBUG) LOG(octave, "(Knob 3) Scale:");
refreshLights();
memcpy(knobRawValueCache, knobRawValue, 3);
}
}
}
Adafruit_WS2801 knobLights = Adafruit_WS2801(3, PINS_LIGHTS_DATA, PINS_LIGHTS_CLOCK);
void setupKnobLights(){
knobLights.begin();
knobLights.show();
}
void refreshLights(){ //don't need note...
//note
int nR = NORMALIZE(baseNote, MIDI_NOTE_LOW, MIDI_NOTE_HIGH, 0, 255);
int nB = NORMALIZE(baseNote, MIDI_NOTE_LOW, MIDI_NOTE_HIGH, 255, 0);
uint32_t nRGB = color(nR, 0, nB); //From Blue-Red, entire scale. Changes color when octave changes color.
//octave
int oR = NORMALIZE(knobValue[1], 0, 7, 0, 255);
int oB = NORMALIZE(knobValue[1], 0, 7, 255, 0);
uint32_t oRGB = color(oR, 0, oB); //From Blue-Red
//scale
int sR = NORMALIZE(knobValue[2], 0, 12, 50, 10);
int sB = NORMALIZE(knobValue[2], 0, 12, 120, 40);
int sG = NORMALIZE(knobValue[2], 0, 12, 100, 255);
uint32_t sRGB = color(sR, sG, sB); //From Don't know. to who knows.
//Happy Pixels
knobLights.setPixelColor(0, nRGB);
knobLights.setPixelColor(1, oRGB);
knobLights.setPixelColor(2, sRGB);
knobLights.show();
}
uint32_t color(byte r, byte g, byte b)
{
uint32_t c;
c = r;
c <<= 8;
c |= g;
c <<= 8;
c |= b;
return c;
}
//*********************
// Chords
// **** **** **********
void setBaseNote(int n){
if(DEBUG) LOG(n, "(Notes) Setting Basenote");
baseNote = n;
if(baseNote > MIDI_NOTE_HIGH) { baseNote = MIDI_NOTE_HIGH; }
if(baseNote < MIDI_NOTE_LOW) { baseNote = MIDI_NOTE_LOW; }\
if(DEBUG) LOG(baseNote, "(Notes) Basenote set");
}
void setHarpScale(char scale){
harpScale = scale;
setScale();
if(DEBUG) LOG(scale, "(Notes) Scale has been set.");
}
void setScale(){
if(DEBUG) LOG("harpscale", "(Notes) Setting scale.");
switch(harpScale){
//TODO
//Increasing/decreasing scale?
case 0: //W – W – H – W – W – W – H //ionian/major
stringNote[0] = baseNote;
stringNote[1] = stringNote[0] + 2;
stringNote[2] = stringNote[1] + 2;
stringNote[3] = stringNote[2] + 1;
stringNote[4] = stringNote[3] + 2;
stringNote[5] = stringNote[4] + 2;
stringNote[6] = stringNote[5] + 2;
break;
case 1: //w h w w h w w //MINOR Aeolian
stringNote[0] = baseNote;
stringNote[1] = stringNote[0] + 2;
stringNote[2] = stringNote[1] + 1;
stringNote[3] = stringNote[2] + 2;
stringNote[4] = stringNote[3] + 2;
stringNote[5] = stringNote[4] + 1;
stringNote[6] = stringNote[5] + 2;
break;
case 2: //harmonic minor W - H - W - W - H - W+H - H
stringNote[0] = baseNote;
stringNote[1] = stringNote[0] + 2;
stringNote[2] = stringNote[1] + 1;
stringNote[3] = stringNote[2] + 2;
stringNote[4] = stringNote[3] + 2;
stringNote[5] = stringNote[4] + 1;
stringNote[6] = stringNote[5] + 3;
case 3: //melodic minor W - H - W - W - W - W - H
stringNote[0] = baseNote;
stringNote[1] = stringNote[0] + 2;
stringNote[2] = stringNote[1] + 1;
stringNote[3] = stringNote[2] + 2;
stringNote[4] = stringNote[3] + 2;
stringNote[5] = stringNote[4] + 2;
stringNote[6] = stringNote[5] + 2;
case 4: //w h w w w h w - dorian
stringNote[0] = baseNote;
stringNote[1] = stringNote[0] + 2;
stringNote[2] = stringNote[1] + 1;
stringNote[3] = stringNote[2] + 2;
stringNote[4] = stringNote[3] + 2;
stringNote[5] = stringNote[4] + 2;
stringNote[6] = stringNote[5] + 1;
break;
case 5: //h w w w h w w Phyrigian
stringNote[0] = baseNote;
stringNote[1] = stringNote[0] + 1;
stringNote[2] = stringNote[1] + 2;
stringNote[3] = stringNote[2] + 2;
stringNote[4] = stringNote[3] + 2;
stringNote[5] = stringNote[4] + 1;
stringNote[6] = stringNote[5] + 2;
break;
case 6: //w w w h w w h Lydian
stringNote[0] = baseNote;
stringNote[1] = stringNote[0] + 2;
stringNote[2] = stringNote[1] + 2;
stringNote[3] = stringNote[2] + 2;
stringNote[4] = stringNote[3] + 1;
stringNote[5] = stringNote[4] + 2;
stringNote[6] = stringNote[5] + 2;
break;
case 7: //W - W - W - H - W - H - W Lydian Flat Seven
stringNote[0] = baseNote;
stringNote[1] = stringNote[0] + 2;
stringNote[2] = stringNote[1] + 2;
stringNote[3] = stringNote[2] + 2;
stringNote[4] = stringNote[3] + 1;
stringNote[5] = stringNote[4] + 2;
stringNote[6] = stringNote[5] + 1;
break;
case 8: //Mixolydian w w h w w h w
stringNote[0] = baseNote;
stringNote[1] = stringNote[0] + 2;
stringNote[2] = stringNote[1] + 2;
stringNote[3] = stringNote[2] + 1;
stringNote[4] = stringNote[3] + 2;
stringNote[5] = stringNote[4] + 2;
stringNote[6] = stringNote[5] + 1;
break;
case 9: //w w h w w h w - Locrian
stringNote[0] = baseNote;
stringNote[1] = stringNote[0] + 2;
stringNote[2] = stringNote[1] + 2;
stringNote[3] = stringNote[2] + 1;
stringNote[4] = stringNote[3] + 2;
stringNote[5] = stringNote[4] + 2;
stringNote[6] = stringNote[5] + 1;
break;
case 10: //W - W - W+H - W - W+H - Penta Major
stringNote[0] = baseNote;
stringNote[1] = stringNote[0] + 2;
stringNote[2] = stringNote[1] + 2;
stringNote[3] = stringNote[2] + 3;
stringNote[4] = stringNote[3] + 2;
stringNote[5] = stringNote[4] + 3;
stringNote[6] = stringNote[5] + 4;
break;
case 11: //W+H - W - W - W+H + W - Penta Minor
stringNote[0] = baseNote;
stringNote[1] = stringNote[0] + 3;
stringNote[2] = stringNote[1] + 2;
stringNote[3] = stringNote[2] + 2;
stringNote[4] = stringNote[3] + 3;
stringNote[5] = stringNote[4] + 2;
stringNote[6] = stringNote[5] + 5;
break;
case 12: //W+H - H - H - H - H - W+H + W - blues
stringNote[0] = baseNote;
stringNote[1] = stringNote[0] + 3;
stringNote[2] = stringNote[1] + 1;
stringNote[3] = stringNote[2] + 1;
stringNote[4] = stringNote[3] + 1;
stringNote[5] = stringNote[4] + 1;
stringNote[6] = stringNote[5] + 3;
break;
case 13: //whole tone
stringNote[0] = baseNote;
stringNote[1] = stringNote[0] + 2;
stringNote[2] = stringNote[1] + 2;
stringNote[3] = stringNote[2] + 2;
stringNote[4] = stringNote[3] + 2;
stringNote[5] = stringNote[4] + 2;
stringNote[6] = stringNote[5] + 2;
break;
}
}
/**************************************
**
USABILITY AND INTERACTIONS
The tiny big section for most desired feature
**
***************************************/
boolean cacheIdle;
boolean checkIdle(){
long now = millis();
//
if(now - lastInteraction > IDLE_THRESHOLD) //Is anybody using the damn thing?
{ isIdle = true; }
else
{ isIdle = false; }
//
if(isIdle && !cacheIdle) { //If isIdle was just set as true, and it's not already idling.
lasersOff(999);
if(DEBUG) LOG(lastInteraction, "(Idle) Presently Idle.");
return true;
} else if (cacheIdle && !isIdle) { //if it was idle last time we checked, but it's not anymore.
lasersOn(200);
if(DEBUG) LOG(now, "(Idle) Idle Broken, activity detected.");
return false;
} //else do nothing because it's idle or running.
//
cacheIdle = isIdle; //So we have something to match against.
}
//*********************
// Ambient Light
// **** **** **********
// int subtractAmbience = 0;
int ambientPollEvery = 1000*3;
int ambientPollLast = millis();
void pollAmbientLight(){
long now = millis();
if(now-ambientPollLast>ambientPollEvery) {
subtractAmbience = analogRead(PINS_PC_AMBIENT);
if(DEBUG) LOG(subtractAmbience, "(Ambient Light) Subtracting ambient light in 532nm wavelength.");
}
}
//*********************
// Utilites
// **** **** **********
int NORMALIZE(int set, int rangeLow, int rangeHigh, int toMin, int toMax) {
// toMin = toMin || 0;
// toMax = toMax || 1000;
int result = toMin + (set-rangeLow)*(toMax-toMin)/(rangeHigh-rangeLow);
return result;
}
/**************************************
**
STEPPER
Oh shit, I wanted to avoid this. le sigh
**
***************************************/
int stepIndex[8];
int stepNumber = 14;
int stepDuration = 1000;
int stepEvery = stepDuration/stepNumber;
// boolean stepping[NUMBER_STRINGS];
//
int stepTo[NUMBER_STRINGS];
int stepFrom[NUMBER_STRINGS];
int stepCurrent[NUMBER_STRINGS];
int stepDistance[NUMBER_STRINGS];
int stepMillis[NUMBER_STRINGS];
#define STEPPER_THRESHOLD 50 //Must be more than this big of a gap to
void setupStepper(){
memset(stepping, false, NUMBER_STRINGS);
}
int step(int s, int from, int to){
long now = millis();
if(stepping[s]){
if(now-stepMillis[s] > stepEvery) { //is it time to do a step
if(stepIndex[s] < stepNumber) { //have we finished our steps
if(DEBUG) LOG(s, "(Stepper) Step");
stepDistance[s] = (stepFrom[s] - stepTo[s]) / stepNumber;
stepCurrent[s] = stepDistance[s] * (stepIndex[s]+1);
stepMillis[s] = now;
stepIndex[s]++;
return stepCurrent[s];
} else { //give them their number, and make sure it doesn't happen again.
if(DEBUG) LOG(s, "(Stepper) Steps Complete");
stepping[s] = false;
stepReset(s, "all");
stepIndex[s] = -1; //this flags that it has already been completed.
return to;
}
}
} else { //First step!
if(!stepStart(s, from, to)) return to; //If the gap isn't big enough, should have never gotten here but still.
}
}
boolean stepStart(int s, int from, int to){
if(stepIndex[s] == -1) return false;
if(DEBUG) { LOG(s, "(Stepper) Initialized for String/Laser"); }
stepReset(s, "all");
int difference = to-from;
if(difference < 0) difference=difference*1;
if(difference > STEPPER_THRESHOLD) {
stepFrom[s] = from;
stepTo[s] = to;
stepping[s] = true;
return true;
} else {
stepReset(s, "all");
}
}
void stepStop(int s){
stepReset(s, "all");
}
void stepReset(int s, const char* type){
if(DEBUG) { LOG(s, "(Stepper) Reset"); }
if(type == "current") stepCurrent[s] = 0;
if(type == "begin") stepFrom[s] = 0;
if(type == "target") stepTo[s] = 0;
if(type == "distance") stepDistance[s] = 0;
if(type == "stepping") stepping[s] = false;
if(type == "all") {
stepCurrent[s] = 0;
stepFrom[s] = 0;
stepTo[s] = 0;
stepIndex[s] = 0;
stepMillis[s] = 0;
stepping[s] = false;
}
}
void stepResetAll(int s){
for(int s=0;s<NUMBER_STRINGS;s++) { stepReset(s, "all"); }
}
void stepSet(const char* type, int target, int s){
if(type == "current") stepCurrent[s] = target;
if(type == "begin") stepFrom[s] = target;
if(type == "target") stepTo[s] = target;
if(type == "distance") stepDistance[s] = target;
if(type == "stepping") stepping[s] = target;
}
//*********************
// Debugging
// **** **** **********
void LOG(int in ){
Serial.println(in);
}
void LOG(int in, const char* label){
Serial.print(label); Serial.print(": "); Serial.println(in);
}
void LOG(const char* in, const char* label){
Serial.print(label); Serial.print(": "); Serial.println(in);
}
void LOG(const char* in, const char* label, int labelID){
Serial.print(label); Serial.print(" "); Serial.print(labelID); Serial.print(": "); Serial.println(in);
}
void LOG(int in, const char* label, int labelID){
Serial.print(label); Serial.print(" "); Serial.print(labelID); Serial.print(": "); Serial.println(in);
}
void debugReport(){
long now = millis();
if(now-debugLast > debugEvery) {
Serial.println("!!!++++++++++++++++++++++++++++++++++++++++++!!!");
Serial.println("!!!++++++ LASER HARP 2014 +++++!!!");
Serial.println("!!!++++++++++++++++++++++++++++++++++++++++++!!!");
Serial.println(" "); Serial.println(" "); Serial.println(" "); Serial.println(" "); Serial.println(" "); Serial.println(" ");
Serial.println("LIVE STATS"); Serial.println(" ");
for(int s=0;s<NUMBER_STRINGS;s++) {
Serial.print("~~~~~~~~~~ String ");
Serial.print(s);
Serial.println(" -~~~~~~~~~~~~~~~~~~~~~~");
Serial.print("Note: "); Serial.println(stringNote[s]);
Serial.print("Midi Stage: "); Serial.println(midiStage[s]);
Serial.print("Triggered?: "); Serial.println(stringState[s]);
Serial.print("Photocell: "); Serial.println(PCValues[s]);
Serial.print("Range: "); Serial.println(stringRange[s]);
Serial.print("Range (Raw Values): "); Serial.println(rangeRawValue[s]);
}
for(int k=0;k<3;k++) {
Serial.print("Knob: "); Serial.println(knobRawValue[k]);
}
Serial.print("Last Interaction: "); Serial.println(lastInteraction);
Serial.println(" "); Serial.println(" "); Serial.println(" "); Serial.println(" "); Serial.println(" "); Serial.println(" ");
Serial.println("PINS"); Serial.println(" ");
Serial.print("TOTAL STRINGS: "); Serial.println(NUMBER_STRINGS);
Serial.print("TOTAL RANGES: "); Serial.println(NUMBER_RANGE_FINDERS);
Serial.print("LASER 1: "); Serial.println(PINS_LASER_ONE);
Serial.print("LASER 2: "); Serial.println(PINS_LASER_TWO);
Serial.print("LASER 3: "); Serial.println(PINS_LASER_THREE);
Serial.print("LASER 4: "); Serial.println(PINS_LASER_FOUR);
Serial.print("LASER 5: "); Serial.println(PINS_LASER_FIVE);
Serial.print("LASER 6: "); Serial.println(PINS_LASER_SIX);
Serial.print("LASER 7: "); Serial.println(PINS_LASER_SEVEN);
Serial.print("RANGE 2 ECHO: "); Serial.println(PINS_RANGE_ECHO_TWO);
Serial.print("PHOTOCELL 1: "); Serial.println(PINS_PC_ONE);
Serial.print("PHOTOCELL 2: "); Serial.println(PINS_PC_TWO);
Serial.print("PHOTOCELL 3: "); Serial.println(PINS_PC_THREE);
Serial.print("PHOTOCELL 4: "); Serial.println(PINS_PC_FOUR);