forked from SkyHorseTech/F9P_RAWX_Logger_OLED
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRAWX_Logger_F9P_I2C_OLED.ino
2250 lines (2039 loc) · 86.5 KB
/
RAWX_Logger_F9P_I2C_OLED.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
// RAWX_Logger_F9P_I2C
// Logs RXM-RAWX, RXM-SFRBX and TIM-TM2 data from u-blox ZED_F9P GNSS to SD card
// Also logs NAV_PVT messages (which provide the carrSoln status) and NAV-STATUS messages (which indicate a time fix for Survey_In mode)
// Also logs high precision NMEA GNGGA position solution messages which can be extracted by RTKLIB
// GNSS data is provided by the SparkFun GPS-RTK2 Board or the ZED-F9P FeatherWing
// https://www.sparkfun.com/products/15136
// https://github.com/PaulZC/ZED-F9P_FeatherWing_USB
// This version uses **version 1.6** the SparkFun u-blox library by Nathan Seidle to configure the RAWX messages via I2C,
// leaving the UART dedicated for the messages to be logged to SD card
// Feel like supporting open source hardware? Buy a board from SparkFun!
// ZED-F9P RTK2: https://www.sparkfun.com/products/15136
// This code is written for the Adalogger M0 Feather
// https://www.adafruit.com/products/2796
// https://learn.adafruit.com/adafruit-feather-m0-adalogger
// Adafruit invests time and resources providing this open source design, please support Adafruit and open-source hardware by purchasing products from Adafruit!
// Choose a good quality SD card. Some cheap cards can't handle the write rate.
// Ensure the card is formatted as FAT32.
// Changes to a new log file every INTERVAL minutes
// Define how long we should log in minutes before changing to a new file
// Sensible values are: 5, 10, 15, 20, 30, 60
// Must be <= 60 (or RTC alarm code needs to be updated to match on HHMMSS)
const int INTERVAL = 60;
// Define how long we should wait in msec (approx.) for residual RAWX data before closing the last log file
// For a measurement rate of 4Hz (250msec), 300msec is a sensible value. i.e. slightly more than one measurement interval
const int dwell = 300;
// Send serial debug messages
//#define DEBUG // Comment this line out to disable debug messages
//#define DEBUGi2c // Comment this line out to disable I2C debug messages
// Debug SerialBuffer
// Displays a "Max bufAvail:" message each time SerialBuffer.available reaches a new maximum
//#define DEBUGserialBuffer // Comment this to disable serial buffer maximum available debugging
// Connect modePin to GND to select base mode. Leave open for rover mode.
#define modePin 14 // A0 / Digital Pin 14
// Connect a normally-open push-to-close switch between swPin and GND.
// Press it to stop logging and close the log file.
#define swPin 15 // A1 / Digital Pin 15 (0.2" away from the GND pin on the Adalogger)
// Pin A2 (Digital Pin 16) is reserved for the ZED-F9P EXTINT signal
// The code uses this an an interrupt to set the NeoPixel to white
#define ExtIntPin 16 // A2 / Digital Pin 16
#define white_flash 1000 // Flash the NeoPxel white for this many milliseconds on every ExtInt
// Connect A3 (Digital Pin 17) to GND to select SURVEY_IN mode when in BASE mode
#define SurveyInPin 17 // A3 / Digital Pin 17
// Include the SparkFun u-blox Library
#include <Wire.h> //Needed for I2C to GPS
#include <SparkFun_Ublox_Arduino_Library.h> //http://librarymanager/All#SparkFun_Ublox_GPS
SFE_UBLOX_GPS i2cGPS;
// LEDs
//#define NoLED // Uncomment this line to completely disable the LEDs
//#define NoLogLED // Uncomment this line to disable the LEDs during logging only
// NeoPixel Settings
//#define NeoPixel // Uncomment this line to enable a NeoPixel on the same pin as RedLED
// The red LED flashes during SD card writes
#define RedLED 13 // The red LED on the Adalogger is connected to Digital Pin 13
// The green LED indicates that the GNSS has established a fix
#define GreenLED 8 // The green LED on the Adalogger is connected to Digital Pin 8
// Include the Adafruit NeoPixel Library
#ifdef NeoPixel
#include <Adafruit_NeoPixel.h> // Support for the WB2812B
#define swap_red_green // Uncomment this line if your WB2812B has red and green reversed
#ifdef swap_red_green
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(1, RedLED, NEO_GRB + NEO_KHZ800); // GRB WB2812B
#else
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(1, RedLED, NEO_RGB + NEO_KHZ800); // RGB WB2812B
#endif
#define LED_Brightness 40 // 0 - 255 for WB2812B
#endif
// Begin custom Sky Horse
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define OLED_RESET 4 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
//End custom Sky Horse
// Fast SD card logging using Bill Greiman's SdFat
// https://github.com/greiman/SdFat
// From FatFile.h:
// * \note Data is moved to the cache but may not be written to the
// * storage device until sync() is called.
#include <SPI.h>
#include <SdFat.h>
const uint8_t cardSelect = 4; // Adalogger uses D4 as the SD SPI select
SdFat sd;
SdFile rawx_dataFile;
// The log filename starts with "r_" for the rover and "b_" for the static base
bool base_mode = true; // Flag to indicate if the code is in base or rover mode
char rawx_filename[] = "20000000/b_000000.ubx"; // the b will be replaced by an r if required
char dirname[] = "20000000";
long bytes_written = 0;
bool survey_in_mode = false; // Flag to indicate if the code is in survey_in mode
// Timer to indicate if an ExtInt has been received
volatile unsigned long ExtIntTimer; // Load this with millis plus white_flash to show when the ExtInt LED should be switched off
// Define packet size, buffer and buffer pointer for SD card writes
const size_t SDpacket = 512;
uint8_t serBuffer[SDpacket];
size_t bufferPointer = 0;
int numBytes;
// Battery voltage
float vbat;
#define LOWBAT 3.55 // Low battery voltage
// Include Real Time Clock support for the M0
// https://github.com/arduino-libraries/RTCZero
#include <RTCZero.h> // M0 Real Time Clock
RTCZero rtc; // Create an rtc object
volatile bool alarmFlag = false; // RTC alarm (interrupt) flag
// Count number of valid fixes before starting to log
#define maxvalfix 10 // Collect at least this many valid fixes before logging starts
int valfix = 0;
bool stop_pressed = false; // Flag to indicate if stop switch was pressed to stop logging
// Define SerialBuffer as a large RingBuffer which we will use to store the Serial1 receive data
// Actual Serial1 receive data will be copied into SerialBuffer by a timer interrupt
// https://gist.github.com/jdneo/43be30d85080b175cb5aed3500d3f989
// That way, we do not need to increase the size of the Serial1 receive buffer (by editing RingBuffer.h)
// You can use DEBUGserialBuffer to determine how big the buffer should be. Increase it if you see bufAvail get close to or reach the buffer size.
RingBufferN<16384> SerialBuffer; // Define SerialBuffer as a RingBuffer of size 16k bytes
// Loop Steps
#define init 0
#define start_rawx 1
#define open_file 2
#define write_file 3
#define new_file 4
#define close_file 5
#define restart_file 6
int loop_step = init;
// UBX and NMEA Parse State
#define looking_for_B5_dollar 0
#define looking_for_62 1
#define looking_for_class 2
#define looking_for_ID 3
#define looking_for_length_LSB 4
#define looking_for_length_MSB 5
#define processing_payload 6
#define looking_for_checksum_A 7
#define looking_for_checksum_B 8
#define sync_lost 9
#define looking_for_asterix 10
#define looking_for_csum1 11
#define looking_for_csum2 12
#define looking_for_term1 13
#define looking_for_term2 14
int ubx_nmea_state = looking_for_B5_dollar;
int ubx_length = 0;
int ubx_class = 0;
int ubx_ID = 0;
int ubx_checksum_A = 0;
int ubx_checksum_B = 0;
int ubx_expected_checksum_A = 0;
int ubx_expected_checksum_B = 0;
int nmea_char_1 = '0'; // e.g. G
int nmea_char_2 = '0'; // e.g. P
int nmea_char_3 = '0'; // e.g. G
int nmea_char_4 = '0'; // e.g. G
int nmea_char_5 = '0'; // e.g. A
int nmea_csum = 0;
int nmea_csum1 = '0';
int nmea_csum2 = '0';
int nmea_expected_csum1 = '0';
int nmea_expected_csum2 = '0';
#define max_nmea_len 100 // Maximum length for an NMEA message: use this to detect if we have lost sync while receiving an NMEA message
// Definitions for u-blox F9P UBX-format (binary) messages
// Disable NMEA output on the I2C port
// UBX-CFG-VALSET message with a key ID of 0x10720002 (CFG-I2COUTPROT-NMEA) and a value of 0
uint8_t disableI2cNMEA() {
return i2cGPS.setVal8(0x10720002, 0x00, VAL_LAYER_RAM);
}
// Set UART1 to 230400 Baud
// UBX-CFG-VALSET message with a key ID of 0x40520001 (CFG-UART1-BAUDRATE) and a value of 0x00038400 (230400 decimal)
uint8_t setUART1BAUD() {
return i2cGPS.setVal32(0x40520001, 0x00038400, VAL_LAYER_RAM);
}
// setRAWXoff: this is the message which disables all of the messages being logged to SD card
// It also clears the NMEA high precision mode for the GPGGA message
// It also sets the main talker ID to 'GP'
// UBX-CFG-VALSET message with key IDs of:
// 0x209102a5 (CFG-MSGOUT-UBX_RXM_RAWX_UART1)
// 0x20910232 (CFG-MSGOUT-UBX_RXM_SFRBX_UART1)
// 0x20910179 (CFG-MSGOUT-UBX_TIM_TM2_UART1)
// 0x2091002a (CFG-MSGOUT-UBX_NAV_POSLLH_UART1)
// 0x20910007 (CFG-MSGOUT-UBX_NAV_PVT_UART1)
// 0x2091001b (CFG-MSGOUT-UBX_NAV_STATUS_UART1)
// 0x10930006 (CFG-NMEA-HIGHPREC)
// 0x209100bb (CFG-MSGOUT-NMEA_ID_GGA_UART1)
// and values (rates) of zero
// 0x20930031 (CFG-NMEA-MAINTALKERID) has value 1 (GP)
uint8_t setRAWXoff() {
i2cGPS.newCfgValset8(0x209102a5, 0x00, VAL_LAYER_RAM); // CFG-MSGOUT-UBX_RXM_RAWX_UART1
i2cGPS.addCfgValset8(0x20910232, 0x00); // CFG-MSGOUT-UBX_RXM_SFRBX_UART1
i2cGPS.addCfgValset8(0x20910179, 0x00); // CFG-MSGOUT-UBX_TIM_TM2_UART1
i2cGPS.addCfgValset8(0x2091002a, 0x00); // CFG-MSGOUT-UBX_NAV_POSLLH_UART1
i2cGPS.addCfgValset8(0x20910007, 0x00); // CFG-MSGOUT-UBX_NAV_PVT_UART1
i2cGPS.addCfgValset8(0x2091001b, 0x00); // CFG-MSGOUT-UBX_NAV_STATUS_UART1
i2cGPS.addCfgValset8(0x20930031, 0x01); // CFG-NMEA-MAINTALKERID : This line sets the main talker ID to GP
i2cGPS.addCfgValset8(0x10930006, 0x00); // CFG-NMEA-HIGHPREC : This line disables NMEA high precision mode
return i2cGPS.sendCfgValset8(0x209100bb, 0x00); // CFG-MSGOUT-NMEA_ID_GGA_UART1 : This line disables the GGA message
}
// setRAWXon: this is the message which enables all of the messages to be logged to SD card in one go
// It also sets the NMEA high precision mode for the GNGGA message
// It also sets the main talker ID to 'GN'
// UBX-CFG-VALSET message with key IDs of:
// 0x209102a5 (CFG-MSGOUT-UBX_RXM_RAWX_UART1)
// 0x20910232 (CFG-MSGOUT-UBX_RXM_SFRBX_UART1)
// 0x20910179 (CFG-MSGOUT-UBX_TIM_TM2_UART1)
// 0x2091002a (CFG-MSGOUT-UBX_NAV_POSLLH_UART1)
// 0x20910007 (CFG-MSGOUT-UBX_NAV_PVT_UART1)
// 0x2091001b (CFG-MSGOUT-UBX_NAV_STATUS_UART1)
// 0x10930006 (CFG-NMEA-HIGHPREC)
// 0x209100bb (CFG-MSGOUT-NMEA_ID_GGA_UART1)
// and values (rates) of 1
// 0x20930031 (CFG-NMEA-MAINTALKERID) has value 3 (GN)
uint8_t setRAWXon() {
i2cGPS.newCfgValset8(0x209102a5, 0x01, VAL_LAYER_RAM);
i2cGPS.addCfgValset8(0x20910232, 0x01);
i2cGPS.addCfgValset8(0x20910179, 0x01);
i2cGPS.addCfgValset8(0x2091002a, 0x00); // Change the last byte from 0x00 to 0x01 to enable NAV_POSLLH
i2cGPS.addCfgValset8(0x20910007, 0x01); // Change the last byte from 0x01 to 0x00 to leave NAV_PVT disabled
i2cGPS.addCfgValset8(0x2091001b, 0x01); // This line enables the NAV_STATUS message
i2cGPS.addCfgValset8(0x20930031, 0x03); // This line sets the main talker ID to GN
i2cGPS.addCfgValset8(0x10930006, 0x01); // This sets the NMEA high precision mode
return i2cGPS.sendCfgValset8(0x209100bb, 0x01); // This (re)enables the GGA mesage
}
// Enable the NMEA GGA and RMC messages on UART1
// UBX-CFG-VALSET message with key IDs of:
// 0x209100ca (CFG-MSGOUT-NMEA_ID_GLL_UART1)
// 0x209100c0 (CFG-MSGOUT-NMEA_ID_GSA_UART1)
// 0x209100c5 (CFG-MSGOUT-NMEA_ID_GSV_UART1)
// 0x209100b1 (CFG-MSGOUT-NMEA_ID_VTG_UART1)
// 0x20920007 (CFG-INFMSG-NMEA_UART1)
// 0x209100bb (CFG-MSGOUT-NMEA_ID_GGA_UART1)
// 0x209100ac (CFG-MSGOUT-NMEA_ID_RMC_UART1)
uint8_t setNMEAon() {
i2cGPS.newCfgValset8(0x209100ca, 0x00, VAL_LAYER_RAM);
i2cGPS.addCfgValset8(0x209100c0, 0x00);
i2cGPS.addCfgValset8(0x209100c5, 0x00);
i2cGPS.addCfgValset8(0x209100b1, 0x00);
i2cGPS.addCfgValset8(0x20920007, 0x00);
i2cGPS.addCfgValset8(0x209100bb, 0x01);
return i2cGPS.sendCfgValset8(0x209100ac, 0x01);
}
// Disable the NMEA messages
// UBX-CFG-VALSET message with key IDs of:
// 0x209100ca (CFG-MSGOUT-NMEA_ID_GLL_UART1)
// 0x209100c0 (CFG-MSGOUT-NMEA_ID_GSA_UART1)
// 0x209100c5 (CFG-MSGOUT-NMEA_ID_GSV_UART1)
// 0x209100b1 (CFG-MSGOUT-NMEA_ID_VTG_UART1)
// 0x20920007 (CFG-INFMSG-NMEA_UART1)
// 0x209100bb (CFG-MSGOUT-NMEA_ID_GGA_UART1)
// 0x209100ac (CFG-MSGOUT-NMEA_ID_RMC_UART1)
uint8_t setNMEAoff() {
i2cGPS.newCfgValset8(0x209100ca, 0x00, VAL_LAYER_RAM);
i2cGPS.addCfgValset8(0x209100c0, 0x00);
i2cGPS.addCfgValset8(0x209100c5, 0x00);
i2cGPS.addCfgValset8(0x209100b1, 0x00);
i2cGPS.addCfgValset8(0x20920007, 0x00);
i2cGPS.addCfgValset8(0x209100bb, 0x00);
return i2cGPS.sendCfgValset8(0x209100ac, 0x00);
}
// Set the Main NMEA Talker ID to "GP"
// UBX-CFG-VALSET message with a key ID of 0x20930031 (CFG-NMEA-MAINTALKERID) and a value of 1 (GP):
uint8_t setTALKERID() {
return i2cGPS.setVal8(0x20930031, 0x01, VAL_LAYER_RAM);
}
// Set the measurement rate
// UBX-CFG-VALSET message with a key ID of 0x30210001 (CFG-RATE-MEAS)
uint8_t setRATE_20Hz() { return i2cGPS.setVal16(0x30210001, 0x0032, VAL_LAYER_RAM); }
uint8_t setRATE_10Hz() { return i2cGPS.setVal16(0x30210001, 0x0064, VAL_LAYER_RAM); }
uint8_t setRATE_5Hz() { return i2cGPS.setVal16(0x30210001, 0x00c8, VAL_LAYER_RAM); }
uint8_t setRATE_4Hz() { return i2cGPS.setVal16(0x30210001, 0x00fa, VAL_LAYER_RAM); }
uint8_t setRATE_2Hz() { return i2cGPS.setVal16(0x30210001, 0x01f4, VAL_LAYER_RAM); }
uint8_t setRATE_1Hz() { return i2cGPS.setVal16(0x30210001, 0x03e8, VAL_LAYER_RAM); }
// Set the navigation dynamic model
// UBX-CFG-VALSET message with a key ID of 0x20110021 (CFG-NAVSPG-DYNMODEL)
uint8_t setNAVportable() { return i2cGPS.setVal8(0x20110021, 0x00, VAL_LAYER_RAM); };
uint8_t setNAVstationary() { return i2cGPS.setVal8(0x20110021, 0x02, VAL_LAYER_RAM); };
uint8_t setNAVpedestrian() { return i2cGPS.setVal8(0x20110021, 0x03, VAL_LAYER_RAM); };
uint8_t setNAVautomotive() { return i2cGPS.setVal8(0x20110021, 0x04, VAL_LAYER_RAM); };
uint8_t setNAVsea() { return i2cGPS.setVal8(0x20110021, 0x05, VAL_LAYER_RAM); };
uint8_t setNAVair1g() { return i2cGPS.setVal8(0x20110021, 0x06, VAL_LAYER_RAM); };
uint8_t setNAVair2g() { return i2cGPS.setVal8(0x20110021, 0x07, VAL_LAYER_RAM); };
uint8_t setNAVair4g() { return i2cGPS.setVal8(0x20110021, 0x08, VAL_LAYER_RAM); };
uint8_t setNAVwrist() { return i2cGPS.setVal8(0x20110021, 0x09, VAL_LAYER_RAM); };
// Set UART2 to 230400 Baud
// UBX-CFG-VALSET message with a key ID of 0x40530001 (CFG-UART2-BAUDRATE) and a value of 0x00038400 (230400 decimal)
uint8_t setUART2BAUD_230400() {
return i2cGPS.setVal32(0x40530001, 0x00038400, VAL_LAYER_RAM);
}
// Set UART2 to 115200 Baud
// UBX-CFG-VALSET message with a key ID of 0x40530001 (CFG-UART2-BAUDRATE) and a value of 0x0001c200 (115200 decimal)
uint8_t setUART2BAUD_115200() {
return i2cGPS.setVal32(0x40530001, 0x0001c200, VAL_LAYER_RAM);
}
// Set Survey_In mode
// UBX-CFG-VALSET message with a key IDs and values of:
// 0x20030001 (CFG-TMODE-MODE) and a value of 1
// 0x40030011 (CFG-TMODE-SVIN_ACC_LIMIT) and a value of 0x0000c350 (50000 decimal = 5 m)
// 0x40030010 (CFG-TMODE-SVIN_MIN_DUR) and a value of 0x0000003c (60 decimal = 1 min)
uint8_t setSurveyIn() {
i2cGPS.newCfgValset8(0x20030001, 0x01, VAL_LAYER_RAM);
i2cGPS.addCfgValset32(0x40030011, 0x0000c350);
return i2cGPS.sendCfgValset32(0x40030010, 0x0000003c);
}
// Disable Survey_In mode
// UBX-CFG-VALSET message with a key ID of 0x20030001 (CFG-TMODE-MODE) and a value of 0
uint8_t disableSurveyIn() {
return i2cGPS.setVal8(0x20030001, 0x00, VAL_LAYER_RAM);
}
// Enable RTCM message output on UART2
// UBX-CFG-VALSET message with the following key IDs
// Set the value byte to 0x01 to send an RTCM message at RATE_MEAS; set the value to 0x04 to send an RTCM message at 1/4 RATE_MEAS
// (i.e. assumes you will be logging RAWX data at 4 Hz. Adjust accordingly)
// 0x209102bf (CFG-MSGOUT-RTCM_3X_TYPE1005_UART2)
// 0x209102ce (CFG-MSGOUT-RTCM_3X_TYPE1077_UART2)
// 0x209102d3 (CFG-MSGOUT-RTCM_3X_TYPE1087_UART2)
// 0x209102d8 (CFG-MSGOUT-RTCM_3X_TYPE1127_UART2)
// 0x2091031a (CFG-MSGOUT-RTCM_3X_TYPE1097_UART2)
// 0x20910305 (CFG-MSGOUT-RTCM_3X_TYPE1230_UART2)
uint8_t setRTCMon() {
i2cGPS.newCfgValset8(0x209102bf, 0x04, VAL_LAYER_RAM);
i2cGPS.addCfgValset8(0x209102ce, 0x04);
i2cGPS.addCfgValset8(0x209102d3, 0x04);
i2cGPS.addCfgValset8(0x209102d8, 0x04);
i2cGPS.addCfgValset8(0x2091031a, 0x04);
return i2cGPS.sendCfgValset8(0x20910305, 0x28);
}
// Disable RTCM message output on UART2
// UBX-CFG-VALSET message with the following key IDs and values of 0:
// 0x209102bf (CFG-MSGOUT-RTCM_3X_TYPE1005_UART2)
// 0x209102ce (CFG-MSGOUT-RTCM_3X_TYPE1077_UART2)
// 0x209102d3 (CFG-MSGOUT-RTCM_3X_TYPE1087_UART2)
// 0x209102d8 (CFG-MSGOUT-RTCM_3X_TYPE1127_UART2)
// 0x2091031a (CFG-MSGOUT-RTCM_3X_TYPE1097_UART2)
// 0x20910305 (CFG-MSGOUT-RTCM_3X_TYPE1230_UART2)
uint8_t setRTCMoff() {
i2cGPS.newCfgValset8(0x209102bf, 0x00, VAL_LAYER_RAM);
i2cGPS.addCfgValset8(0x209102ce, 0x00);
i2cGPS.addCfgValset8(0x209102d3, 0x00);
i2cGPS.addCfgValset8(0x209102d8, 0x00);
i2cGPS.addCfgValset8(0x2091031a, 0x00);
return i2cGPS.sendCfgValset8(0x20910305, 0x00);
}
// Set TimeGrid for TP1 to GPS (instead of UTC) so TIM_TM2 messages are aligned with GPS time
// UBX-CFG-VALSET message with the key ID 0x2005000c (CFG-TP-TIMEGRID_TP1) and value of 1 (GPS):
uint8_t setTimeGrid() {
return i2cGPS.setVal8(0x2005000c, 0x01, VAL_LAYER_RAM);
}
// Enable NMEA messages on UART2 for test purposes
// UBX-CFG-VALSET message with key ID of 0x10760002 (CFG-UART2OUTPROT-NMEA) and value of 1:
uint8_t setUART2nmea() {
return i2cGPS.setVal8(0x10760002, 0x01, VAL_LAYER_RAM);
}
// 'Disable' timepulse TP1 by setting LEN_LOCK_TP1 to zero
// (This doesn't actually disable the timepulse, it just sets its length to zero!)
// UBX-CFG-VALSET message with key ID of 0x40050005 (CFG-TP-LEN_LOCK_TP1) and value of 0:
uint8_t disableTP1() {
return i2cGPS.setVal32(0x40050005, 0, VAL_LAYER_RAM);
}
// ExtInt interrupt service routine
void ExtInt() {
ExtIntTimer = millis() + white_flash; // Set the timer value to white_flash milliseconds from now
}
// RTC alarm interrupt
// Must be kept as short as possible. Update the alarm time in the main loop, not here.
void alarmMatch()
{
alarmFlag = true; // Set alarm flag
}
// TimerCounter3 functions to copy Serial1 receive data into SerialBuffer
// https://gist.github.com/jdneo/43be30d85080b175cb5aed3500d3f989
#define CPU_HZ 48000000
#define TIMER_PRESCALER_DIV 16
// Set TC3 Interval (sec)
void setTimerInterval(float intervalS) {
int compareValue = intervalS * CPU_HZ / TIMER_PRESCALER_DIV;
if (compareValue > 65535) compareValue = 65535;
TcCount16* TC = (TcCount16*) TC3;
// Make sure the count is in a proportional position to where it was
// to prevent any jitter or disconnect when changing the compare value.
TC->COUNT.reg = map(TC->COUNT.reg, 0, TC->CC[0].reg, 0, compareValue);
TC->CC[0].reg = compareValue;
while (TC->STATUS.bit.SYNCBUSY == 1);
}
// Start TC3 with a specified interval
void startTimerInterval(float intervalS) {
REG_GCLK_CLKCTRL = (uint16_t) (GCLK_CLKCTRL_CLKEN | GCLK_CLKCTRL_GEN_GCLK0 | GCLK_CLKCTRL_ID_TCC2_TC3) ;
while ( GCLK->STATUS.bit.SYNCBUSY == 1 ); // wait for sync
TcCount16* TC = (TcCount16*) TC3;
TC->CTRLA.reg &= ~TC_CTRLA_ENABLE;
while (TC->STATUS.bit.SYNCBUSY == 1); // wait for sync
// Use the 16-bit timer
TC->CTRLA.reg |= TC_CTRLA_MODE_COUNT16;
while (TC->STATUS.bit.SYNCBUSY == 1); // wait for sync
// Use match mode so that the timer counter resets when the count matches the compare register
TC->CTRLA.reg |= TC_CTRLA_WAVEGEN_MFRQ;
while (TC->STATUS.bit.SYNCBUSY == 1); // wait for sync
// Set prescaler to 16
TC->CTRLA.reg |= TC_CTRLA_PRESCALER_DIV16;
while (TC->STATUS.bit.SYNCBUSY == 1); // wait for sync
setTimerInterval(intervalS);
// Enable the compare interrupt
TC->INTENSET.reg = 0;
TC->INTENSET.bit.MC0 = 1;
NVIC_SetPriority(TC3_IRQn, 3); // Set the TC3 interrupt priority to 3 (lowest)
NVIC_EnableIRQ(TC3_IRQn);
TC->CTRLA.reg |= TC_CTRLA_ENABLE;
while (TC->STATUS.bit.SYNCBUSY == 1); // wait for sync
}
// TC3 Interrupt Handler
void TC3_Handler() {
TcCount16* TC = (TcCount16*) TC3;
// If this interrupt is due to the compare register matching the timer count
// copy any available Serial1 data into SerialBuffer
if (TC->INTFLAG.bit.MC0 == 1) {
TC->INTFLAG.bit.MC0 = 1;
int available1 = Serial1.available(); // Check if there is any data waiting in the Serial1 RX buffer
while (available1 > 0) {
SerialBuffer.store_char(Serial1.read()); // If there is, copy it into our RingBuffer
available1--;
}
}
}
// NeoPixel Functions
// WB2812B blue LED has the highest forward voltage and is slightly dim at 3.3V. The red and green values are adapted accordingly (222 instead of 255).
#ifdef NeoPixel
// Define the NeoPixel colors
#define black 0
#define blue 1
#define cyan 2
#define green 3
#define yellow 4
#define red 5
#define magenta 6
#define white 7
#define dim_blue 8
#define dim_cyan 9
#define dim_green 10
#define dim_yellow 11
#define dim_red 12
#define dim_magenta 13
#define dim_white 14
#define to_dim 7 // Offset from bright to dim colors
int write_color = green; // Flash the NeoPixel this color during SD writes (can be set to magenta or yellow too)
volatile int last_color = black;
volatile int this_color = black;
void setLED(int color) // Set NeoPixel color
{
if (color >= dim_blue)
{
pixels.setBrightness(LED_Brightness / 2); // Dim the LED brightness
}
if (color == black)
{
pixels.setPixelColor(0,0,0,0);
}
else if ((color == dim_blue) || (color == blue))
{
pixels.setPixelColor(0, pixels.Color(0,0,255)); // Set color
}
else if ((color == dim_cyan) || (color == cyan))
{
pixels.setPixelColor(0, pixels.Color(0,222,255)); // Set color
}
else if ((color == dim_green) || (color == green))
{
pixels.setPixelColor(0, pixels.Color(0,222,0)); // Set color
}
else if ((color == dim_yellow) || (color == yellow))
{
pixels.setPixelColor(0, pixels.Color(222,222,0)); // Set color
}
else if ((color == dim_red) || (color == red))
{
pixels.setPixelColor(0, pixels.Color(222,0,0)); // Set color
}
else if ((color == dim_magenta) || (color == magenta))
{
pixels.setPixelColor(0, pixels.Color(222,0,255)); // Set color
}
else // must be dim_white or white
{
pixels.setPixelColor(0, pixels.Color(222,222,255)); // Set color
}
pixels.show();
if (color >= dim_blue)
{
pixels.setBrightness(LED_Brightness); // Reset the LED brightness
}
last_color = this_color;
this_color = color;
}
#endif
// SerialBuffer DEBUG
#ifdef DEBUGserialBuffer
int maxSerialBufferAvailable = 0;
#endif
void setup()
{
#ifdef NeoPixel
// Initialise the NeoPixel
pixels.begin(); // This initializes the NeoPixel library.
delay(100); // Seems necessary to make the NeoPixel start reliably
pixels.setBrightness(LED_Brightness); // Initialize the LED brightness
setLED(black); // Set NeoPixel off
#ifndef NoLED
setLED(dim_blue); // Set NeoPixel to dim blue
#endif
#else
// initialize digital pins RedLED and GreenLED as outputs.
pinMode(RedLED, OUTPUT); // Red LED
pinMode(GreenLED, OUTPUT); // Green LED
digitalWrite(RedLED, LOW); // Turn Red LED off
digitalWrite(GreenLED, LOW); // Turn Green LED off
#ifndef NoLED
// flash red and green LEDs on reset
for (int i=0; i <= 4; i++) {
digitalWrite(RedLED, HIGH);
delay(200);
digitalWrite(RedLED, LOW);
digitalWrite(GreenLED, HIGH);
delay(200);
digitalWrite(GreenLED, LOW);
}
#endif
#endif
// initialize modePin (A0) as an input for the Base/Rover mode select switch
pinMode(modePin, INPUT_PULLUP);
// initialize swPin (A1) as an input for the stop switch
pinMode(swPin, INPUT_PULLUP);
// initialise ExtIntPin (A2) as an input for the EVENT switch
pinMode(ExtIntPin, INPUT_PULLUP);
// Attach the interrupt service routine
// Interrupt on falling edge of the ExtInt signal
attachInterrupt(ExtIntPin, ExtInt, FALLING);
ExtIntTimer = millis(); // Initialise the ExtInt LED timer
// initialise SurveyInPin (A3) as an input for the SURVEY_IN switch
pinMode(SurveyInPin, INPUT_PULLUP);
// SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3D for 128x64
Serial.println(F("SSD1306 allocation failed"));
for(;;); // Don't proceed, loop forever
}
// Show initial display buffer contents on the screen --
// the library initializes this with an Adafruit splash screen.
display.display();
delay(2000); // Pause for 2 seconds
// Clear the buffer
display.clearDisplay();
delay(5000); // Allow 10 sec for user to open serial monitor (Comment this line if required)
//while (!Serial); // OR Wait for user to open the serial monitor (Comment this line as required)
Serial.begin(115200);
// Initialise UBX communication over I2C
Wire.begin();
Wire.setClock(400000); //In-+2crease I2C clock speed to 400kHz
//Begin custom Sky Horse
display.clearDisplay(); // Clear display buffer
display.setTextColor(WHITE);
display.setTextSize(1); // Normal 1:1 pixel scale
display.setCursor(10, 30);
display.write("OLED Ready");
display.display();
delay(1000); //custom Sky Horse
Serial.println("RAWX Logger F9P");
Serial.println("Log GNSS RAWX data to SD card");
display.clearDisplay(); // Clear display buffer
display.setTextColor(WHITE);
display.setTextSize(1); // Normal 1:1 pixel scale
display.setCursor(2, 2);
display.write("RAWX Logger F9P ");
display.display();
display.setCursor(2, 16);
display.write("Logs GNSS data. ");
display.display();
display.setCursor(2, 24);
display.write("Interval minutes: ");
display.display();
display.setCursor(8, 32);
display.write(INTERVAL);
display.display();
delay(2000); //custom Sky Horse
#ifndef NeoPixel
Serial.println("Green LED = Initial GNSS Fix");
Serial.println("Red LED Flash = SD Write");
#else
Serial.println("Blue = Init");
Serial.println("Dim Cyan = Waiting for GNSS Fix");
Serial.println("Cyan = Checking GNSS Fix");
Serial.println("Green flash = SD Write");
Serial.println("Magenta flash = TIME fix in Survey_In mode");
Serial.println("Yellow flash = fixed carrier solution");
Serial.println("White = EVENT (ExtInt) detected");
#endif
Serial.println("Continuous Red indicates a problem or that logging has been stopped");
Serial.println("Initializing GNSS...");
display.clearDisplay(); // Clear display buffer
display.setTextColor(WHITE);
display.setTextSize(1); // Normal 1:1 pixel scale
display.setCursor(1, 30);
display.write("Initializing GNSS.");
display.display();
#ifndef NoLED
#ifdef NeoPixel
setLED(blue); // Set NeoPixel to blue
#endif
#endif
// Initialise UBX communication over I2C
//Wire.begin();
//Wire.setClock(400000); //Increase I2C clock speed to 400kHz
if (i2cGPS.begin(Wire,0x42) == false) //Connect to the Ublox module using Wire port
{
Serial.println(F("Panic!! Ublox GNSS not detected at default I2C address. Please check wiring. Freezing!"));
display.clearDisplay(); // Clear display buffer
display.setTextColor(WHITE);
display.setTextSize(1); // Normal 1:1 pixel scale
display.setCursor(2, 30);
display.write("Ublox GNSS not detected!");
display.display();
#ifndef NoLED
#ifdef NeoPixel
setLED(red); // Set NeoPixel to red
#else
digitalWrite(RedLED, HIGH); // Turn red LED on
#endif
#endif
while (1);
}
Serial.println(F("Ublox GNSS found!"));
display.clearDisplay(); // Clear display buffer
//display.setTextColor(WHITE);
display.setTextSize(1); // Normal 1:1 pixel scale
display.setCursor(2, 30);
display.write("Ublox GNSS found!");
display.display();
#ifdef DEBUG
#ifdef DEBUGi2c
i2cGPS.enableDebugging(); //Enable debug messages over Serial (default)
#endif
#endif
// Turn on DEBUG to see if the commands are acknowledged (Received: CLS:5 ID:1 Payload: 6 8A) or not acknowledged (CLS:5 ID:0)
boolean response = true;
response &= disableI2cNMEA(); //Disable NMEA messages on the I2C port leaving it clear for UBX messages
response &= setUART1BAUD(); // Change the UART1 baud rate to 230400
response &= setRAWXoff(); // Disable RAWX messages on UART1. Also disables the NMEA high precision mode
response &= setNMEAoff(); // Disable NMEA messages on UART1
response &= setTALKERID(); // Set NMEA TALKERID to GP
response &= setRATE_1Hz(); // Set Navigation/Measurement Rate to 1Hz
response &= setUART2BAUD_115200(); // Set UART2 Baud rate
response &= disableSurveyIn(); // Disable Survey_In mode
response &= setRTCMoff(); // Disable RTCM output on UART2
response &= setTimeGrid(); // Set the TP1 TimeGrid to GPS so TIM_TM2 messages are aligned with GPS time
if (response == false) {
Serial.println("Panic!! Unable to initialize GNSS!");
Serial.println("Waiting for reset...");
display.clearDisplay(); // Clear display buffer
display.setTextColor(WHITE);
display.setTextSize(1); // Normal 1:1 pixel scale
display.setCursor(2, 1);
display.write("Unable to initialize GNSS!");
display.setCursor(4, 4);
display.write("Waiting for reset...");
display.display();
#ifndef NoLED
#ifdef NeoPixel
setLED(red); // Set NeoPixel to red
#else
digitalWrite(RedLED, HIGH); // Turn red LED on
#endif
#endif
// don't do anything more:
while(1);
}
// Check the modePin and set the navigation dynamic model
if (digitalRead(modePin) == LOW) {
Serial.println("BASE mode selected");
display.clearDisplay(); // Clear display buffer
display.setTextColor(WHITE);
display.setTextSize(1); // Normal 1:1 pixel scale
display.setCursor(2, 30);
display.write("BASE mode selected");
display.display();
setNAVstationary(); // Set Static Navigation Mode (use this for the Base Logger)
}
else {
base_mode = false; // Clear base_mode flag
Serial.println("ROVER mode selected");
display.clearDisplay(); // Clear display buffer
//display.setTextColor(WHITE);
//display.setTextSize(1); // Normal 1:1 pixel scale
display.setCursor(2, 30);
display.write("ROVER mode selected");
display.display();
// Select one mode for the mobile Rover Logger
//setNAVportable(); // Set Portable Navigation Mode
//setNAVpedestrian(); // Set Pedestrian Navigation Mode
//setNAVautomotive(); // Set Automotive Navigation Mode
//setNAVsea(); // Set Sea Navigation Mode
setNAVair1g(); // Set Airborne <1G Navigation Mode
//setNAVair2g(); // Set Airborne <2G Navigation Mode
//setNAVair4g(); // Set Airborne <4G Navigation Mode
//setNAVwrist(); // Set Wrist Navigation Mode
}
#if defined(NoLED) || defined(NoLogLED)
disableTP1(); // Disable the timepulse to stop the LED from flashing
#endif
Serial1.begin(230400); // Start Serial1 at 230400 baud
delay(2000); //custom Sky Horse
Serial.println("GNSS initialized!");
display.clearDisplay(); // Clear display buffer
//display.setTextColor(WHITE);
//display.setTextSize(1); // Normal 1:1 pixel scale
display.setCursor(2, 30);
display.write("GNSS initialized!");
display.display();
#ifndef NoLED
#ifndef NeoPixel
// flash the red LED during SD initialisation
digitalWrite(RedLED, HIGH);
#endif
#endif
// Initialise SD card
Serial.println("Initializing SD card...");
display.clearDisplay(); // Clear display buffer
//display.setTextColor(WHITE);
//display.setTextSize(1); // Normal 1:1 pixel scale
display.setCursor(2, 30);
display.write("Initializing SD card.");
display.display();
// See if the SD card is present and can be initialized
if (!sd.begin(cardSelect, SD_SCK_MHZ(4))) {
Serial.println("Panic!! SD Card Init failed, or not present!");
Serial.println("Waiting for reset...");
display.clearDisplay(); // Clear display buffer
display.setTextColor(WHITE);
display.setTextSize(1); // Normal 1:1 pixel scale
display.setCursor(2, 1);
display.write("SD Card Init failed.");
display.setCursor(3, 3);
display.write("Waiting for reset...");
display.display();
#ifndef NoLED
#ifdef NeoPixel
setLED(red); // Set NeoPixel to red
#endif
#endif
// don't do anything more:
while(1);
}
Serial.println("SD Card initialized!");
delay(1000); //custom Sky Horse
display.clearDisplay(); // Clear display buffer
//display.setTextColor(WHITE);
//display.setTextSize(1); // Normal 1:1 pixel scale
display.setCursor(2, 30);
display.write("SD Card initialized!");
display.display();
#ifndef NoLED
#ifdef NeoPixel
setLED(dim_cyan); // Set NeoPixel to dim cyan now that the SD card is initialised
#else
// turn red LED off
digitalWrite(RedLED, LOW);
#endif
#endif
#ifdef NeoPixel
write_color = green; // Reset the write color to green
#endif
Serial.println("Waiting for GNSS fix.");
delay(1000); //custom Sky Horse
display.clearDisplay(); // Clear display buffer
//display.setTextColor(WHITE);
//display.setTextSize(1); // Normal 1:1 pixel scale
display.setCursor(2, 30);
display.write("Waiting for GNSS fix.");
display.display();
}
void loop() // run over and over again
{
switch(loop_step) {
case init: {
delay(1000); //Don't pound too hard on the I2C bus
#ifdef DEBUG
Serial.print("\nTime: ");
Serial.print(i2cGPS.getHour(), DEC); Serial.print(':');
Serial.print(i2cGPS.getMinute(), DEC); Serial.print(':');
Serial.print(i2cGPS.getSecond(), DEC); Serial.print('.');
Serial.println(i2cGPS.getMillisecond());
Serial.print("Date: ");
Serial.print(i2cGPS.getDay(), DEC); Serial.print('/');
Serial.print(i2cGPS.getMonth(), DEC); Serial.print("/");
Serial.println(i2cGPS.getYear(), DEC);
Serial.print("Fix: "); Serial.println((int)i2cGPS.getFixType());
if (i2cGPS.getFixType() > 0) {
Serial.print("Location: ");
float latitude = ((float)(i2cGPS.getLatitude())) / 10000000;
Serial.print(F("Lat: "));
Serial.print(latitude, 6);
float longitude = ((float)(i2cGPS.getLongitude())) / 10000000;
Serial.print(F(" Lon: "));
Serial.print(longitude, 6);
Serial.print(F(" (degrees)"));
float altitude = ((float)(i2cGPS.getAltitude())) / 1000;
Serial.print(F(" Alt: "));
Serial.print(altitude, 2);
Serial.println(F(" (m)"));
float speed = ((float)(i2cGPS.getGroundSpeed())) / 1000;
Serial.print("Ground Speed (m/s): "); Serial.println(speed, 3);
float heading = ((float)(i2cGPS.getHeading())) / 10000000;
Serial.print("Heading: "); Serial.println(heading, 1);
Serial.print("Satellites: "); Serial.println(i2cGPS.getSIV());
float PDOP = ((float)(i2cGPS.getPDOP())) / 100;
Serial.print("PDOP: "); Serial.println(PDOP, 2);
}
#endif
// read battery voltage
vbat = analogRead(A7) * (2.0 * 3.3 / 1023.0);
#ifdef DEBUG
Serial.print("Battery(V): ");
Serial.println(vbat, 2);
#endif
// turn green LED on to indicate GNSS fix
// or set NeoPixel to cyan
if (i2cGPS.getFixType() > 0) {
#ifndef NoLED
#ifdef NeoPixel
setLED(cyan); // Set NeoPixel to cyan
#else
digitalWrite(GreenLED, HIGH);
#endif
#endif
// increment valfix and cap at maxvalfix
// don't do anything fancy in terms of decrementing valfix as we want to keep logging even if the fix is lost
valfix += 1;
if (valfix > maxvalfix) valfix = maxvalfix;
}
else {
#ifndef NoLED
#ifdef NeoPixel
setLED(dim_cyan); // Set NeoPixel to dim cyan
#else
digitalWrite(GreenLED, LOW); // Turn green LED off
#endif
#endif
}
if (valfix == maxvalfix) { // wait until we have enough valid fixes
// Set and start the RTC
alarmFlag = false; // Make sure alarm flag is clear
rtc.begin(); // Start the RTC
rtc.setTime(i2cGPS.getHour(), i2cGPS.getMinute(), i2cGPS.getSecond()); // Set the time
rtc.setDate(i2cGPS.getDay(), i2cGPS.getMonth(), (uint8_t)(i2cGPS.getYear() - 2000)); // Set the date
rtc.setAlarmSeconds(0); // Set RTC Alarm Seconds to zero
uint8_t nextAlarmMin = ((i2cGPS.getMinute()+INTERVAL)/INTERVAL)*INTERVAL; // Calculate next alarm minutes
nextAlarmMin = nextAlarmMin % 60; // Correct hour rollover
rtc.setAlarmMinutes(nextAlarmMin); // Set RTC Alarm Minutes
rtc.enableAlarm(rtc.MATCH_MMSS); // Alarm Match on minutes and seconds
rtc.attachInterrupt(alarmMatch); // Attach alarm interrupt
// check if voltage is > LOWBAT(V), if not then don't try to log any data
if (vbat < LOWBAT) {
Serial.println("Low Battery!");
delay(1000); //custom Sky Horse
display.clearDisplay(); // Clear display buffer
display.setTextColor(WHITE);
display.setTextSize(1); // Normal 1:1 pixel scale
display.setCursor(2, 1);
display.write("Low Battery!"); //custom Sky Horse
display.display();