-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlego-apollo-controller-mqtt.ino
1043 lines (892 loc) · 32.3 KB
/
lego-apollo-controller-mqtt.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
// This #include statement was automatically added by the Particle IDE.
#include <MQTT.h>
void callback(char* topic, byte* payload, unsigned int length);
byte server[] = { 192, 168, 2, 84 }; //IP Address of Home Assistant Server. 192.168.2.84
const char* DeviceID = System.deviceID();
const char* Username = "XXXXXXXXXXXXX"; //Home Assistant generates this when you setup MQTT
const char* Password = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"; //Home Assistant generates this when you setup MQTT
MQTT client(server,
1883 /* port */,
600 /* keep alive in seconds (10 mins) */,
callback,
512 /* max packet size */);
//int led2 = D7; // led on pin7
#define LIGHTS_ON 1
#define LIGHTS_OFF 2
#define SHOULD_TURN_ON 3
#define SHOULD_TURN_OFF 4
int SV_state = 0;
int LL_state = 0;
const char* on = "ON";
const char* off = "OFF";
const char* SV_command_topic = "home/particle/sv/control";
const char* LL_command_topic = "home/particle/ll/control";
const char* SV_state_topic = "home/particle/sv/status";
const char* LL_state_topic = "home/particle/ll/status";
const char* SV_discovery_payload = "{\"~\":\"home/particle/sv\",\"name\":\"SV_Light\",\"unique_id\":\"sv_light\",\"state_topic\":\"~/status\",\"command_topic\":\"~/control\",\"device\":{\"identifiers\":\"SV_Controller\",\"model\":\"Photon\",\"manufacturer\":\"Particle\"}}";
const char* LL_discovery_payload = "{\"~\":\"home/particle/ll\",\"name\":\"LL_Light\",\"unique_id\":\"ll_light\",\"state_topic\":\"~/status\",\"command_topic\":\"~/control\",\"device\":{\"identifiers\":\"LL_Controller\",\"model\":\"Photon\",\"manufacturer\":\"Particle\"}}";
//Code is mostly duplicated since it's controlling two Neopixels. _SV denotes Saturn V Neopixels and _LM denotes Lunar Module Neopixels.
//There are duplicates for the display; 7-segment has been commented out (and un-tested) and the Alphanumeric one has been enabled/tested.
#include <Adafruit_LEDBackpack_RK.h>
#include <neopixel.h>
#include <Particle.h>
#include <math.h>
#include "Particle.h"
#include "Adafruit_GFX_RK.h"
#include "Adafruit_LEDBackpack_RK.h"
//Adafruit_7segment matrix = Adafruit_7segment();
Adafruit_AlphaNum4 alpha4 = Adafruit_AlphaNum4();
//Saturn V pins/parameters
#define PIN_SV D2
#define PIXEL_COUNT_SV 60
#define arrayLength_SV PIXEL_COUNT_SV/3
#define SCALE_AMOUNT_SV 0.5
#define FLICKER_SPEED_SV 5
#define FLICKER_AMOUNT_SV 50
//Lunar Module pins/parameters
#define PIN_LM D3
#define PIXEL_COUNT_LM 6
#define arrayLength_LM PIXEL_COUNT_LM/3
#define SCALE_AMOUNT_LM 0.5
#define FLICKER_SPEED_LM 5
#define FLICKER_AMOUNT_LM 50
//Pins to control the lights
#define Fuel_Red A0
#define Fuel_Green A1
#define Fuel_Blue A2
#define Alarm_1201 A3
#define Alarm_1202 A4
//Pins to turn on the audio clips
#define SV_COUNTDOWN D4
#define SV_SHUTDOWN D5
#define LM_LANDING D6
String SV_Status = "Off";
String LM_Status = "Off";
//Values and flag variables for the Saturn V
int SV_Brightness = 0; //Value to set the brightness level of the SV lights
bool Launch_Flag = false; //Flag to ramp up the SV lights from off to full-on
bool Engine_Sequence_Flag = false; //Flag to ramp up the SV lights from off to low
bool Engine_Launch_Flag = false; //Flag to ramp up the SV lights from low to full-on
bool Audio_Launch_Flag = false; //Flag to start the SV launch audio sequence
int Month;
int Day;
int Hour;
int Minute;
int Second;
int Last_Second;
//Values and flag variables for the Lunar Module
int LM_Brightness = 0; //Value to set the brightness level of the LM lights
bool Landing_Flag = false; //Flag to ramp up the LM lights from off to full-on
bool PDI_Flag = false;
bool Throttle_Down_Flag = false; //Flag to ramp down the LM lights from full-on to medium
bool LM_Shutdown_Flag = false;
bool Audio_Landing_Flag = false; //Flag to start up the LM landing audio sequence
int Alarm_Number;
int Alarm_Loops = 0;
int Alarm_Counter = 0;
//Values and flag variables For the timer/stopwatch/clock display
int Clock_Type = 0; //0 is for a timer (count down), 1 is for a stopwatch (count up)
int Minute_1 = 0;
int Minute_2 = 0;
int Second_1 = 0;
int Second_2 = 0;
bool Clock_Display_Flag = false;
bool Time_Display_Flag = true; //Dictate whether you want the time to start displaying immediately or not
int hour = 00;
int minute = 00;
//Variables used to control the flicker delay for the Saturn V and Lunar Module. Original source didn't need this but the Photon runs faster.
int Flicker_Delay = 50; //Delay the looping lights code this many ms so the flicker looks right
unsigned long lastTime_SV = 0;
unsigned long lastTime_LM = 0;
//Timers to start engine sequence, launch and shutdown for Saturn V (SV) and Lunar Module (LM)
Timer timer_SVES(24000, SV_Engine_Sequence, true);
Timer timer_SVL(32000, SV_Launch, true);
Timer timer_SVSD(20700, SV_Shutdown, true);
Timer timer_LMSU(33000, LM_Startup, true);
Timer timer_LMCD(35000, LM_Countdown_Start, true);
Timer timer_CD(1000, Clock_Display, false);
Timer timer_F1202(125750, LM_First_1202, true);
Timer timer_S1202(156500, LM_Second_1202, true);
Timer timer_LMTD(169000, LM_Throttle_Down, true);
Timer timer_1201(186800, LM_1201, true);
Timer timer_LM60(215000, LM_60_Seconds, true);
Timer timer_LM30(245000,LM_30_Seconds, true);
Timer timer_LMSD(261500, LM_Shutdown, true);
Timer timer_LMCE(310000, LM_Countdown_End, true);
Timer timer_LM_Alarm(412, LM_Alarm, true);
//All of the following are setting up the parameters for both of the neopixels.
struct RGBW_SV {
byte r_SV;
byte g_SV;
byte b_SV;
byte w_SV;
};
struct RGBW_LM {
byte r_LM;
byte g_LM;
byte b_LM;
byte w_LM;
};
RGBW_SV colors_SV[] = {
{ 250, 150, 0, 250}, // yellow + white //Removed the 250 value for white to simulate my other non-white neopixel strip
{ 250, 120, 0, 0}, // yellow + white
{ 250, 90, 0, 0}, // orange
{ 250, 30, 0, 0}, // orangie-red
{ 250, 0, 0, 0}, // red
{ 250, 0, 0, 0} // extra red
};
RGBW_LM colors_LM[] = {
{ 250, 150, 0, 250}, // yellow + white //Removed the 250 value for white to simulate my other non-white neopixel strip
{ 250, 120, 0, 0}, // yellow + white
{ 250, 90, 0, 0}, // orange
{ 250, 30, 0, 0}, // orangie-red
{ 250, 0, 0, 0}, // red
{ 250, 0, 0, 0} // extra red
};
int NUMBER_OF_COLORS_SV = sizeof(colors_SV) / sizeof(RGBW_SV);
int NUMBER_OF_COLORS_LM = sizeof(colors_LM) / sizeof(RGBW_LM);
int percentBetween_SV(int a, int b, float percent) {
return (int)(((b - a) * percent) + a);
}
int percentBetween_LM(int a, int b, float percent) {
return (int)(((b - a) * percent) + a);
}
// Parameter 1 = number of pixels in strip
// Parameter 2 = Arduino pin number (most are valid)
// Parameter 3 = pixel type flags:
//#define WS2811 0x00 // 400 KHz datastream (NeoPixel)
//#define WS2812 0x02 // 800 KHz datastream (NeoPixel)
//#define WS2812B 0x02 // 800 KHz datastream (NeoPixel)
//#define WS2813 0x02 // 800 KHz datastream (NeoPixel)
//#define TM1803 0x03 // 400 KHz datastream (Radio Shack Tri-Color Strip)
//#define TM1829 0x04 // 800 KHz datastream ()
//#define WS2812B2 0x05 // 800 KHz datastream (NeoPixel)
//#define SK6812RGBW 0x06 // 800 KHz datastream (NeoPixel RGBW)
//#define WS2812B_FAST 0x07 // 800 KHz datastream (NeoPixel)
//#define WS2812B2_FAST 0x08 // 800 KHz datastream (NeoPixel)
Adafruit_NeoPixel strip_SV = Adafruit_NeoPixel(PIXEL_COUNT_SV, PIN_SV, WS2812); //Select you neopixel type for the third parameter
Adafruit_NeoPixel strip_LM = Adafruit_NeoPixel(PIXEL_COUNT_LM, PIN_LM, SK6812RGBW); //Select you neopixel type for the third parameter
// IMPORTANT: To reduce NeoPixel burnout risk, add 1000 uF capacitor across
// pixel power leads, add 300 - 500 Ohm resistor on first pixel's data input
// and minimize distance between Arduino and first pixel. Avoid connecting
// on a live circuit...if you must, connect GND first.
float scale_SV = 1;
float inc_SV = 0.1;
float dir_SV = 1.0;
byte pixelBrightness_SV[arrayLength_SV];
bool pixelDirection_SV[arrayLength_SV];
float scale_LM = 1;
float inc_LM = 0.1;
float dir_LM = 1.0;
byte pixelBrightness_LM[arrayLength_LM];
bool pixelDirection_LM[arrayLength_LM];
// Prepopulate the pixel arrays
void seedArray_SV() {
for (uint16_t i_SV=0; i_SV < PIXEL_COUNT_SV; i_SV++) {
uint16_t p_SV = i_SV % arrayLength_SV;
pixelBrightness_SV[p_SV] = random(255-FLICKER_AMOUNT_SV, 255);
pixelDirection_SV[p_SV] = !!random(0, 1);
}
}
void seedArray_LM() {
for (uint16_t i_LM=0; i_LM < PIXEL_COUNT_LM; i_LM++) {
uint16_t p_LM = i_LM % arrayLength_LM;
pixelBrightness_LM[p_LM] = random(255-FLICKER_AMOUNT_LM, 255);
pixelDirection_LM[p_LM] = !!random(0, 1);
}
}
void setup() {
SV_state = LIGHTS_OFF;
LL_state = LIGHTS_OFF;
setupMQTT();
//Start the display and clear all values. Also start the neopixels.
//matrix.begin(0x70);
alpha4.begin(0x70);
//matrix.writeDigitRaw(0, 0);
alpha4.writeDigitRaw(0, 0);
//matrix.writeDigitRaw(1, 0);
alpha4.writeDigitRaw(1, 0);
//matrix.drawColon(false);
//matrix.writeDigitRaw(3, 0);
alpha4.writeDigitRaw(2, 0);
//matrix.writeDigitRaw(4, 0);
alpha4.writeDigitRaw(3, 0);
//matrix.writeDisplay();
alpha4.writeDisplay();
strip_SV.begin();
strip_SV.setBrightness(SV_Brightness);
strip_SV.show();
strip_LM.begin();
strip_LM.setBrightness(LM_Brightness);
strip_LM.show();
//Not strictly required but good practice to declare pin modes
pinMode(SV_COUNTDOWN, OUTPUT);
pinMode(SV_SHUTDOWN, OUTPUT);
pinMode(LM_LANDING, OUTPUT);
pinMode(Fuel_Red, OUTPUT);
pinMode(Fuel_Green, OUTPUT);
pinMode(Fuel_Blue, OUTPUT);
pinMode(Alarm_1201, OUTPUT);
pinMode(Alarm_1202, OUTPUT);
//Set all of the light/audio control pins to inactive states
digitalWrite(SV_COUNTDOWN, HIGH);
digitalWrite(SV_SHUTDOWN, HIGH);
digitalWrite(LM_LANDING, HIGH);
digitalWrite(Fuel_Red, LOW);
digitalWrite(Fuel_Green, LOW);
digitalWrite(Fuel_Blue, LOW);
digitalWrite(Alarm_1201, HIGH);
digitalWrite(Alarm_1202, HIGH);
//Subscribe to whatever Publishes you want to listen to and declare the Particle Variables you want on the device
Particle.subscribe("Launch", Launch, MY_DEVICES);
Particle.subscribe("Landing", Landing, MY_DEVICES);
Particle.variable("SV_Status", SV_Status);
Particle.variable("LM_Status", LM_Status);
//Time zone declaration required for the clock to work properly. For those in the US, keep in mind that Daylight Savings isn't accounted for.
Time.zone(-6);
}
void loop() {
if (client.isConnected()) {
client.loop();
if (SV_state == SHOULD_TURN_ON) {
SV_turnOn();
} else if (SV_state == SHOULD_TURN_OFF) {
SV_turnOff();
}
if (LL_state == SHOULD_TURN_ON) {
LL_turnOn();
} else if (LL_state == SHOULD_TURN_OFF) {
LL_turnOff();
}
}
else {
delay(900000); //changed this from 1000
System.reset();
}
Month = Time.month();
Day = Time.day();
Hour = Time.hour();
Minute = Time.minute();
Last_Second = Second;
Second = Time.second();
//Have the Saturn V launch every year on the Apollo 11 launch anniversary day and time
if (Month == 7 and Day == 16 and Hour == 8 and Minute == 31 and Second == 28 and Second-Last_Second != 0) {
Particle.publish("Launch", "Audio", 60, PRIVATE);
}
//Have the Saturn V TLI end every year on the Apollo 11 TLI anniversary day and time
if (Month == 7 and Day == 16 and Hour == 11 and Minute == 21 and Second == 46 and Second-Last_Second != 0) {
Particle.publish("Launch", "NoGo", 60, PRIVATE);
}
//Have the LM land every year on the Apollo 11 landing anniversary day and time
if (Month == 7 and Day == 20 and Hour == 15 and Minute == 13 and Second == 24 and Second-Last_Second != 0) {
Particle.publish("Landing", "Audio", 60, PRIVATE);
}
//Variables for keeping track of the current 'time' for the neopixel flicker delay
unsigned long now_SV = millis();
unsigned long now_LM = millis();
//Saturn V Lights Loop (it's fairly long)
//Set of if statements to change the brightness level based on various scenarios/flags
if (((now_SV - lastTime_SV) >= Flicker_Delay) and Launch_Flag == true) {
lastTime_SV = now_SV;
if (Audio_Launch_Flag == true) {
if (Engine_Launch_Flag == true){
//if statement to ramp up SV Brightness from low to high for full launch
if (SV_Brightness < 255) {
SV_Brightness += 1;
strip_SV.setBrightness(SV_Brightness);
}
}
else if (Engine_Sequence_Flag == true) {
//if statement to ramp up SV_Brightness from off to low for engine startup
if (SV_Brightness < 5) {
SV_Brightness += 1;
strip_SV.setBrightness(SV_Brightness);
}
}
}
else {
//if statement to ramp up SV Brightness from 0 to full for a normal launch
if (SV_Brightness <255) {
SV_Brightness += 1;
strip_SV.setBrightness(SV_Brightness);
}
}
// how many leds for each color
int ledsPerColor_SV = ceil(PIXEL_COUNT_SV / (NUMBER_OF_COLORS_SV-1));
// the scale animation direction
if (scale_SV <= 0.5 || scale_SV >= 1) {
dir_SV = dir_SV * -1;
}
// add a random amount to inc
inc_SV = ((float)random(0, 50)/1000);
// add the increment amount to the scale
scale_SV += (inc_SV * dir_SV);
// constrain the scale
scale_SV = constrain(scale_SV, 0.5, 1);
for (uint16_t i_SV=0; i_SV < PIXEL_COUNT_SV; i_SV++) {
uint16_t p_SV = i_SV % arrayLength_SV;
float val_SV = ((float)i_SV * scale_SV) / (float)ledsPerColor_SV;
int currentIndex_SV = floor(val_SV);
int nextIndex_SV = ceil(val_SV);
float transition_SV = fmod(val_SV, 1);
// color variations
if (pixelDirection_SV[p_SV]) {
pixelBrightness_SV[p_SV] += FLICKER_SPEED_SV;
if (pixelBrightness_SV[p_SV] >= 255) {
pixelBrightness_SV[p_SV] = 255;
pixelDirection_SV[p_SV] = false;
}
} else {
pixelBrightness_SV[p_SV] -= FLICKER_SPEED_SV;
if (pixelBrightness_SV[p_SV] <= 255-FLICKER_AMOUNT_SV) {
pixelBrightness_SV[p_SV] = 255-FLICKER_AMOUNT_SV;
pixelDirection_SV[p_SV] = true;
}
}
RGBW_SV currentColor_SV = colors_SV[currentIndex_SV];
RGBW_SV nextColor_SV = colors_SV[nextIndex_SV];
float flux_SV = (float)pixelBrightness_SV[p_SV] / 255;
byte r_SV = percentBetween_SV(currentColor_SV.r_SV, nextColor_SV.r_SV, transition_SV) * flux_SV;
byte g_SV = percentBetween_SV(currentColor_SV.g_SV, nextColor_SV.g_SV, transition_SV) * flux_SV;
byte b_SV = percentBetween_SV(currentColor_SV.b_SV, nextColor_SV.b_SV, transition_SV) * flux_SV;
byte w_SV = percentBetween_SV(currentColor_SV.w_SV, nextColor_SV.w_SV, transition_SV) * flux_SV;
//You may have to swap the RGBW order depending on the neopixel you use
strip_SV.setPixelColor(i_SV, r_SV, g_SV, b_SV, w_SV);
}
strip_SV.show();
}
//Slowly ramp the leds down if you shut it off
else if(Launch_Flag == false) {
if (SV_Brightness > 0) {
SV_Brightness -= 5;
if (SV_Brightness < 0) {
SV_Brightness = 0;
}
strip_SV.setBrightness(SV_Brightness);
}
strip_SV.show();
}
//Lunar Module Lights Loop (it's fairly long)
//Set of if statements to change the brightness level based on various scenarios/flags
if (((now_LM - lastTime_LM) >= Flicker_Delay) and Landing_Flag == true) {
lastTime_LM = now_LM;
if (Audio_Landing_Flag == true) {
if (LM_Shutdown_Flag == true){
//if statement to turn down LM_Brightness from low to off
if (LM_Brightness > 0) {
LM_Brightness -= 1;
strip_LM.setBrightness(LM_Brightness);
}
}
else if (Throttle_Down_Flag == true) {
//if statement to turn down LM_Brightness from high to low
if (LM_Brightness > 25) {
LM_Brightness -= 1;
strip_LM.setBrightness(LM_Brightness);
}
}
else if (PDI_Flag == true) {
//if statement to turn up LM_Brightness from off to high
if (LM_Brightness < 255) {
LM_Brightness += 1;
strip_LM.setBrightness(LM_Brightness);
}
}
}
else {
//if statement to ramp up LM Brightness from 0 to full
if (LM_Brightness <255) {
LM_Brightness += 1;
strip_LM.setBrightness(LM_Brightness);
}
}
// how many leds for each color
int ledsPerColor_LM = ceil(PIXEL_COUNT_LM / (NUMBER_OF_COLORS_LM-1));
// the scale animation direction
if (scale_LM <= 0.5 || scale_LM >= 1) {
dir_LM = dir_LM * -1;
}
// add a random amount to inc
inc_LM = ((float)random(0, 50)/1000);
// add the increment amount to the scale
scale_LM += (inc_LM * dir_LM);
// constrain the scale
scale_LM = constrain(scale_LM, 0.5, 1);
for (uint16_t i_LM=0; i_LM < PIXEL_COUNT_LM; i_LM++) {
uint16_t p_LM = i_LM % arrayLength_LM;
float val_LM = ((float)i_LM * scale_LM) / (float)ledsPerColor_LM;
int currentIndex_LM = floor(val_LM);
int nextIndex_LM = ceil(val_LM);
float transition_LM = fmod(val_LM, 1);
// color variations
if (pixelDirection_LM[p_LM]) {
pixelBrightness_LM[p_LM] += FLICKER_SPEED_LM;
if (pixelBrightness_LM[p_LM] >= 255) {
pixelBrightness_LM[p_LM] = 255;
pixelDirection_LM[p_LM] = false;
}
} else {
pixelBrightness_LM[p_LM] -= FLICKER_SPEED_LM;
if (pixelBrightness_LM[p_LM] <= 255-FLICKER_AMOUNT_LM) {
pixelBrightness_LM[p_LM] = 255-FLICKER_AMOUNT_LM;
pixelDirection_LM[p_LM] = true;
}
}
RGBW_LM currentColor_LM = colors_LM[currentIndex_LM];
RGBW_LM nextColor_LM = colors_LM[nextIndex_LM];
float flux_LM = (float)pixelBrightness_LM[p_LM] / 255;
byte r_LM = percentBetween_LM(currentColor_LM.r_LM, nextColor_LM.r_LM, transition_LM) * flux_LM;
byte g_LM = percentBetween_LM(currentColor_LM.g_LM, nextColor_LM.g_LM, transition_LM) * flux_LM;
byte b_LM = percentBetween_LM(currentColor_LM.b_LM, nextColor_LM.b_LM, transition_LM) * flux_LM;
byte w_LM = percentBetween_LM(currentColor_LM.w_LM, nextColor_LM.w_LM, transition_LM) * flux_LM;
//You may have to swap the RGBW order depending on the neopixel you use
strip_LM.setPixelColor(i_LM, g_LM, r_LM, b_LM, w_LM);
}
strip_LM.show();
}
//Slowly ramp the leds down if you shut it off
else if (Landing_Flag == false) {
if (LM_Brightness > 0) {
LM_Brightness -= 5;
if (LM_Brightness < 0) {
LM_Brightness = 0;
}
strip_LM.setBrightness(LM_Brightness);
}
strip_LM.show();
}
//If statement to display the current time if the device knows the time and is commanded to
if (Time_Display_Flag == true and Time.isValid()) {
hour = Time.hour();
minute = Time.minute();
if (hour < 10) {
alpha4.writeDigitRaw(0, 0); // blank
}
else {
alpha4.writeDigitAscii(0, hour / 10 + '0');
}
alpha4.writeDigitAscii(1, hour % 10 + '0');
alpha4.writeDigitAscii(2, minute / 10 + '0');
alpha4.writeDigitAscii(3, minute % 10 + '0');
}
//Display the timer/stopwatch every second and count down/up depending on the scenario
if (Clock_Display_Flag == true) {
Clock_Display_Flag = false;
if(Clock_Type == 0) { //Timer scenario, which in my case is always less than 10 minutes and the first minute digit is a '-'
if(Second_2 == 0 and Second_1 == 0 and Minute_2 == 0) {
Clock_Type = 1;
//matrix.writeDigitNum(0, Minute_1);
//alpha4.writeDigitAscii(0, Minute_1 + '0');
alpha4.writeDigitAscii(0, 'T');
alpha4.writeDigitAscii(1, '+');
}
else if(Second_2 == 0 and Second_1 == 0) {
Minute_2 -= 1;
Second_1 = 5;
Second_2 = 9;
//matrix.writeDigitNum(1, Minute_2);
alpha4.writeDigitAscii(1, Minute_2 + '0');
//matrix.writeDigitNum(3, Second_1);
alpha4.writeDigitAscii(2, Second_1 + '0');
//matrix.writeDigitNum(4, Second_2);
alpha4.writeDigitAscii(3, Second_2 + '0');
}
else if(Second_2 ==0){
Second_1 -=1;
Second_2=9;
//matrix.writeDigitNum(3, Second_1);
alpha4.writeDigitAscii(2, Second_1 + '0');
//matrix.writeDigitNum(4, Second_2);
alpha4.writeDigitAscii(3, Second_2 + '0');
}
else {
Second_2 -=1;
//matrix.writeDigitNum(4, Second_2);
alpha4.writeDigitAscii(3, Second_2 + '0');
}
}
if(Clock_Type == 1) { //Stopwatch scenario
if(Minute_2 == 9 and Second_1 == 5 and Second_2 == 9) {
Minute_1 += 1;
Minute_2 = 0;
Second_1 = 0;
Second_2 = 0;
//matrix.writeDigitNum(0, Minute_1);
alpha4.writeDigitAscii(0, Minute_1 + '0');
//matrix.writeDigitNum(1, Minute_2);
alpha4.writeDigitAscii(1, Minute_2 + '0');
//matrix.writeDigitNum(3, Second_1);
alpha4.writeDigitAscii(2, Second_1 + '0');
//matrix.writeDigitNum(4, Second_2);
alpha4.writeDigitAscii(3, Second_2 + '0');
}
else if(Second_1 ==5 and Second_2 ==9) {
Minute_2 += 1;
Second_1 = 0;
Second_2 = 0;
if (Minute_1 == 0) {
alpha4.writeDigitAscii(0, '+');
}
//matrix.writeDigitNum(1, Minute_2);
alpha4.writeDigitAscii(1, Minute_2 + '0');
//matrix.writeDigitNum(3, Second_1);
alpha4.writeDigitAscii(2, Second_1 + '0');
//matrix.writeDigitNum(4, Second_2);
alpha4.writeDigitAscii(3, Second_2 + '0');
}
else if(Second_2 == 9) {
Second_1 += 1;
Second_2 = 0;
//matrix.writeDigitNum(3, Second_1);
alpha4.writeDigitAscii(2, Second_1 + '0');
//matrix.writeDigitNum(4, Second_2);
alpha4.writeDigitAscii(3, Second_2 + '0');
}
else {
Second_2 += 1;
//matrix.writeDigitNum(4, Second_2);
alpha4.writeDigitAscii(3, Second_2 + '0');
}
}
}
//matrix.writeDisplay(); //Display the new time
alpha4.writeDisplay();
}
bool setupMQTT() {
client.connect(DeviceID, Username, Password);
if (client.isConnected()) {
client.publish("homeassistant/light/SV_Controller/config", SV_discovery_payload, "retain=True");
client.publish("homeassistant/light/LL_Controller/config", LL_discovery_payload, "retain=True");
client.subscribe(SV_command_topic);
client.subscribe(LL_command_topic);
client.publish(SV_state_topic, off);
client.publish(LL_state_topic, off);
return true;
} else {
return false;
}
}
const unsigned int on_command_size = 3;
const unsigned int off_command_size = 4;
const byte on_command[on_command_size] = "ON";
const byte off_command[off_command_size] = "OFF";
bool equal(const byte* a, const byte* b, unsigned int length_a, unsigned int length_b) {
for(int i = 0; i < length_a; i++) {
if (i + 1 >= length_b)
return false;
if (a[i] != b[i])
return false;
}
return true;
}
void callback(char* topic, byte* payload, unsigned int length) {
if(!strcmp(topic, SV_command_topic)) {
if (equal(payload, on_command, length, on_command_size)) {
SV_state = SHOULD_TURN_ON;
} else if (equal(payload, off_command, length, off_command_size)) {
SV_state = SHOULD_TURN_OFF;
}
}
if(!strcmp(topic, LL_command_topic)) {
if (equal(payload, on_command, length, on_command_size)) {
LL_state = SHOULD_TURN_ON;
} else if (equal(payload, off_command, length, off_command_size)) {
LL_state = SHOULD_TURN_OFF;
}
}
}
int SV_turnOn() {
Launch_Flag = true;
SV_Status = "We Have Liftoff!";
SV_state = LIGHTS_ON;
client.publish(SV_state_topic, on);
return 1;
}
int LL_turnOn() {
Landing_Flag = true;
LM_Status = "PDI Started";
LL_state = LIGHTS_ON;
client.publish(LL_state_topic, on);
return 1;
}
int SV_turnOff() {
Launch_Flag = false;
SV_Status = "TLI Cutoff";
SV_state = LIGHTS_OFF;
client.publish(SV_state_topic, off);
return 0;
}
int LL_turnOff() {
Landing_Flag = false;
LM_Status = "The Eagle Has Landed!";
LL_state = LIGHTS_OFF;
client.publish(LL_state_topic, off);
return 0;
}
//Scenarios for controlling the Saturn V Neopixels based on a Publish event
void Launch(const char *event, const char *data)
{
if (strcmp(data,"Go")==0) {
Launch_Flag = true;
SV_Status = "We Have Liftoff!";
}
else if (strcmp(data,"NoGo")==0) {
if (Audio_Launch_Flag == true) {
SV_Status = "TLI Started";
digitalWrite(SV_SHUTDOWN,LOW);
timer_SVSD.start();
Audio_Launch_Flag = false;
Engine_Launch_Flag = false;
timer_CD.stop();
//Clear the timer display
//matrix.writeDigitRaw(0, 0);
alpha4.writeDigitRaw(0, 0);
//matrix.writeDigitRaw(1, 0);
alpha4.writeDigitRaw(1, 0);
//matrix.drawColon(false);
//matrix.writeDigitRaw(3, 0);
alpha4.writeDigitRaw(2, 0);
//matrix.writeDigitRaw(4, 0);
alpha4.writeDigitRaw(3, 0);
//matrix.writeDisplay();
alpha4.writeDisplay();
Time_Display_Flag = true; //Start displaying the time
}
else {
Launch_Flag = false;
SV_Status = "TLI Cutoff";
}
}
else if (strcmp(data,"Audio")==0) {
SV_Brightness = 0;
strip_SV.setBrightness(SV_Brightness);
SV_Status = "Countdown Started";
Audio_Launch_Flag = true;
Launch_Flag = true;
Engine_Sequence_Flag = false;
Engine_Launch_Flag = false;
digitalWrite(SV_COUNTDOWN,LOW);
Time_Display_Flag = false; //Stop displaying the time
Clock_Display_Flag = false;
Audio_Landing_Flag = false;
Landing_Flag = false;
Clock_Type = 0; //0 is for a timer (count down), 1 is for a stopwatch (count up)
Minute_1 = 0;
Minute_2 = 0;
Second_1 = 3;
Second_2 = 1;
timer_CD.start();
timer_SVES.start();
timer_SVL.start();
//matrix.writeDigitRaw(0,0x40); //Draw just the middle line, a minus sign.
alpha4.writeDigitAscii(0, 'T');
//matrix.writeDigitNum(1, Minute_2);
alpha4.writeDigitAscii(1, '-');
//matrix.drawColon(true);
//matrix.writeDigitNum(3, Second_1);
alpha4.writeDigitAscii(2, Second_1 + '0');
//matrix.writeDigitNum(4, Second_2);
alpha4.writeDigitAscii(3, Second_2 + '0');
}
}
//Scenarios for controlling the Saturn V Neopixels based on a Publish event
void Landing(const char *event, const char *data)
{
if (strcmp(data,"Go")==0) {
Landing_Flag = true;
LM_Status = "PDI Started";
}
else if (strcmp(data,"NoGo")==0) {
Landing_Flag = false;
LM_Status = "The Eagle Has Landed!";
}
else if (strcmp(data,"Audio")==0) {
LM_Brightness = 0;
strip_LM.setBrightness(LM_Brightness);
LM_Status = "Landing Started";
Audio_Landing_Flag = true;
Landing_Flag = true;
PDI_Flag = false;
Throttle_Down_Flag = false;
LM_Shutdown_Flag = false;
Time_Display_Flag = false; //Stop displaying the time
Clock_Display_Flag = false;
Audio_Launch_Flag = false;
Engine_Launch_Flag = false;
Launch_Flag = false;
digitalWrite(LM_LANDING,LOW);
timer_CD.stop();
timer_LMSU.start();
timer_LMCD.start();
timer_F1202.start();
timer_S1202.start();
timer_LMTD.start();
timer_1201.start();
timer_LM60.start();
timer_LM30.start();
timer_LMSD.start();
timer_LMCE.start();
//Clear the timer display
//matrix.writeDigitRaw(0, 0);
alpha4.writeDigitRaw(0, 0);
//matrix.writeDigitRaw(1, 0);
alpha4.writeDigitRaw(1, 0);
//matrix.drawColon(false);
//matrix.writeDigitRaw(3, 0);
alpha4.writeDigitRaw(2, 0);
//matrix.writeDigitRaw(4, 0);
alpha4.writeDigitRaw(3, 0);
//matrix.writeDisplay();
alpha4.writeDisplay();
}
}
//The beginning of a LOT of timers used for coordinating the audio/visual 'experience' :)
void SV_Engine_Sequence()
{
SV_Status = "Engines Started";
Engine_Sequence_Flag = true;
digitalWrite(SV_COUNTDOWN,HIGH);
}
void SV_Launch()
{
SV_Status = "We Have Liftoff!";
Engine_Launch_Flag=true;
Engine_Sequence_Flag=false;
}
void SV_Shutdown()
{
SV_Status = "TLI Cutoff";
Launch_Flag = false;
digitalWrite(SV_SHUTDOWN,HIGH);
}
void LM_Startup()
{
LM_Status = "PDI Started";
PDI_Flag = true;
digitalWrite(LM_LANDING,HIGH);
}
void LM_Countdown_Start()
{
//Code to start countdown timer and start displaying it
Clock_Type = 0; //0 is for a timer (count down), 1 is for a stopwatch (count up)
Minute_1 = 0;
Minute_2 = 4;
Second_1 = 0;
Second_2 = 0;
//matrix.writeDigitRaw(0,0x40); //Draw just the middle line, a minus sign.
alpha4.writeDigitAscii(0, '-');
//matrix.writeDigitNum(1, Minute_2);
alpha4.writeDigitAscii(1, Minute_2 + '0');
//matrix.drawColon(true);
//matrix.writeDigitNum(3, Second_1);
alpha4.writeDigitAscii(2, Second_1 + '0');
//matrix.writeDigitNum(4, Second_2);
alpha4.writeDigitAscii(3, Second_2 + '0');
alpha4.writeDisplay();
//Turn Fuel light to white
digitalWrite(Fuel_Red, HIGH);
digitalWrite(Fuel_Green, HIGH);
digitalWrite(Fuel_Blue, HIGH);
timer_CD.start();
}
void Clock_Display()
{
Clock_Display_Flag = true;
}
void LM_First_1202()
{
Alarm_Number = 1202;
Alarm_Loops = 32;
Alarm_Counter = 1;
digitalWrite(Alarm_1202, LOW);
timer_LM_Alarm.start();
}
void LM_Second_1202()
{
Alarm_Number = 1202;
Alarm_Loops = 38;
Alarm_Counter = 1;
digitalWrite(Alarm_1202, LOW);
timer_LM_Alarm.start();
}
void LM_Throttle_Down()
{
LM_Status = "LM Engines Throttled Down";
PDI_Flag = false;
Throttle_Down_Flag = true;
}
void LM_1201()
{
Alarm_Number = 1201;
Alarm_Loops = 34;
Alarm_Counter = 1;
digitalWrite(Alarm_1201, LOW);
timer_LM_Alarm.start();
}
void LM_60_Seconds()
{
//Turn Fuel light to yellow
digitalWrite(Fuel_Red, HIGH);
digitalWrite(Fuel_Green, HIGH);
digitalWrite(Fuel_Blue, LOW);
}
void LM_30_Seconds()
{
//Turn Fuel light to red
digitalWrite(Fuel_Red, HIGH);
digitalWrite(Fuel_Green, LOW);
digitalWrite(Fuel_Blue, LOW);
}
void LM_Shutdown()
{
LM_Status = "The Eagle Has Landed!";
Throttle_Down_Flag = false;
LM_Shutdown_Flag = true;
timer_CD.stop(); //Stop the fuel countdown timer
}
void LM_Countdown_End()
{
//Turn Fuel light off
digitalWrite(Fuel_Red, LOW);
digitalWrite(Fuel_Green, LOW);
digitalWrite(Fuel_Blue, LOW);