-
Notifications
You must be signed in to change notification settings - Fork 10
/
OpenLedRace.ino
1617 lines (1466 loc) · 55.2 KB
/
OpenLedRace.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
/*
* ____ _ ______ _____ _____
* / __ \ | | | ____| __ \ | __ \
*| | | |_ __ ___ _ __ | | | |__ | | | | | |__) |__ _ ___ ___
*| | | | '_ \ / _ \ '_ \ | | | __| | | | | | _ // _` |/ __/ _ \
*| |__| | |_) | __/ | | | | |____| |____| |__| | | | \ \ (_| | (_| __/
* \____/| .__/ \___|_| |_| |______|______|_____/ |_| \_\__,_|\___\___|
* | |
* |_|
*/
/*
* OpenLedRace.cpp
*
* Extended version of the OpenLedRace "version Basic for PCB Rome Edition. 2 Player, without Boxes Track"
*
* Extensions are:
* Accelerator MPU6050 input.
* Classes for Car, Bridge, Ramp and Loop.
* Dynamic activation of up to 4 cars.
* Light effects by NeoPattern library.
* Tone generation without dropouts by use of hardware timer output.
* Winner melody by PlayRTTTL library.
* Compensation for blocked millis() timer during draw.
* Checks for RAM availability.
* Overlapping of cars is handled by using addPixelColor() for drawing.
*
* One 1200 mA Li-ion Battery lasts for around 6 hours
*
* You need to install "Adafruit NeoPixel" library under "Tools -> Manage Libraries..." or "Ctrl+Shift+I" -> use "neoPixel" as filter string
* You also need to install "NeoPatterns" and "PlayRtttl" library under "Tools -> Manage Libraries..." or "Ctrl+Shift+I"
*
* Copyright (C) 2020-2023 Armin Joachimsmeyer
* armin.joachimsmeyer@gmail.com
*
* This file is part of OpenledRace https://github.com/ArminJo/OpenledRace.
* This file is part of NeoPatterns https://github.com/ArminJo/NeoPatterns.
*
* NeoPatterns and OpenledRace is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/gpl.html>.
*/
/*
* Open LED Race
* An minimalist cars race for LED strip
*
* by gbarbarov@singulardevices.com for Arduino day Seville 2019
* https://www.hackster.io/gbarbarov/open-led-race-a0331a
* https://twitter.com/openledrace
*
* https://gitlab.com/open-led-race
* https://openledrace.net/open-software/
*/
#include <Arduino.h>
#include "PlayRtttl.hpp"
/*
* Enable only these 3 patterns to save program space
*/
#define ENABLE_PATTERN_SCANNER_EXTENDED
#define ENABLE_PATTERN_COLOR_WIPE
#define ENABLE_PATTERN_STRIPES
#include "NeoPatterns.hpp"
//#define TRACE
//#define DEBUG
#define INFO // Enable additional informative output like e.g. offsets,
#include "DebugLevel.h" // to propagate debug level
#if defined(INFO) && defined(__AVR__)
#include "AVRUtils.h" // for initStackFreeMeasurement() and printRAMInfo()
#endif
// for hunting errors
//#include "AvrTracing.hpp"
#define VERSION_EXAMPLE "1.3"
// 1.4 - work in progress
// 1.3 Moved Bridge and loop, VU Bar animations
// 1.2 Improvements from Hannover Maker Faire
// 1.1 Hannover Maker Faire version
//#define BIG_NUMBERS_ALTERNATIVE // use alternative font for big numbers
//#define USE_NO_LCD // this suppresses the error tone and print if LCD was not found
//#define TEST_MODE
//#define TIMING_TEST
#define ENABLE_ACCELERATOR_INPUT // Enables readout of MPU6050 IMU as input. Saves 320 bytes program space and 10 bytes RAM.
#define USE_ACCELERATION_NEOPIXEL_BARS // Shows the low pass value of user acceleration input (button or IMU) on an 8 Neopixel bar
//#define BRIDGE_NO_NEOPATTERNS // No patterns on bridge. Saves 13 bytes RAM
//#define LOOP_NO_NEOPATTERNS // No patterns on loop. Saves 4 bytes RAM
#if defined(ENABLE_ACCELERATOR_INPUT)
/*
* Modifiers for the MPU6050IMUData library to save speed and space
*/
#define DO_NOT_USE_GYRO
#define USE_ONLY_ACCEL_FLOATING_OFFSET
//#define USE_SOFT_I2C_MASTER // Requires SoftI2CMaster.h + SoftI2CMasterConfig.h. Saves 2110 bytes program memory and 200 bytes RAM compared with Arduino Wire
#include "MPU6050IMUData.hpp" // This defines USE_SOFT_I2C_MASTER, if SoftI2CMasterConfig.h is available
#endif // #if defined(ENABLE_ACCELERATOR_INPUT)
#include "LongUnion.h"
#define LCD_I2C_ADDRESS 0x27 // Default LCD address is 0x27 for a 20 chars and 4 line / 2004 display
#include "LiquidCrystal_I2C.hpp" // This defines USE_SOFT_I2C_MASTER, if SoftI2CMasterConfig.h is available. Use only the modified version delivered with this program!
LiquidCrystal_I2C myLCD(LCD_I2C_ADDRESS, 20, 4);
#define USE_SERIAL_2004_LCD // required by LCDBigNumbers.hpp
#include "LCDBigNumbers.hpp" // Include sources for LCD big number generation
LCDBigNumbers BigNumbers(&myLCD, BIG_NUMBERS_FONT_3_COLUMN_4_ROWS_VARIANT_1);
void checkForLCDConnected();
bool sSerialLCDAvailable;
/*
* Pin layout - adapt it to your need
*/
#define PIN_PLAYER_1_VU_BAR 2 // RED
#define PIN_PLAYER_2_VU_BAR 3// GREEN
#define PIN_PLAYER_1_BUTTON 4 // RED
#define PIN_PLAYER_2_BUTTON 5 // GREEN
#if !defined(ENABLE_ACCELERATOR_INPUT)
#define PIN_PLAYER_3_BUTTON 6
#endif
#define PIN_RESET_GAME_BUTTON 7
#define PIN_NEOPIXEL_TRACK 8
#if defined(TIMING_TEST)
#define PIN_TIMING 9
#endif
#define PIN_MANUAL_PARAMETER_MODE 9 // if connected to ground, analog inputs for parameters are used
#define PIN_BUZZER 11 // must be pin 11, since we use the direct hardware tone output for ATmega328, which is not disturbed by Neopixel show()
#define PIN_GRAVITY A0
#define PIN_FRICTION A1
#define PIN_DRAG A2
#define ONLY_PLOTTER_OUTPUT_PIN 12 // Verbose output to Arduino Serial Monitor is disabled, if connected to ground. This is intended for Arduino Plotter mode.
bool sOnlyPlotterOutput;
#define ANALOG_OFFSET 20 // Bias/offset to get real 0 analog value, because of high LED current on Breadboard, which cause a ground bias.
/*
* The track
* Think of 1 Pixel = 1 meter
*/
#define NUMBER_OF_TRACK_PIXELS 300 // Number of LEDs in strip
/*
* The cars
* Maximum number of cars supported. Each car is individually activated at runtime
* by first press of its button or movement of its accelerator input.
*/
#if defined(ENABLE_ACCELERATOR_INPUT)
#define NUMBER_OF_CARS 2 // Currently we can handle only 2 distinct accelerometers.
#define ACCELERATOR_TRIGGER_VALUE 32 // The value of getAcceleratorValueShift8() to trigger a new race
#else
#define NUMBER_OF_CARS 4
#endif
#define CAR_1_COLOR (color32_t)COLOR32_RED
#define CAR_2_COLOR (color32_t)COLOR32_GREEN
#define CAR_3_COLOR (color32_t)COLOR32_BLUE
const char Car1ColorString[] PROGMEM = "RED";
const char Car2ColorString[] PROGMEM = "GREEN";
const char Car3ColorString[] PROGMEM = "BLUE";
/*
* The bridge with a ramp up, a flat platform and a ramp down
*/
#define NUMBER_OF_BRIDGES 1
#define BRIDGE_1_START 87
#define BRIDGE_1_RAMP_LENGTH 21 // in pixel
#define BRIDGE_1_PLATFORM_LENGTH 20 // > 0 for bridges with a ramp up a flat bridge and a ramp down
#define BRIDGE_1_HEIGHT 15 // in pixel -> 45 degree slope here
#define RAMP_COLOR COLOR32_CYAN_QUARTER // COLOR32(0,64,64)
#define RAMP_COLOR_DIMMED COLOR32(0,8,8)
/*
* The loop
*/
#define NUMBER_OF_LOOPS 1
#define LOOP_1_UP_START 228
#define LOOP_1_LENGTH 48 // in pixel
#define LOOP_COLOR COLOR32_PURPLE_QUARTER // COLOR32(64,0,64)
#define LOOP_DIMMED_COLOR COLOR32(8,0,8)
#define GAMMA_FOR_DIMMED_VALUE 160
NeoPatterns track = NeoPatterns(NUMBER_OF_TRACK_PIXELS, PIN_NEOPIXEL_TRACK, NEO_GRB + NEO_KHZ800);
#if defined(USE_ACCELERATION_NEOPIXEL_BARS)
//#define ACCELERATION_BAR_SCALE_VALUE 100
/*
* The central NeoPixel object used for every bar, since there is no persistence needed for the bar pixel content
*/
NeoPixel AccelerationCommonNeopixelBar = NeoPixel(8, 0, NEO_GRB + NEO_KHZ800);
#endif
/*
* Game loop timing
*/
#define MILLISECONDS_PER_LOOP 20 // 50 fps
// Required for correction of the millis() timer
#define MILLIS_FOR_TRACK_TO_SHOW (NUMBER_OF_TRACK_PIXELS / 33) // 33 pixels can be sent per ms. 9ms for 300 pixels.
#define ANIMATION_INTERVAL_MILLIS 10000
#define FULL_GRAVITY 100 // 100 is gravity for a vertical slope, 0 for a horizontal and 70 (sqrt(0,5)*100) for 45 degree.
/*
* Sound
*/
unsigned long sBeepEndMillis = 0; // for special sounds - overtaking and leap
int sBeepFrequency = 0;
bool sSoundEnabled = true; // not really used yet - always true
/*
* Race control
*/
#if defined(TEST_MODE)
#define START_ANIMATION_MILLIS 500 // The duration of the start animation
#define WINNER_ANIMATION_DELAY_MILLIS 1000 // The time to show the end of race situation
#define LAPS_PER_RACE 255
#else
#define START_ANIMATION_MILLIS 1500 // The duration of the start animation
#define WINNER_ANIMATION_DELAY_MILLIS 2000 // The time to show the end of race situation
#define WINNER_MINIMAL_ANIMATION_DURATION_MILLIS 2000 // The minimum time time to show the winner animation + sound
#define LAPS_PER_RACE 5
#endif
// Main loop modes
#define MODE_IDLE 0
#define MODE_START 1
#define MODE_RACE 2
uint8_t sLoopMode = MODE_IDLE;
uint16_t sLoopCountForDebugPrint;
uint8_t sIndexOfLeadingCar = 0; // To check for playing overtaking sound.
/*
* Car control
* we get a press each 70 to 150 milliseconds / each 3th to 7th loop
*/
#define ENERGY_PER_BUTTON_PRESS 0.2 // As pixel per loop
#define FRICTION_PER_LOOP 0.006
#define AERODYNAMIC_DRAG_PER_LOOP 0.0002 // can also be 0.0
#define GRAVITY_FACTOR_FOR_MAP 0.0007 // gravity constant for gravity values from 0 to 100 => gravity 1 is 0.02 -- 0.001 is too much.
/*
* Forward declarations
*/
void startRace();
void printStartMessage();
void resetAllCars();
void resetAndDrawTrack(bool aDoAnimation);
void resetAndShowTrackWithoutCars();
bool isInRegion(uint16_t aPixelPosition, unsigned int aRegionFirst, unsigned int aRegionLength);
bool isAnyCarInRegion(unsigned int aRegionFirst, unsigned int aRegionLength);
void playError();
void playShutdownMelody();
void playMelodyAndShutdown();
void checkAndHandleWinner();
void checkForOvertakingLeaderCar();
bool checkAllInputs();
void printConfigPinInfo(Print *aSerial, uint8_t aConfigPinNumber, const __FlashStringHelper *aConfigPinDescription);
extern volatile unsigned long timer0_millis; // Used for ATmega328P to adjust for missed millis interrupts
/*
* Helper macro for getting a macro definition as string
*/
#define STR_HELPER(x) #x
#define STR(x) STR_HELPER(x)
void myTone(int aFrequency) {
tone(PIN_BUZZER, aFrequency);
#if defined(TCCR2A)
TCCR2A |= _BV(COM2A0); // switch to direct toggle output at OC2A / pin 11 to enable direct hardware tone output
#endif
}
/*******************************************************************************************
* The RAMP class
* Sets gravity for the ramps and allocates NeoPatterns for the ramps to AnimationForIdle
*
* Requires 10 bytes RAM + sizeof(NeoPatterns) per ramp
* One ramp consists of (aRampLength + 1) pixel with the first and last with half the gravity
* aRampUpStart - First pixel with gravity = 1/2 gravity
* aRampLength - aRampUpStart + aRampLength is last ramp pixel with gravity = 1/2 gravity
* Gravity - 100 is full gravity for vertical slope
*******************************************************************************************/
class Ramp {
public:
#if !defined(BRIDGE_NO_NEOPATTERNS)
NeoPatterns *TrackPtr;
NeoPatterns *RampPatterns;
bool isInitialized;
#endif
uint16_t StartPositionOnTrack;
uint8_t RampLength;
uint8_t RampHeight;
bool isRampDown;
void init(NeoPatterns *aTrackPtr, uint16_t aRampUpStartPositionOnTrack, uint8_t aRampHeight, uint8_t aRampLength,
bool aIsRampDown) {
StartPositionOnTrack = aRampUpStartPositionOnTrack;
RampHeight = aRampHeight;
RampLength = aRampLength;
isRampDown = aIsRampDown;
#if !defined(BRIDGE_NO_NEOPATTERNS)
TrackPtr = aTrackPtr;
/*
* NeoPatterns segments to control light effects on both ramps
* Call malloc() and free() before, since the compiler calls the constructor even
* when the result of malloc() is NULL, which leads to overwrite low memory.
*/
void *tMallocTest = malloc(sizeof(NeoPatterns)); // 67 + 2
if (tMallocTest != NULL) {
free(tMallocTest);
RampPatterns = new NeoPatterns(TrackPtr, StartPositionOnTrack, RampLength, false);
isInitialized = true;
} else {
Serial.print(F("Not enough heap memory ("));
Serial.print(sizeof(NeoPatterns) + 2);
Serial.println(F(") for RampPatterns."));
}
#else
(void) aTrackPtr;
#endif // !defined(BRIDGE_NO_NEOPATTERNS)
}
/*
* Compute Gravity acceleration on the fly
* @ return Values from -100 to +100 with 100 is gravity for a vertical slope, 0 for a horizontal and 70 (sqrt(0,5)*100) for 45 degree.
* Positive values increases speed, negative values decreases speed.
*/
int8_t getGravityAcceleration(uint16_t aPositionOnTrack) {
#if defined(DEBUG)
if (aPositionOnTrack < StartPositionOnTrack || aPositionOnTrack > StartPositionOnTrack + RampLength) {
Serial.print(F("Error! position="));
Serial.print(aPositionOnTrack);
Serial.print(F(" is not on ramp "));
Serial.print(StartPositionOnTrack);
Serial.print(F(" to "));
Serial.println(StartPositionOnTrack + RampLength);
}
#endif
uint8_t tPositionOnRamp = aPositionOnTrack - StartPositionOnTrack;
int8_t tResultingForce = ((uint16_t) (RampHeight * FULL_GRAVITY)) / RampLength; // results in values from 0 to 100
if (!isRampDown) {
tResultingForce = -tResultingForce; // deceleration for ramp up
}
if (tPositionOnRamp == 0 || tPositionOnRamp == RampLength) {
return tResultingForce / 2; // Start and end with half deceleration for ramp up
} else {
return tResultingForce; // Deceleration for ramp up
}
}
void startIdleAnimation(long aRandomValue) {
#if !defined(BRIDGE_NO_NEOPATTERNS)
if (isInitialized) {
uint8_t tDirection;
if (isRampDown) {
tDirection = DIRECTION_DOWN;
} else {
tDirection = DIRECTION_UP;
}
LongUnion tRandom;
tRandom.Long = aRandomValue;
if (tRandom.UBytes[0] & 0x03) {
if (tRandom.UBytes[0] & 0x01) {
//1 + 3
RampPatterns->Stripes(NeoPatterns::Wheel(tRandom.UBytes[0]), (tRandom.UBytes[1] & 0x03) + 1, COLOR32_BLACK, 4,
(tRandom.UBytes[2] & 0x7F) + 64, (tRandom.UBytes[3] & 0x1F) + 4, tDirection);
} else {
//2
initMultipleFallingStars(RampPatterns, COLOR32_WHITE_HALF, 7, (tRandom.UBytes[0] & 0x07) + 1,
(tRandom.UBytes[0] & 0x03) + 2, NULL, tDirection);
}
} else {
if (tRandom.UBytes[0] > 0x03) {
// 0 - especially used for setup
RampPatterns->ColorWipeD(NeoPatterns::Wheel(tRandom.UBytes[0]), START_ANIMATION_MILLIS, 0, tDirection);
} else {
// 0 - especially used for setup
RampPatterns->ColorWipeD(RAMP_COLOR, START_ANIMATION_MILLIS, 0, tDirection);
}
// RampPatterns->ScannerExtendedD(COLOR32_BLUE_HALF, 8, START_ANIMATION_MILLIS, 2,
// FLAG_SCANNER_EXT_ROCKET | FLAG_SCANNER_EXT_VANISH_COMPLETE | FLAG_SCANNER_EXT_START_AT_BOTH_ENDS, tDirection);
}
}
#endif
}
/*
* Draw ramp and dim brightness, if car is on ramp
*/
void draw() {
#if !defined(BRIDGE_NO_NEOPATTERNS)
bool tCarIsOnRamp = isAnyCarInRegion(StartPositionOnTrack, RampLength);
color32_t tColor = RAMP_COLOR;
if (tCarIsOnRamp) {
tColor = TrackPtr->dimColorWithGamma5(tColor, 160);
}
TrackPtr->fillRegion(tColor, StartPositionOnTrack, RampLength);
#endif
}
};
/*******************************************************************************************
* The BRIDGE class
* Requires 23 bytes + 2 * sizeof(NeoPatterns) RAM per bridge
*******************************************************************************************/
class Bridge {
#if !defined(BRIDGE_NO_NEOPATTERNS)
public:
NeoPatterns *TrackPtr;
bool isInitialized;
#endif
Ramp RampUp;
Ramp RampDown;
public:
void init(NeoPatterns *aTrackPtr, uint16_t aBridgeStartPositionOnTrack, uint8_t aBridgeHeight, uint8_t aRampLength,
uint8_t aRampPlatformLength) {
RampUp.init(aTrackPtr, aBridgeStartPositionOnTrack, aBridgeHeight, aRampLength, false);
RampDown.init(aTrackPtr, aBridgeStartPositionOnTrack + aRampLength + aRampPlatformLength, aBridgeHeight, aRampLength, true);
#if !defined(BRIDGE_NO_NEOPATTERNS)
TrackPtr = aTrackPtr;
isInitialized = true;
#endif
}
void startIdleAnimation(bool doRandom) {
#if !defined(BRIDGE_NO_NEOPATTERNS)
if (isInitialized) {
// Show the same animation on both ramps
long tRandom = random();
if (!doRandom) {
tRandom &= 0xFFFFFF00; // this forces pattern 0
}
RampUp.startIdleAnimation(tRandom);
RampDown.startIdleAnimation(tRandom);
}
#endif
}
/*
* Draw both ramps and dim brightness, if car is on ramp
*/
void draw() {
#if !defined(BRIDGE_NO_NEOPATTERNS)
if (isInitialized) {
RampUp.draw();
RampDown.draw();
}
#endif
}
};
Bridge bridges[NUMBER_OF_BRIDGES];
/********************************
* The LOOP class
********************************/
class Loop {
/*
* Requires 10 bytes + sizeof(NeoPatterns) RAM per loop
*
* Loop consists of (LoopLength) pixel
* Gravity - 100 is full gravity for vertical slope
*/
public:
NeoPatterns *TrackPtr;
#if !defined(LOOP_NO_NEOPATTERNS)
NeoPatterns *LoopPatterns;
bool isInitialized;
uint8_t RainbowIndex;
uint8_t RainbowIndexDividerCounter; // divides the call to RainbowIndex++
#endif
uint16_t StartPositionOnTrack; // Starting with 0
uint8_t LoopLength;
void init(NeoPatterns *aTrackPtr, uint16_t aStartPositionOnTrack, uint8_t aLength) {
StartPositionOnTrack = aStartPositionOnTrack;
LoopLength = aLength;
TrackPtr = aTrackPtr;
#if !defined(LOOP_NO_NEOPATTERNS)
/*
* NeoPatterns segments to control light effects on both ramps
* Call malloc() and free() before, since the compiler calls the constructor even when the result of malloc() is NULL, which leads to overwrite low memory.
*/
void *tMallocTest = malloc(sizeof(NeoPatterns)); // 67 + 2
if (tMallocTest != NULL) {
free(tMallocTest);
// Create a NeoPattern, which runs on a segment of the existing NeoPattern object.
LoopPatterns = new NeoPatterns(TrackPtr, StartPositionOnTrack, LoopLength, false);
isInitialized = true;
# if defined(__AVR__) && defined(DEBUG)
printRAMInfo(&Serial);
# endif
} else {
Serial.print(F("Not enough heap memory ("));
Serial.print(sizeof(NeoPatterns) + 2);
Serial.println(F(") for LoopPatterns."));
# if defined(__AVR__)
printRAMInfo(&Serial);
# endif
}
#else
(void) aTrackPtr;
#endif
}
/*
* Compute Gravity acceleration on the fly
* This saves the 200 bytes RAM of the old Acceleration map array
* @ return Values from -100 to +100 with 100 is gravity for a vertical slope, 0 for a horizontal and 70 (sqrt(0,5)*100) for 45 degree.
* Positive values increases speed, negative values decreases speed.
*/
int8_t getGravityAcceleration(uint16_t aPositionOnTrack) {
#if defined(DEBUG)
if (aPositionOnTrack < StartPositionOnTrack || aPositionOnTrack >= StartPositionOnTrack + LoopLength) {
Serial.print(F("Error! position="));
Serial.print(aPositionOnTrack);
Serial.print(F(" is not on loop "));
Serial.print(StartPositionOnTrack);
Serial.print(F(" to "));
Serial.println(StartPositionOnTrack + LoopLength - 1);
}
#endif
uint8_t tPositionOnRamp = aPositionOnTrack - StartPositionOnTrack;
return -((sin((TWO_PI / LoopLength) * tPositionOnRamp) + 0.005) * FULL_GRAVITY); // we start with deceleration for 1. half of loop
}
void startIdleAnimation(bool doRandom) {
#if !defined(LOOP_NO_NEOPATTERNS)
if (isInitialized) {
LongUnion tRandom;
tRandom.Long = random();
if (doRandom && (tRandom.UBytes[0] & 0x03)) {
if (tRandom.UBytes[0] & 0x01) {
//1 + 3
LoopPatterns->Stripes(NeoPatterns::Wheel(tRandom.UBytes[0]), (tRandom.UBytes[1] & 0x03) + 1, COLOR32_BLACK, 5,
(tRandom.UBytes[2] & 0x7F) + 64, (tRandom.UBytes[3] & 0x1F) + 4, DIRECTION_UP);
} else {
//2
initMultipleFallingStars(LoopPatterns, COLOR32_WHITE_HALF, 7, (tRandom.UBytes[0] & 0x07) + 1,
4 - (tRandom.UBytes[0] & 0x03), NULL, DIRECTION_UP);
}
} else {
// 0 or not random
LoopPatterns->ScannerExtendedD(COLOR32_BLUE_HALF, 8, START_ANIMATION_MILLIS, 2,
FLAG_SCANNER_EXT_ROCKET | FLAG_SCANNER_EXT_VANISH_COMPLETE | FLAG_SCANNER_EXT_START_AT_BOTH_ENDS, DIRECTION_UP);
}
}
#endif
}
/*
* Draw the loop in a fixed or changing color
*/
void draw(bool aDoRaceAnimation) {
bool tCarIsOnLoop = isAnyCarInRegion(StartPositionOnTrack, LoopLength);
color32_t tColor = LOOP_COLOR;
#if !defined(LOOP_NO_NEOPATTERNS)
if (isInitialized && aDoRaceAnimation) {
if (!LoopPatterns->update()) {
// do not increment RainbowIndex at each MILLISECONDS_PER_LOOP (20 ms) call
if (RainbowIndexDividerCounter++ >= 6) {
RainbowIndexDividerCounter = 0;
RainbowIndex++;
}
tColor = NeoPixel::Wheel(RainbowIndex);
}
}
#else
(void) aDoAnimation; // to avoid compiler warning
#endif
if (tCarIsOnLoop) {
tColor = TrackPtr->dimColorWithGamma5(tColor, 160);
}
TrackPtr->fillRegion(tColor, StartPositionOnTrack, LoopLength);
}
};
Loop loops[NUMBER_OF_LOOPS];
/*******************************************************************************************
* The CAR class
* Code related to each car is contained in this class
* Requires 23 bytes RAM per car
*******************************************************************************************/
/*
* Return values for computeNewSpeedAndDistance
*/
#define CAR_NOP 0
#define CAR_LAP_CONDITION 1
class Car {
public:
NeoPatterns *TrackPtr;
uint8_t NumberOfThisCar; // 1, 2...
uint8_t AcceleratorButtonPin;
#if defined(USE_ACCELERATION_NEOPIXEL_BARS)
uint8_t AccelerationBarPin;
uint16_t AcceleratorLowPassValue;
#endif
#if defined(ENABLE_ACCELERATOR_INPUT)
MPU6050IMUData AcceleratorInput;
bool AcceleratorInputConnected; // Dynamically detection of IMU accelerator connection; true -> accelerator is connected
bool ButtonInputDetected; // true -> button input was detected and has precedence of acceleration input
#endif // defined(ENABLE_ACCELERATOR_INPUT)
color32_t Color; // Car pixel color
const char *CarColorString; // Car color string
float SpeedAsPixelPerLoop; // Reasonable values are 0.5 to 2.0
float Distance; // Complete racing distance in pixel
uint16_t PixelPosition; // The index of head of car on the track
uint8_t Laps;
bool lastButtonState;
const char *WinnerMelody;
Car() { // @suppress("Class members should be properly initialized")
TrackPtr = NULL;
// CarIsActive = false;
}
void init(NeoPatterns *aTrackPtr, uint8_t aNumberOfThisCar, uint8_t aButtonPin, color32_t aCarColor,
const char *Car1ColorString, const char *aWinnerMelody) {
TrackPtr = aTrackPtr;
NumberOfThisCar = aNumberOfThisCar;
AcceleratorButtonPin = aButtonPin;
pinMode(AcceleratorButtonPin, INPUT_PULLUP);
Color = aCarColor;
CarColorString = Car1ColorString;
WinnerMelody = aWinnerMelody;
reset();
#if defined(ENABLE_ACCELERATOR_INPUT)
if (aNumberOfThisCar == 2) {
AcceleratorInput.setI2CAddress(MPU6050_ADDRESS_AD0_HIGH);
}
// use maximum filtering. It prefers slow and huge movements :-)
if (!AcceleratorInput.initMPU6050AndCalculateAllOffsetsAndWait(20, MPU6050_BAND_5_HZ)) {
AcceleratorInputConnected = false;
Serial.print(F("No MPU6050 IMU connected at address 0x"));
#if defined(USE_SOFT_I2C_MASTER)
Serial.print(AcceleratorInput.I2CAddress >> 1, HEX);
#else
Serial.print(AcceleratorInput.I2CAddress, HEX);
#endif
Serial.print(F(" for car "));
Serial.print(aNumberOfThisCar);
Serial.println(F(". You may want to disable \"#define ENABLE_ACCELERATOR_INPUT\""));
if (sSerialLCDAvailable) {
myLCD.setCursor(0, aNumberOfThisCar + 1);
myLCD.print(F("No IMU for car "));
myLCD.print(aNumberOfThisCar);
myLCD.print(' '); // to overwrite button info
}
playError();
} else {
AcceleratorInputConnected = true;
# if defined(INFO)
if (!sOnlyPlotterOutput) {
Serial.print(NumberOfThisCar);
Serial.print(' ');
AcceleratorInput.printAllOffsets(&Serial);
}
# endif
}
#endif // defined(ENABLE_ACCELERATOR_INPUT)
}
void reset() {
SpeedAsPixelPerLoop = 0;
Distance = 0;
Laps = 0;
PixelPosition = 0;
lastButtonState = 1;
#if defined(ENABLE_ACCELERATOR_INPUT)
ButtonInputDetected = false;
#endif
#if defined(USE_ACCELERATION_NEOPIXEL_BARS)
AccelerationCommonNeopixelBar.setPin(AccelerationBarPin);
AccelerationCommonNeopixelBar.clear();
AccelerationCommonNeopixelBar.show();
#endif
}
/*
* The car consists of number of laps pixels
* Overlapping of cars is handled by using addPixelColor for drawing
*/
void draw() {
#if defined(TEST_MODE)
for (int i = 0; i <= 1; i++) {
#else
for (int i = 0; i <= Laps; i++) {
#endif
// draw from back to front
int16_t tDrawPosition = PixelPosition - i;
if (tDrawPosition < 0) {
// wrap around
tDrawPosition += TrackPtr->numPixels();
}
TrackPtr->addPixelColor(tDrawPosition, getRedPart(Color), getGreenPart(Color), getBluePart(Color));
}
}
/*
* @return true if button was pressed or accelerator was moved
*/
bool checkInput() {
#if !defined(ENABLE_ACCELERATOR_INPUT)
return checkButton();
#else
bool tButtonIsPressed = checkButton(); // checkButton() also sets ButtonInputDetected used below
if (tButtonIsPressed
|| (!ButtonInputDetected && AcceleratorInputConnected && (getAcceleratorValueShift8() >= ACCELERATOR_TRIGGER_VALUE))) {
return true;
}
#endif
return false;
}
#if defined(ENABLE_ACCELERATOR_INPUT)
/*
* @return 4 g for 16 bit full range
*/
uint8_t getAcceleratorValueShift8() {
AcceleratorInput.readDataFromMPU6050();
// AcceleratorInput.readDataFromMPU6050Fifo();
uint8_t tAcceleration = AcceleratorInput.computeAccelerationWithFloatingOffset() >> 9; // 8 is too easy, 10 is a bit hard
# if defined(TRACE)
//#if defined(INFO)
Serial.print(NumberOfThisCar);
Serial.print(F(" Acc="));
Serial.println(tAcceleration);
# endif
# if defined(USE_ACCELERATION_NEOPIXEL_BARS)
// AccelerationBar->setBarAndPeakAndShow(tAcceleration);
# endif
return tAcceleration;
}
#endif // defined(ENABLE_ACCELERATOR_INPUT)
/*
* Check if button was just pressed
* activates car and returns true if button was just pressed
*/
bool checkButton() {
bool tLastButtonState = lastButtonState;
lastButtonState = digitalRead(AcceleratorButtonPin);
if (tLastButtonState == true && lastButtonState == false) {
#if defined(DEBUG)
Serial.print(NumberOfThisCar);
Serial.println(F(" Button pressed"));
#endif
#if defined(ENABLE_ACCELERATOR_INPUT)
ButtonInputDetected = true;
#endif
return true;
}
return false;
}
/*
* @return true if this car is the winner
*/
bool checkIfWinner(uint8_t aLapsNeededToWin) {
if (Laps >= aLapsNeededToWin) {
return true;
}
return false;
}
/*
* Play a winner melody and run animations with the car color on the track
* Returns if melody ends
* You can stop melody and animation by pressing the car button.
* @return true if stopped by user input
*/
bool doWinnerAnimationAndSound() {
startPlayRtttlPGM(PIN_BUZZER, WinnerMelody);
TrackPtr->Stripes(Color, 2, COLOR32_BLACK, 8, 300, 50, DIRECTION_UP);
bool tReturnValue = false;
auto tStartMillis = millis();
while (updatePlayRtttl()) {
#if defined(TIMING_TEST)
/*
* 20 microseconds for loop, 300 microseconds if melody updated
* 19 ms duration for resetAndDrawTrack() - every 50 ms
*/
digitalWrite(PIN_TIMING, HIGH);
#endif
if (TrackPtr->update()) {
timer0_millis += MILLIS_FOR_TRACK_TO_SHOW;
// restore bridge and loop pattern, which might be overwritten by scanner.
resetAndDrawTrack(false);
}
#if defined(TIMING_TEST)
digitalWrite(PIN_TIMING, LOW);
#endif
if ((millis() - tStartMillis > WINNER_MINIMAL_ANIMATION_DURATION_MILLIS) && checkAllInputs()) {
// minimal animation time was reached and input was activated
stopPlayRtttl(); // to stop in a deterministic fashion
tReturnValue = true;
}
yield();
}
TrackPtr->stop();
return tReturnValue;
}
void print(float aGravity, float aFricion, float aDrag) {
Serial.print(NumberOfThisCar);
Serial.print(F(" Speed="));
Serial.print(SpeedAsPixelPerLoop, 2);
Serial.print(F(" - Gravity="));
Serial.print(aGravity, 6);
Serial.print(F(" Fricion="));
Serial.print(aFricion, 6);
Serial.print(F(" Drag="));
Serial.println(aDrag, 6);
}
/*
* Check buttons and gravity and get new speed
*/
uint8_t computeNewSpeedAndDistance() {
uint8_t tRetval = CAR_NOP;
#if defined(TIMING_TEST)
digitalWrite(PIN_TIMING, HIGH);
#endif
uint16_t tAcceleration;
if (checkButton()) {
// add fixed amount of energy -> 800 => tAdditionalEnergy is 0.2 per press
tAcceleration = 600;
}
#if defined(ENABLE_ACCELERATOR_INPUT)
else if (!ButtonInputDetected && AcceleratorInputConnected) {
//Here, no button was pressed before and accelerator is connected
tAcceleration = getAcceleratorValueShift8();
} else {
tAcceleration = 0;
}
#endif
#if defined(USE_ACCELERATION_NEOPIXEL_BARS)
AcceleratorLowPassValue += (((int16_t) (tAcceleration - AcceleratorLowPassValue))) >> 3;
// scale it so that 100 -> 8
// Parameter NumberOfThisCar == 1 has the effect, that bar for car 2 can be mounted upside down
AccelerationCommonNeopixelBar.drawBar(AcceleratorLowPassValue / (100 / 8), Color, NumberOfThisCar == 1);
AccelerationCommonNeopixelBar.setPin(AccelerationBarPin);
AccelerationCommonNeopixelBar.show();
#endif
if (tAcceleration > 0) {
float tAdditionalEnergy = ((float) tAcceleration) / 4096;
SpeedAsPixelPerLoop = sqrt((SpeedAsPixelPerLoop * SpeedAsPixelPerLoop) + tAdditionalEnergy);
}
if (sOnlyPlotterOutput) {
/*
* Print data for Arduino plotter
*/
Serial.print(tAcceleration);
Serial.print(' ');
Serial.print(AcceleratorLowPassValue);
Serial.print(' ');
Serial.print(int(SpeedAsPixelPerLoop * 100));
Serial.print(' ');
}
bool tIsAnalogParameterInputMode = !digitalRead(PIN_MANUAL_PARAMETER_MODE);
float tGravity;
float tFricion;
float tDrag;
if (tIsAnalogParameterInputMode) {
/*
* Read analog values for gravity etc. from potentiometers
* 850 us for analogRead + DEBUG print
*/
uint16_t tGravityRaw = analogRead(PIN_GRAVITY);
uint16_t tFricionRaw = analogRead(PIN_FRICTION);
uint16_t tDragRaw = analogRead(PIN_DRAG);
if (tGravityRaw >= ANALOG_OFFSET) {
// -ANALOG_OFFSET (20) to get real 0 value even if ground has bias because of high LED current on breadboard
tGravityRaw -= ANALOG_OFFSET;
}
if (tFricionRaw >= ANALOG_OFFSET) {
tFricionRaw -= ANALOG_OFFSET;
}
if (tDragRaw >= ANALOG_OFFSET) {
tDragRaw -= ANALOG_OFFSET;
}
tGravity = tGravityRaw * (GRAVITY_FACTOR_FOR_MAP / 512);
tFricion = tFricionRaw * (FRICTION_PER_LOOP / 512);
tDrag = tDragRaw * (AERODYNAMIC_DRAG_PER_LOOP / 512);
#if defined(DEBUG)
if ((((sLoopCountForDebugPrint & 0x3F) == 0) || checkInput()) && NumberOfThisCar == 1) {
#if defined(TRACE)
Serial.print(F("GRaw="));
Serial.print(tGravityRaw);
Serial.print(F(" FRaw="));
Serial.print(tFricionRaw);
Serial.print(F(" DRaw="));
Serial.println(tDragRaw);
#endif
print(tGravity, tFricion, tDrag);
}
#endif
} else {
// 100 microseconds
tGravity = GRAVITY_FACTOR_FOR_MAP;
tFricion = FRICTION_PER_LOOP;
tDrag = AERODYNAMIC_DRAG_PER_LOOP;
}
/*
* Compute new position
*/
Distance += SpeedAsPixelPerLoop; // Take speed to compute new position
if (Distance < 0.0) {
Distance = 0.0;
}
PixelPosition = (uint16_t) Distance % TrackPtr->numPixels();
/*
* Check for lap counter
*/
if (Distance > TrackPtr->numPixels() * (Laps + 1)) {
Laps++;
#if defined(INFO)
if (!sOnlyPlotterOutput) {
Serial.print(F("Car "));
Serial.print(NumberOfThisCar);
Serial.print(F(" starts "));
Serial.print(Laps + 1);
Serial.println(F(". lap"));
}
#endif
if (sSerialLCDAvailable) {
BigNumbers.writeAt(Laps, ((NumberOfThisCar - 1) * 13) + 2, 0); // red is left, green is right
}
tRetval = CAR_LAP_CONDITION;
}
/*
* Compute new speed:
* - Acceleration from map and friction are simply subtracted from speed
* - Aerodynamic drag is subtracted proportional from speed
*/
int8_t tGravityAcceleration = 0;
if (PixelPosition >= bridges[0].RampUp.StartPositionOnTrack
&& PixelPosition <= (bridges[0].RampUp.StartPositionOnTrack + bridges[0].RampUp.RampLength)) {
tGravityAcceleration = bridges[0].RampUp.getGravityAcceleration(PixelPosition);
} else if (PixelPosition >= bridges[0].RampDown.StartPositionOnTrack
&& PixelPosition <= (bridges[0].RampDown.StartPositionOnTrack + bridges[0].RampDown.RampLength)) {
tGravityAcceleration = bridges[0].RampDown.getGravityAcceleration(PixelPosition);
} else if (PixelPosition >= loops[0].StartPositionOnTrack
&& PixelPosition < (loops[0].StartPositionOnTrack + loops[0].LoopLength)) {
tGravityAcceleration = loops[0].getGravityAcceleration(PixelPosition);
}
if (tGravityAcceleration != 0) {
/*
* Here we are on a ramp or loop
*/
SpeedAsPixelPerLoop += tGravity * tGravityAcceleration;
#if defined(DEBUG)
Serial.print(F(" GravityAcceleration="));
Serial.print(tGravityAcceleration);
#endif
}