-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathR2UnifiedLogics.ino
2114 lines (1992 loc) · 99.9 KB
/
R2UnifiedLogics.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
/**
* RSERIES UNIFIED LOGIC ENGINE 0.7
* Code originally by Joymonkey, updated by IOIIOOO
* With portions inspired by / lifted from Curiousmarc, Mowee, Flthymcnasty, and others!
*
* Works with both AVR and Teensy Boards
* AVR Boards don't support Mic effects due to a lack of analog input
*
* Now supports I2C Input!
*
* **********
*
* REQUIREMENTS
*
* Arduino IDE: https://www.arduino.cc/en/Main/Software
* Teensyduino (if using Teensy board): http://www.pjrc.com/teensy/td_download.html
* FastLED: https://github.com/FastLED/FastLED/releases
*
* **********
*
* COMMAND INPUT
* The RSeries use a command input string to tell it what effects combination should be run.
* This is comprised of a 7 digit long int.
*
* LEECSNN
*
* L - the logic designator - if not provided, defaults to 0 (all)
* *** NOT YET SUPPORTED ***
* 0 - All
* 1 - Front
* 2 - Rear
* EE - the effect - use two digits if logic designator provided
* 01 - Alarm - flips between color and red with mic effects
* 02 - Failure - cycles colors and brightness fading - roughly timed to 128 screa-3.mp3
* 03 - Leia - pale green with mic effects
* 04 - March - sequence timed to Imperial March
* 05 - Single Color - single hue shown
* 06 - Flashing Color - single hue on and off
* 07 - Flip Flop Color - boards flip back and forth - similar to march
* 08 - Flip Flop Alt - other direction of flips on back board, front is same to flip flop
* 09 - Color Swap - switches between color specified and inverse compliment color
* 10 - Rainbow - rotates through colors over time
* 11 - Red Alert - shows color specified based on mic input
* 12 - Mic Bright - brightness of color specified back on mic input
* 13 - Mic Rainbow - color goes from default specified through color range based on mic input
* 98 - Displays Off - turns off displays
* 00 - Reset to Normal
* C - color designator
* 1 - Red
* 2 - Orange
* 3 - Yellow
* 4 - Green
* 5 - Cyan (Aqua)
* 6 - Blue
* 7 - Purple
* 8 - Magenta
* 9 - Pink
* 0 - Default color on alarm / default to red on many effects / color cycle on march / ignored on failure and rainbow
* S - speed or sensitivity (1-9 scale) with 5 generally considered default for speed
* Flip Flop and Rainbow - 200ms x speed
* Flash - 250ms x speed
* March - 150ms x speed
* Color Swap - 350ms x speed
* Red Alert - sets mic sensitivity - as a fraction of speed / 10 - we recommend 3
* Mic Bright - sets minimum brightness - fraction of speed / 10
* NN - 2 digit time length in seconds
* 00 for continuous use on most
* 00 for default length on Leia
* Not used on March or Failure
*
* Some sequence examples:
* Note: Leading 0s drop off as these are long ints
* Solid Red: 51000
* Solid Orange: 52000
* Solid Yellow: 53000
* Solid Green: 54000
* Solid Cyan: 55000
* Solid Blue: 56000
* Solid Purple: 57000
* Solid Magenta: 58000
* Solid Pink: 59000
* Alarm (default): 10500
* Failure: 20000
* Leia: 30000
* March: 40500
* March (Red Only): 41500
* Flash (Yellow): 63500
* Color Swap (pink): 99500
* Rainbow: 100500
* Red Alert: 111300
* Mic Bright (Green): 124200
* Mic Rainbow (Cyan): 135000
*
* 54008 - solid green for 8 seconds
* 63315 - flashing yellow at slightly higher speed for 15 seconds
* 30008 - leia effect for only 8 seconds
*
* **********
*
* AVR BOARDS
*
* Use jumper on S4 to indicate if rear (jumper on) or front (jumper off) logic display
*
* **********
*
* USING TRIMPOTS FOR ADJUSTMENTS
*
* Teensy: jumper on Front or Rear pins
* AVR: jumper on S3
* Boot with jumper in place to reset EEPROM to defaults and then go into adjustment mode. Adjust trimpots.
* Place jumper on after boot to skip EEPROM reset and just go into adjustment mode directly.
* Remove jumper with power on to save adjustments made so they will stay in effect after power off.
*
* **********
*
* MARCDUINOS / JAWALITE / TEECES
*
* Input from Marcduinos work with either I2C (if using the Marcduino v2 firmware) or JAWALITE (default serial commands)
*
* Marcduinos use a sequence of &<i2caddress>,"<i2ccommand>\r for I2C commands
* So for example, to send to the default Teensy a command for static green, you would use:
* &10,"54000\r
* If sending more than one command in a sequence, put a few \p in between for pauses
* &10,"54000\r\p\p\p&11"54000\r
* The above would send the static green to both front and read AVR boards on the default I2C assignment
*
* To pass commands via Jawalite (default Marcduino commands) connect the serial out on the Marcduino (Teeces out)
* to the UART Rx input on the Teensy board.
*
* NOTE: Much of the Teeces communication code came directly from CuriousMarc's Teeces sketch at http://www.curiousmarc.com/teeces-logics-dome-lights
* It's highly likely there are some functions in here not currently used or available.
*
* Text input for example is NOT currently supported.
*
* **********
*
* SOME OTHER USEFUL LINKS
*
* JawaLite Command Information: https://github.com/joymonkey/logicengine/wiki/JawaLite-Commands
*
* The Logic of Logic Displays: https://github.com/joymonkey/logicengine/wiki/The-Logic-of-Logic-Displays
*
* Developer Notes: https://github.com/joymonkey/logicengine/wiki/Developer-Notes
*
* Calculate HSV Color Values: http://rseries.net/logic2/hsv/
*
* Explanation of how "Tween" colors are implimented: http://rseries.net/logic2/color/?keys=4&tweens=4
*
**/
#define PCBVERSION 0 //what kind of LED PCBs are they? 0 = Originals (with Naboo logo on backs of Front and Rear Logic)
// 1 = 2014 Version (with Kenny & McQuarry art on Rear, C3PO on Fronts)
// 2 = 2016 Version (with Deathstar plans on back of Rear Logic)
#define MAX_BRIGHTNESS 225 //can go up to 255, but why? this limit keeps current and heat down, and not noticeably dimmer than 255
#define MIN_BRIGHTNESS 1 //minimum brightness for standard logic patterns that adjustment pots can go down to
#define DEBUG 0 //debug mode slows down startup and prints some extra info to serial, only really helpful for development
//be warned - likely won't work on AVR due to memory limitations
#if defined(__MK20DX128__) || defined(__MK20DX256__) || defined(__MK64FX512__) || defined(__MK66FX1M0__)
#define BOARDTYPE 1 // Teensy
#else
#define BOARDTYPE 0 // AVR
#endif
#include <FastLED.h>
#include <Wire.h>
#include <avr/pgmspace.h> //to save SRAM, some constants get stored in flash memory
#include "EEPROM.h" //used to store user settings in EEPROM (settings will persist on reboots)
//a struct that holds the current color number and pause value of each LED (when pause value hits 0, the color number gets changed)
//to save SRAM, we don't store the "direction" anymore, instead we pretend that instead of having 16 colors, we've got 31 (16 that cross-fade up, and 15 "bizarro" colors that cross-fade back in reverse)
struct LEDstat { byte colorNum; byte colorPause; };
//adjustable settings
struct userSettings { byte maxBri; byte frontFade; byte frontDelay; byte frontHue; byte rearFade; byte rearDelay; byte rearHue; byte frontPalNum; byte rearPalNum; byte frontBri; byte rearBri; };
//default settings (will be overwritten from stored EEPROM data during setup(), or stored in EEPROM if current EEPROM values look invalid)
//to-do: add a version number
#define DFLT_FRONT_FADE 1
#define DFLT_FRONT_DELAY 40
#define DFLT_FRONT_HUE 0
#define DFLT_REAR_FADE 3
#define DFLT_REAR_DELAY 200
#define DFLT_REAR_HUE 0
#define DFLT_FRONT_PAL 0
#define DFLT_REAR_PAL 1
#define DFLT_FRONT_BRI 200
#define DFLT_REAR_BRI 200
userSettings settings[]={ MAX_BRIGHTNESS, DFLT_FRONT_FADE,DFLT_FRONT_DELAY,DFLT_FRONT_HUE,
DFLT_REAR_FADE, DFLT_REAR_DELAY, DFLT_REAR_HUE,
DFLT_FRONT_PAL, DFLT_REAR_PAL, DFLT_FRONT_BRI, DFLT_REAR_BRI };
#if (PCBVERSION==0)
#define FRONT_LED_COUNT 96
#else
#define FRONT_LED_COUNT 80
#endif
byte adjMode=0; // 0 for no adjustments, 1 for front, 3 for rear. if adjMode>0, then trimpots will be enabled
byte prevAdjMode=0;
byte prevBrightness=0;
byte prevPalNum=0;
//microphone stuff...
#define MIC_PERIOD 100 //the sample window for saving mic data (milliseconds)
unsigned int micVal;
unsigned int micStartMillis;
byte peakVal = 0; //microphone highest level for the current period
byte peakToPeak = 0; //microphone amplitude for the current period (difference between higest and lowest, a good way to detect )
unsigned int signalMax = 0;
unsigned int signalMin = 1024;
//some serial related stuff...
#if (BOARDTYPE==1)
#define JEDI_SERIAL Serial3 //use Teensy's third hardware serial port to listen for Jawalite commands
#else
#define JEDI_SERIAL Serial // use standard?
#endif
#define DEBUG_SERIAL Serial
#define CMD_MAX_LENGTH 64 // maximum number of characters in a command (63 chars since we need the null termination)
#define MAXSTRINGSIZE 64 // maximum number of letters in a logic display message
char cmdString[CMD_MAX_LENGTH];
char logicText[3][MAXSTRINGSIZE+1]; // memory for the text strings to be displayed, add one for the NULL character at the end
char ch;
byte command_available;
// alphabet 0= english, 1= aurabesh
#define LATIN 0
#define AURABESH 1
byte alphabetType[3];
// display mode
#define NORM 0
#define ALARM 1
#define MARCH 2
#define LEIA 3
#define FAILURE 4
#define SINGLE 5
#define FLASH 6
#define FLIPFLOP 7
#define FLIPFLOPALT 8
#define COLORSWAP 9
#define RAINBOW 10
#define REDALERT 11
#define MICBRIGHT 12
#define MICRAINBOW 13
#define LIGHTSOUT 98
byte displayEffect; // 0 = normal, 1 = alarm, 2 = march, 3 = leia, 4 = failure
byte previousEffect;
// display state for the logics, 0=normal random, 1=text display, 2=bargraph, 3=test, 4=off
#define RANDOM 0
#define TEXT 1
#define BARGRAPH 2
#define TEST 3
#define OFF 4
byte displayState[3];
// display state for the PSI, 0=normal random, 1=color1, 2=color2, 3=test, 4=off (0, 3 and 4 are shared with displays above)
byte psiState[2];
#define COLOR1 1
#define COLOR2 2
#define BLUE COLOR1
#define RED COLOR2
#define YELLOW COLOR1
#define GREEN COLOR2
#define LOGICRandomStyle 4
byte randomStyle[3]={LOGICRandomStyle, LOGICRandomStyle, LOGICRandomStyle}; // start with the default display random mode. This can be altered via extended JawaLite commands.
byte effectRunning; // tracks if we are in the midst of running an effect
//some default effect parameters
#define LEIAduration 34000
#define ALARMduration 4000
#define MARCHduration 48300
unsigned int marchSegment[8]={ 0,9800,14500,19300,28800,38300,45300,48300 };
#define FAILUREduration 10000
long displayEffectVal = 0; // if you want to boot with a different effect, set it here
#define NORMVAL 0 // set the default effect string for "normal" running
boolean logicInput = 0;
//some LED and pattern related stuff...
#if (BOARDTYPE==0)
// Pins specific to AVR
#define delayPin A0 //analog pin to read keyPause value
#define fadePin A1 //analog pin to read tweenPause value
#define briPin A2 //analog pin to read Brightness value
#define huePin A3 //analog pin to read Color/Hue shift value
#define FRONT_PIN 6
#define REAR_PIN 6
#define TOGGLEPIN 4 // pin to detect which logic is front vs back on AVR boards
#define MIC_PIN 23 //pin used to for microphone preamp
#define FJUMP_PIN 3 //front jumper
#define RJUMP_PIN 3 //rear jumper
#define STATUSLED_PIN 13 //status LED is connected to pin 10, incorrectly labelled 9 on the PCB!!
#else
// Pins Specific to Teensy
#define delayPin A1 //15analog pin to read keyPause value
#define fadePin A2 //16analog pin to read tweenPause value
#define briPin A3 //17analog pin to read Brightness value
#define huePin A6 //20analog pin to read Color/Hue shift value
#define FRONT_PIN 21
#define REAR_PIN 22
#define MIC_PIN 23 //pin used to for microphone preamp
#define FJUMP_PIN 0 //front jumper
#define RJUMP_PIN 1 //rear jumper
#define STATUSLED_PIN 10 //status LED is connected to pin 10, incorrectly labelled 9 on the PCB!!
#define TOGGLEPIN 0 // not used
#endif
#define TWEENS 14 //lower=faster higher=smoother color crossfades, closely related to the Fade setting
#define PAL_PIN 2 //pin used to switch palettes in ADJ mode
#define REAR_I2C 10 // A in hex - I2C Address for Rear or BOTH (if Teensy)
#define FRONT_I2C 11 // B in hex - I2C Address for Front
byte I2CAddress;
#define I2CMAXCOMMANDLENGTH 8
char I2CInput[I2CMAXCOMMANDLENGTH] = {}; // a char array to hold incoming data
// String I2CInput = ""; // a string to hold incoming data
volatile boolean I2CComplete = false; // whether the serial string is complete
CRGB frontLEDs[FRONT_LED_COUNT];
CRGB rearLEDs[96];
CRGB statusLED[1];
#define TOTALCOLORS (4+(TWEENS*(3)))
#define TOTALCOLORSWBIZ ((TOTALCOLORS*2)-2)
byte allColors[2][TOTALCOLORS][3]; //the allColor array will comprise of two color palettes on a Teensy
LEDstat ledStatus[FRONT_LED_COUNT + 96]; //status array will cover both front and rear logics on a Teensy
#if (PCBVERSION<2)
#define LED_TYPE WS2812
#else
#define LED_TYPE SK6812
#endif
#define JEDI_BAUDRATE 2400
//////////////////////////////////////////////////////////////////////////////////////////////////////
// FRONT LED MAPPING...
#if (PCBVERSION==0)
//mapping for FLD boards from first run (with 48 LEDs per PCB)
const byte frontLEDmap[]PROGMEM = {
0, 1, 2, 3, 4, 5, 6, 7,
15,14,13,12,11,10, 9, 8,
16,17,18,19,20,21,22,23,
31,30,29,28,27,26,25,24,
32,33,34,35,36,37,38,39,
47,46,45,44,43,42,41,40, //
88,89,90,91,92,93,94,95, //
87,86,85,84,83,82,81,80,
72,73,74,75,76,77,78,79,
71,70,69,68,67,66,65,64,
56,57,58,59,60,61,62,63,
55,54,53,52,51,50,49,48};
#else
//mapping for newer FLD PCBs (40 LEDs per PCB, lower FLD upside-down)...
const byte frontLEDmap[]PROGMEM = {
0, 1, 2, 3, 4, 5, 6, 7,
15,14,13,12,11,10, 9, 8,
16,17,18,19,20,21,22,23,
31,30,29,28,27,26,25,24,
32,33,34,35,36,37,38,39,
79,78,77,76,75,74,73,72,
64,65,66,67,68,69,70,71,
63,62,61,60,59,58,57,56,
48,49,50,51,52,53,54,55,
47,46,45,44,43,42,41,40};
#endif
//REAR LED MAPPING...
#if (PCBVERSION==0)
//mapping for first RLD (two PCBs soldered together)
const byte rearLEDmap[]PROGMEM = {
0, 1, 2, 3, 4, 5, 6, 7, 48,49,50,51,52,53,54,55,
15,14,13,12,11,10, 9, 8, 63,62,61,60,59,58,57,56,
16,17,18,19,20,21,22,23, 64,65,66,67,68,69,70,71,
31,30,29,28,27,26,25,24, 79,78,77,76,75,74,73,72,
32,33,34,35,36,37,38,39, 80,81,82,83,84,85,86,87,
47,46,45,44,43,42,41,40, 95,94,93,92,91,90,89,88 };
#elif (PCBVERSION==1)
//mapping for single RLD PCB (second parts run on)...
const byte rearLEDmap[]PROGMEM = {
0, 1, 2,28, 3,27, 4,26, 5,25, 6, 7, 8, 9,22,10,21,11,20,12,19,13,14,15,
31,30,29,32,33,34,35,36,37,38,39,24,23,40,41,42,43,44,45,46,47,18,17,16,
64,65,66,63,62,61,60,59,58,57,56,71,72,55,54,53,52,51,50,49,48,77,78,79,
95,94,93,67,92,68,91,69,90,70,89,88,87,86,73,85,74,84,75,83,76,82,81,80 };
#else
//mapping for 2016 RLD (Death Star on back, direct mapping)...
const byte rearLEDmap[]PROGMEM = {
0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,
47,46,45,44,43,42,41,40,39,38,37,36,35,34,33,32,31,30,29,28,27,26,25,24,
48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,
95,94,93,92,91,90,89,88,87,86,85,84,83,82,81,80,79,78,77,76,75,74,73,72 };
#endif
//////////////////////////////////////////////////////////////////////////////////////////////////////
//we define both all palettes, in case the user wants to try out rear colors on the front etc
// note that these are not RGB colors, they're HSV
// for help calculating HSV color values see http://joymonkey.com/logic/
#define PAL_COUNT 4
const byte keyColors[PAL_COUNT][4][3]PROGMEM = { { {170,255,0} , {170,255,136} , {170,255,255} , {170,0,255} } , //front colors
{ {87,206,0} , {87,206,105} , {45,255,184} , {0,255,250} } , //rear colors (hues: 87=bright green, 79=yellow green, 45=orangey yellow, 0=red)
{ {0,255,0} , {0,255,85} , {0,255,170} , {0,255,255} } , //monotone (black to solid red)
{ {255,0,255} , {0,255,255} , {85,255,255} , {170,255,255}} }; //rainbow (white, red, green, blue) THIS PALETTE IS HORRIBLE, CHANGE IT TO SOMETHING MORE USEFUL
//some variables used for status display and timekeeping...
unsigned long currentMillis = millis();
unsigned long effectStartMillis = millis();
unsigned int effectMillis;
unsigned long statusMillis = millis(); //last time status LED was changed, or effect sequence was started
unsigned long adjMillis = millis(); //last time we entered an adj mode
unsigned long statusDelay=1500;
bool flipflop;
bool flipfloplast;
//////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////
//function to calculate all colors based on the chosen colorPalNum
//this will get called during setup(), and also if we've remotely commanded to change color palettes
//NEW: added a "bright" parameter, to allow front and rear brightness to be adjusted independantly
void calculateAllColors(byte colorPalNum, bool frontRear=0, byte brightVal=MAX_BRIGHTNESS) {
//take a set of 4 key colors from keyColors[3][4][3] and generate 16 colors in allColors[frontRear]
// 328P will only have one set of full colors, Teensy will have two
for(byte kcol=0;kcol<(4);kcol++) { //go through each Key color
byte workColor[3]={ pgm_read_byte(&keyColors[colorPalNum][kcol][0]) , pgm_read_byte(&keyColors[colorPalNum][kcol][1]) , pgm_read_byte(&keyColors[colorPalNum][kcol][2]) };
allColors[frontRear][kcol+(TWEENS*kcol)][0]=workColor[0];
allColors[frontRear][kcol+(TWEENS*kcol)][1]=workColor[1];
#if (DEBUG==1)
//DEBUG_SERIAL.println( "Key color "+String(kcol)+" val is "+String( map8(workColor[2], 0, brightVal)+ "( was "+String( workColor[2] )+", max would be "+String(brightVal) ) );
#endif
allColors[frontRear][kcol+(TWEENS*kcol)][2]=map8(workColor[2], MIN_BRIGHTNESS, brightVal); //Value (V) is adjusted down to whatever brightness setting we've specified
if (kcol+(TWEENS*kcol)+1!=TOTALCOLORS) {
for(byte el=0;el<3;el++) { //loop through H, S and V from this key to the next
int perStep=int(pgm_read_byte(&keyColors[colorPalNum][kcol+1][el])-workColor[el])/(TWEENS+1);
if (perStep!=0) {
for (byte tweenCount=1; tweenCount<=TWEENS; tweenCount++) {
byte val= pgm_read_byte(&keyColors[colorPalNum][kcol][el]) + (tweenCount*perStep);
if (el==2) allColors[frontRear][kcol+(TWEENS*kcol)+tweenCount][el]=map8(val, MIN_BRIGHTNESS, brightVal);
else allColors[frontRear][kcol+(TWEENS*kcol)+tweenCount][el]=val;
}
}
else {
//tweens for this element (h,s or v) don't change between this key and the next, fill em up
for (byte tweenCount=1; tweenCount<=TWEENS; tweenCount++) {
/*if (el==2) {
//allColors[frontRear][kcol+(TWEENS*kcol)+tweenCount][el]=map(workColor[el], 0, 255, 0, bright);
allColors[frontRear][kcol+(TWEENS*kcol)+tweenCount][el]=map(workColor[el], 0, 255, 0, bright); //Value (V) is adjusted down to whatever brightness setting we've specified
}
else allColors[frontRear][kcol+(TWEENS*kcol)+tweenCount][el]=workColor[el];*/
if (el==2) allColors[frontRear][kcol+(TWEENS*kcol)+tweenCount][el]=map8(workColor[el], MIN_BRIGHTNESS, brightVal); //map V depending on brightness setting
else allColors[frontRear][kcol+(TWEENS*kcol)+tweenCount][el]=workColor[el];
}
}
}
}
}
#if (DEBUG==1)
/*DEBUG_SERIAL.println("Logic "+String(frontRear));
for(byte colorNum=0;colorNum<TOTALCOLORS;colorNum++) {
DEBUG_SERIAL.println( String( allColors[frontRear][colorNum][0] )+","+String( allColors[frontRear][colorNum][1] )+","+String( allColors[frontRear][colorNum][2] ) );
}*/
#endif
}
//function takes a color number (that may be bizarro) and returns an actual color number
int actualColorNum(int x) {
if (x>=TOTALCOLORS) x=(TOTALCOLORS-2)-(x-TOTALCOLORS);
return(x);
}
void updateLED(byte LEDnum , byte hueVal, bool frontRear, byte briVal=255) {
//frontRear is 0 for front, 1 for rear
//briVal is optional, typically used to change individual LED brightness during effect sequences
//remember that ledStatus[] has 176/192 LEDs to track, frontLEDs is an 80/96 LED object, rearLEDs is a 96 LED object
//current front pallete is settings[0].frontPalNum
//current rear pallete is settings[0].rearPalNum
if (frontRear==0) {
if (ledStatus[LEDnum].colorPause!=0) ledStatus[LEDnum].colorPause--; //reduce the LEDs pause number and check back next loop
else {
ledStatus[LEDnum].colorNum++;
if (ledStatus[LEDnum].colorNum>=TOTALCOLORSWBIZ) ledStatus[LEDnum].colorNum=0; //bring it back to color zero
byte realColor=actualColorNum(ledStatus[LEDnum].colorNum);
if (ledStatus[LEDnum].colorNum%(5)==0) ledStatus[LEDnum].colorPause=random8(settings[0].frontDelay); //color is a key, assign random pause
else ledStatus[LEDnum].colorPause=settings[0].frontFade; //color is a tween, assign a quick pause
//
if (briVal==255) frontLEDs[LEDnum].setHSV( (allColors[0][realColor][0]+hueVal), allColors[0][realColor][1], allColors[0][realColor][2] );
else frontLEDs[LEDnum].setHSV( (allColors[0][realColor][0]+hueVal), allColors[0][realColor][1], map8( briVal, 0, allColors[0][realColor][2] ) );
}
}
if (frontRear==1) {
LEDnum=LEDnum+FRONT_LED_COUNT; //because ledStatus also holds status of the fronts (0-79 or 0-95), we need to start at 80/96
if (ledStatus[LEDnum].colorPause!=0) ledStatus[LEDnum].colorPause--; //reduce the LEDs pause number and check back next loop
else {
ledStatus[LEDnum].colorNum++;
if (ledStatus[LEDnum].colorNum>=TOTALCOLORSWBIZ) ledStatus[LEDnum].colorNum=0; //bring it back to color zero
byte realColor=actualColorNum(ledStatus[LEDnum].colorNum);
if (ledStatus[LEDnum].colorNum%(5)==0) ledStatus[LEDnum].colorPause=random8(settings[0].rearDelay); //color is a key, assign random pause
else ledStatus[LEDnum].colorPause=settings[0].rearFade; //color is a tween, assign a quick pause
//
if (briVal==255) rearLEDs[LEDnum-FRONT_LED_COUNT].setHSV( (allColors[1][realColor][0]+hueVal), allColors[1][realColor][1], allColors[1][realColor][2] );
else rearLEDs[LEDnum-FRONT_LED_COUNT].setHSV( (allColors[1][realColor][0]+hueVal), allColors[1][realColor][1], map8( briVal, 0, allColors[1][realColor][2] ) );
}
}
#if (DEBUG==1)
/*if (LEDnum==0) {
DEBUG_SERIAL.print(String(LEDnum)+": "+String(frontLEDs[0][0])+","+String(frontLEDs[0][1])+","+String(frontLEDs[0][2])+".");
}*/
#endif
}
//microphone preamp function
void microphoneRead(unsigned int micPeriod=100) {
micVal=analogRead(MIC_PIN);
if (currentMillis-micStartMillis<micPeriod) {
if (micVal > signalMax) signalMax = micVal; //things were louder than before, so save this value as the highest so far
if (micVal < signalMin) signalMin = micVal; //things were quieter than before, so save this value as the lowest so far
}
else {
//we've waited long enough, save the highest micVal for the last period as peakVal and biggest difference to peakToPeak
peakVal = map(signalMax, 525, 1024, 0, 255);
peakToPeak = map((signalMax-signalMin), 0, 1024, 0, 255);
//reset timer and signal measures...
micStartMillis=currentMillis;
signalMin = 1024; signalMax = 0;
}
}
//quick funciton to write all the current++++ settings to EEPROM in order
void writeSettingsToEEPROM() {
EEPROM.write(0, settings[0].maxBri);
EEPROM.write(1, settings[0].frontFade);
EEPROM.write(2, settings[0].frontDelay);
EEPROM.write(3, settings[0].frontHue);
EEPROM.write(4, settings[0].rearFade);
EEPROM.write(5, settings[0].rearDelay);
EEPROM.write(6, settings[0].rearHue);
EEPROM.write(7, settings[0].frontPalNum);
EEPROM.write(8, settings[0].rearPalNum);
EEPROM.write(9, settings[0].frontBri);
EEPROM.write(10, settings[0].rearBri);
}
void readSettingsFromEEPROM() {
settings[0]={EEPROM.read(0), EEPROM.read(1),EEPROM.read(2), EEPROM.read(3), EEPROM.read(4), EEPROM.read(5), EEPROM.read(6), EEPROM.read(7), EEPROM.read(8), EEPROM.read(9), EEPROM.read(10)};
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void setup() {
delay(50);
#if (DEBUG==1)
delay(5000); //long delay to give you time to open a serial monitor
#endif
// start listening for serial
JEDI_SERIAL.begin(JEDI_BAUDRATE); //start whichever serial device we're using for Jawalite commands on this board
DEBUG_SERIAL.begin(9600);
if (BOARDTYPE == 0) {
#if (DEBUG==1)
DEBUG_SERIAL.println("AVR Board");
#endif
} else {
#if (DEBUG==1)
DEBUG_SERIAL.println("Teensy Reactor");
#endif
}
// Determine which board is running and act accordingly
if (BOARDTYPE == 0) {
pinMode(TOGGLEPIN, INPUT);
logicInput = digitalRead(TOGGLEPIN);
if (logicInput == 1) {
I2CAddress = REAR_I2C;
#if (DEBUG==1)
DEBUG_SERIAL.println("RLD");
#endif
} else {
I2CAddress = FRONT_I2C;
#if (DEBUG==1)
DEBUG_SERIAL.println("FLD");
#endif
}
} else {
I2CAddress = REAR_I2C;
logicInput = 0;
#if (DEBUG==1)
DEBUG_SERIAL.println("Teensy");
#endif
}
// start listening for I2C
Wire.begin(I2CAddress); // Start I2C Bus as Master I2C Address
#if (DEBUG==1)
DEBUG_SERIAL.print("I2C Start: ");
DEBUG_SERIAL.println(I2CAddress);
#endif
Wire.onReceive(i2cEvent); // register event so when we receive something we jump to receiveEvent();
if (BOARDTYPE == 1 || logicInput == 1) {
FastLED.addLeds<LED_TYPE, REAR_PIN, GRB>(rearLEDs, 96);
fill_solid( rearLEDs, 96, CRGB(0,0,0));
}
if (BOARDTYPE == 1 || logicInput == 0) {
FastLED.addLeds<LED_TYPE, FRONT_PIN, GRB>(frontLEDs, FRONT_LED_COUNT);
fill_solid( frontLEDs, FRONT_LED_COUNT, CRGB(0,0,0));
}
// turn on status LED for Teensy
if (BOARDTYPE == 1) {
FastLED.addLeds<SK6812, STATUSLED_PIN, GRB>(statusLED, 1);
statusLED[0] = 0x220000; FastLED.show(); //status LED dark red
pinMode(13, OUTPUT); digitalWrite(13, HIGH); //turn on Teensy LED
delay(100);
}
//jumper is used to set front or rear adjustment mode (pull low to adjust)
pinMode(FJUMP_PIN,INPUT_PULLUP); //use internal pullup resistors of Teensy
pinMode(RJUMP_PIN,INPUT_PULLUP);
pinMode(PAL_PIN,INPUT_PULLUP);
//if eeprom settings look invalid (frontFade>100), or front jumper is pulled low on Teensy or S3 in place on AVR, reset eeprom values to defaults
if ((EEPROM.read(1)>100)||(BOARDTYPE == 1 && digitalRead(FJUMP_PIN)==0)||(BOARDTYPE == 0 && digitalRead(FJUMP_PIN)==HIGH)) {
if (EEPROM.read(1)>100) {
#if (DEBUG==1)
DEBUG_SERIAL.println("Bad eeprom value. 001="+String(EEPROM.read(1)));
#endif
} else {
#if (DEBUG==1)
DEBUG_SERIAL.println("Front Jumper pin is low");
#endif
}
#if (DEBUG==1)
DEBUG_SERIAL.println("Writing EEPROM defaults");
#endif
if (BOARDTYPE == 1) {
for (byte i=0; i<5; i++){ statusLED[0] = 0x222200; FastLED.delay(200); statusLED[0] = 0x000000; FastLED.delay(200);} //blinky
} else if (BOARDTYPE == 0) {
for (byte i=0; i<5; i++){ digitalWrite(STATUSLED_PIN, HIGH); FastLED.delay(200); digitalWrite(STATUSLED_PIN, LOW); FastLED.delay(200);} //blinky
}
writeSettingsToEEPROM();
if (BOARDTYPE == 1) {
statusLED[0] = 0x110022; FastLED.show(); // Teensy LED
} else if (BOARDTYPE == 0) { // AVR flutter the status LED to show we are done
digitalWrite(STATUSLED_PIN, HIGH);
delay(20);
digitalWrite(STATUSLED_PIN, LOW);
delay(20);
digitalWrite(STATUSLED_PIN, HIGH);
delay(20);
digitalWrite(STATUSLED_PIN, LOW);
delay(20);
digitalWrite(STATUSLED_PIN, HIGH);
delay(20);
digitalWrite(STATUSLED_PIN, LOW);
delay(20);
digitalWrite(STATUSLED_PIN, HIGH);
delay(20);
digitalWrite(STATUSLED_PIN, LOW);
delay(20);
digitalWrite(STATUSLED_PIN, HIGH);
delay(20);
digitalWrite(STATUSLED_PIN, LOW);
delay(20);
digitalWrite(STATUSLED_PIN, HIGH);
delay(20);
digitalWrite(STATUSLED_PIN, LOW);
delay(20);
}
delay(1000); //
}
//read settings from eeprom...
readSettingsFromEEPROM();
//settings[0]={EEPROM.read(0), EEPROM.read(1),EEPROM.read(2), EEPROM.read(3), EEPROM.read(4), EEPROM.read(5), EEPROM.read(6), EEPROM.read(7), EEPROM.read(8), EEPROM.read(9), EEPROM.read(10)};
#if (DEBUG==1)
DEBUG_SERIAL.println("bri,frontFade,frontDelay,frontHue,rearFade,rearDelay,rearHue,frontPalNum,rearPalNum,frontBrightness,rearBrightness");
DEBUG_SERIAL.print(F("eeprom: "));
for (byte i=0; i<11; i++) { DEBUG_SERIAL.print(String(EEPROM.read(i))); if (i<10) DEBUG_SERIAL.print(F(",")); }
DEBUG_SERIAL.println(".");
#endif
#if (DEBUG==1)
DEBUG_SERIAL.println("Calculating all colors using max brightness value of "+String(settings[0].frontBri));
#endif
calculateAllColors(settings[0].frontPalNum,0,settings[0].frontBri);
for( byte i = 0; i < FRONT_LED_COUNT; i++) {
ledStatus[i]={random8(TOTALCOLORSWBIZ),random8()};
updateLED(pgm_read_byte(&frontLEDmap[i]),settings[0].frontHue,0);
#if (DEBUG==1)
FastLED.delay(10);
DEBUG_SERIAL.print(String(ledStatus[i].colorNum)+" ");
if (((i+1)%8)==0) DEBUG_SERIAL.println();
if (((i+1)%40)==0) DEBUG_SERIAL.println();
#endif
}
#if (DEBUG==1)
FastLED.delay(250);
#endif
calculateAllColors(settings[0].rearPalNum,1,settings[0].rearBri);
for( byte i = 0; i < 96; i++) {
ledStatus[i+FRONT_LED_COUNT]={random8(TOTALCOLORSWBIZ),random8()}; //assign the LED a random color number
updateLED(pgm_read_byte(&rearLEDmap[i]),settings[0].rearHue,1); //update that LED to actually set its color
#if (DEBUG==1)
FastLED.delay(10);
DEBUG_SERIAL.print(String(ledStatus[i+FRONT_LED_COUNT].colorNum)+" ");
if (((i+1)%24)==0) DEBUG_SERIAL.println();
#endif
}
if (BOARDTYPE == 1) {
statusLED[0] = 0x002200; FastLED.show(); //status LED dark green
//delay(50);
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////
// Command language - JawaLite emulation
///////////////////////////////////////////////////////
////////////////////////////////
// command line builder, makes a valid command line from the input
byte buildCommand(char ch, char* output_str) {
static uint8_t pos=0;
switch(ch) {
case '\r': // end character recognized
output_str[pos]='\0'; // append the end of string character
pos=0; // reset buffer pointer
return true; // return and signal command ready
break;
default: // regular character
output_str[pos]=ch; // append the character to the command string
if(pos<=CMD_MAX_LENGTH-1)pos++; // too many characters, discard them.
break;
}
// DEBUG_SERIAL.println(pos);
// DEBUG_SERIAL.println(ch);
return false;
}
///////////////////////////////////
// command parser and switcher,
// breaks command line in pieces,
// rejects invalid ones,
// switches to the right command
void parseCommand(char* inputStr) {
byte hasArgument=false;
long argument=0;
int address;
byte pos=0;
byte length=strlen(inputStr);
if(length<2) goto beep; // not enough characters
// If it starts with I, treat as an I2C command
if (inputStr[0]=='I') {
pos++;
if(!length>pos) hasArgument=false; // end of string reached, no arguments
else {
for(byte i=pos; i<length; i++) if(!isdigit(inputStr[i])) goto beep; // invalid, end of string contains non-numerial arguments
argument=atol(inputStr+pos); // that's the numerical argument after the command character
hasArgument=true;
}
if(!hasArgument) goto beep; // invalid, no argument after command
//DEBUG_SERIAL.print(F("Arg: "));
//DEBUG_SERIAL.println(argument);
doIcommand(argument);
return; // exit
}
// get the address, one or two digits
char addrStr[3];
if(!isdigit(inputStr[pos])) goto beep; // invalid, first char not a digit
addrStr[pos]=inputStr[pos];
pos++; // pos=1
if(isdigit(inputStr[pos])) { // add second digit address if it's there
addrStr[pos]=inputStr[pos];
pos++; // pos=2
}
addrStr[pos]='\0'; // add null terminator
address= atoi(addrStr); // extract the address
// check for more
if(!length>pos) goto beep; // invalid, no command after address
// echo the command out, so the other logic gets it
JEDI_SERIAL.println(inputStr);
// special case of M commands, which take a string argument
if(inputStr[pos]=='M') {
pos++;
if(!length>pos) goto beep; // no message argument
doMcommand(address, inputStr+pos); // pass rest of string as argument
return; // exit
}
// other commands, get the numerical argument after the command character
pos++; // need to increment in order to peek ahead of command char
if(!length>pos) hasArgument=false; // end of string reached, no arguments
else {
for(byte i=pos; i<length; i++) if(!isdigit(inputStr[i])) goto beep; // invalid, end of string contains non-numerial arguments
argument=atoi(inputStr+pos); // that's the numerical argument after the command character
hasArgument=true;
}
// switch on command character
switch(inputStr[pos-1]) { // 2nd or third char, should be the command char
case 'T':
if(!hasArgument) goto beep; // invalid, no argument after command
doTcommand(address, argument);
break;
case 'F': // adjust logic fade setting
if(!hasArgument) goto beep; // invalid, no argument after command
doFcommand(address, argument);
break;
case 'G': // adjust logic delay setting
if(!hasArgument) goto beep; // invalid, no argument after command
doGcommand(address, argument);
break;
case 'H': // adjust logic hue setting
if(!hasArgument) goto beep; // invalid, no argument after command
doHcommand(address, argument);
break;
case 'J': // adjust logic brightness setting
if(!hasArgument) goto beep; // invalid, no argument after command
doJcommand(address, argument);
break;
case 'D': // holos,D command is weird, does not need an argument, ignore if it has one
//doDcommand(address);
break;
case 'P': // custom parameter (alphabet switching, palette cycling, settings saving etc)
if(!hasArgument) goto beep; // invalid, no argument after command
doPcommand(address, argument);
break;
case 'R': // random styles for Logics (extra CuriousMarc command)
if(!hasArgument) goto beep; // invalid, no argument after command
//doRcommand(address, argument);
break;
case 'S': // PSI State (extra CuriousMarc command)
if(!hasArgument) goto beep; // invalid, no argument after command
//doScommand(address, argument);
break;
default:
goto beep; // unknown command
break;
}
return; // normal exit
beep: // error exit
JEDI_SERIAL.write(0x7); // beep the terminal, if connected
return;
}
//////////////////////////////////////////////////////////////////////////////////////////////
// Special Effect Routines
/////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////
// Reset Utilities
// resets text scrolling except alphabet
void resetText(byte display) {
// reset text
//scrollPositions[display]= (display==2? 27 : 9);
//textScrollCount[display]=0;
}
// same for all the displays
void resetAllText() {
//for(byte disp=0; disp<3; disp++) resetText(disp);
}
// forces exit from effects immediately
void exitEffects() {
displayEffect=NORM;
effectRunning=0;
}
// exit effects, reset scrolling, alphabet back to latin, mode to random
void resetDisplays() {
resetAllText();
exitEffects();
for(byte disp=0; disp<3; disp++) {
alphabetType[disp]=LATIN;
displayState[disp]=NORM;
}
}
//////////////////////////////////////
// Text Display Routines
/////////////////////////////////////
//////////////////////
// Set String
void setText(byte disp, const char* message)
{
strncpy(logicText[disp], message, MAXSTRINGSIZE);
logicText[disp][MAXSTRINGSIZE]=0; // just in case
}
//////////////////////
// command executers
// set text command
void doMcommand(int address, char* message){
DEBUG_SERIAL.print(F("\nCmd: M Addr: "));
DEBUG_SERIAL.print(address);
DEBUG_SERIAL.print(F(" Arg: "));
DEBUG_SERIAL.print(message);
/*if(address==0) {setText(0, message); setText(1, message); setText(2, message); resetAllText();}
if(address==1) {setText(0, message); resetText(0);}
if(address==2) {setText(1, message); resetText(1);}
if(address==3) {setText(2, message); resetText(2);} */
}
/////////////////////////////////////////////////////////
///***** I2C Event Function *****///
/////////////////////////////////////////////////////////
/// This routine is run when an onRecieve event is ///
/// triggered. Multiple bytes of data may be ///
/// available. ///
/////////////////////////////////////////////////////////
void i2cEvent(int howMany)
{
#if(DEBUG==1)
DEBUG_SERIAL.println("I2C Event Received.");
#endif
I2CInput[I2CMAXCOMMANDLENGTH] = {}; // Ensures I2CInput is empty
int i=0;
while(Wire.available()) { // loop through all incoming bytes
char inChar = (char)Wire.read(); // Receive each byte as a character
//DEBUG_SERIAL.print("Char: ");
//DEBUG_SERIAL.println(inChar);
if(i<I2CMAXCOMMANDLENGTH) {
//I2CInput += inChar; // Add each Character to I2CInput
if (isDigit(inChar)) {
I2CInput[i] = inChar;
//DEBUG_SERIAL.println(I2CInput[i]);
} else {
I2CInput[i] = 0; // empty out the rest of the array
//DEBUG_SERIAL.println(I2CInput[i]);
}
}
i++;
}
for (i=i;i<I2CMAXCOMMANDLENGTH;i++)
I2CInput[i] = 0; // empty out the rest of the array
//DEBUG_SERIAL.println(I2CInput);
I2CComplete = true; // Once done, set a flag so the main loop can do something about it.
}
// I2C commands
void doIcommand(long commandInput) {
//DEBUG_SERIAL.print(F("I2C Command: "));
//DEBUG_SERIAL.println(commandInput);
updateEffect(commandInput);
}
// various commands for states and effects
void doTcommand(int address, int argument) {
JEDI_SERIAL.print(F("\nCmd: T Addr: "));
JEDI_SERIAL.print(address);
JEDI_SERIAL.print(F(" Arg: "));
JEDI_SERIAL.print(argument);
FastLED.show();
//***
switch(argument) {
case 0: // test mode
exitEffects();
if(address==0) {displayState[0]=displayState[1]=displayState[2]=psiState[0]=psiState[1]=TEST; resetAllText();}
/*if(address==1) {displayState[0]=TEST; resetText(0);}
if(address==2) {displayState[1]=TEST; resetText(1);}
if(address==3) {displayState[2]=TEST; resetText(2);}
if(address==4) {psiState[0]=TEST;}
if(address==5) {psiState[1]=TEST;}*/
break;
case 1: // normal random mode, cancel effects too
exitEffects();
if(address==0) {displayState[0]=displayState[1]=displayState[2]=psiState[0]=psiState[1]=RANDOM; resetAllText();}
updateEffect(0);// default sequence
/*if(address==1) {displayState[0]=RANDOM; resetText(0);}
if(address==2) {displayState[1]=RANDOM; resetText(1);}
if(address==3) {displayState[2]=RANDOM; resetText(2);}
if(address==4) {psiState[0]=RANDOM;}