This repository has been archived by the owner on Dec 10, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patholed_datalogger_software_1_0.c
830 lines (748 loc) · 17.5 KB
/
oled_datalogger_software_1_0.c
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
/*
OLED Datalogger 3 Channel System
Jed Hodson 2015
*/
//Includes
#include <LBattery.h>
//Time and Location
#include <LDateTime.h>
#include <LGPS.h>
//OLED and SD
#include <SPI.h>
#include <SD.h>
#include <FTOLED.h>
//Memory Onboard for Logging
#include <LTask.h>
#include <LFlash.h>
#include <LSD.h>
#include <LStorage.h>
#define Drv LFlash // use Internal 10M Flash
// #define Drv LSD // use SD card
//Fonts
#include <fonts/Arial14.h>
#include <fonts/Droid_Sans_24.h>
//OLED and SD Params
const byte pin_cs = 7;
const byte pin_dc = 2;
const byte pin_reset = 3;
const byte pin_sd_cs = 4;
OLED oled(pin_cs, pin_dc, pin_reset);
//GPS and Time Params
datetimeInfo t;
unsigned int rtc;
bool GPS_ACTIVE = FALSE;
bool GPS_SYNCED = FALSE;
int getDay;
int getMonth;
int getYear;
int getHour;
int getMin;
int getSec;
//Other Params
const byte CH1 = A0;
const byte CH2 = A1;
const byte CH3 = 9;
int CH1_RAW = 0; //For calculations on Channel data feed
int CH2_RAW = 0;
int CH3_RAW = 0;
float CH1_CAL = 0;
float CH2_CAL = 0;
float CH3_CAL = 0;
const byte button = 8;
int operationStatus = 0;
int screenCycle = 0;
int buttonCycle = 1;
int lastUpdate = 0;
int lastMinute = 0;
bool lowBattery = FALSE;
long currentmillis = 0;
unsigned long sinceCharge = 0;
const int waitInterval = 5000;
//Messages
#define WELCOME1 F("Multi-Channel \nDatalogger by \nJed Hodson 2015 \n\nPlease Wait...")
#define GPSINSTRUC F("Syncing Time via \nGPS. Please Turn \nDevice Upside-down \nin clear view of sky \nThen wait for Sync")
//OLED Boxes
OLED_TextBox titleScreen(oled, 0, 96, 128, 32);
OLED_TextBox mainScreen(oled, 0, 0, 128, 96);
//Test and Debug Params
bool debug = FALSE; //Enable Debug Mode?
const int interval = 2000;
void setup() {
//Setup OLED and SD
oled.begin();
operationStatus = 3;
setTitle();
setBackgrounds();
mainScreen.println(WELCOME1);
showDebugInfo();
delay(interval);
//Setup all params
pinMode(button, INPUT_PULLUP);
pinMode(CH1, INPUT);
pinMode(CH2, INPUT);
pinMode(CH3, INPUT);
//Set up memory
pinMode(10, OUTPUT);
// see if the card is present and can be initialized:
LTask.begin();
Drv.begin();
mainScreen.clear();
Serial.begin(115200); //Begin Serial Regardless of DEBUG state
if (debug == TRUE) //If debug is enabled, wait until a character is sent before beginging
{
while (!Serial.available())
{
Serial.println("Send character to begin debug");
mainScreen.println("Waiting for");
mainScreen.println("Serial Line");
delay(2000);
mainScreen.reset();
}
mainScreen.clear();
delay(100);
Serial.println("Debug Mode Enabled Beginging Startup...");
delay(interval);
}
Serial.println("Going into GPS Set Time Void...");
setTimeGPS();
operationStatus = 0;
setTitle();
}
void loop()
{
screenChange();
lowBatteryWarning();
runLogger();
}
void setBackgrounds()
{
titleScreen.setBackgroundColour(BLACK);
mainScreen.setBackgroundColour(BLACK);
delay(10);
titleScreen.setForegroundColour(RED);
mainScreen.setForegroundColour(RED);
delay(10);
oled.selectFont(Arial14);
delay(10);
}
void setTitle()
{
oled.selectFont(Droid_Sans_24);
if (operationStatus == 0)
{
titleScreen.clear();
titleScreen.setForegroundColour(GREEN);
titleScreen.print("RUNNING ");
setBackgrounds();
delay(10);
} else if (operationStatus == 1)
{
titleScreen.clear();
titleScreen.setForegroundColour(YELLOW);
titleScreen.print("ATTENTION ");
setBackgrounds();
delay(10);
} else if (operationStatus == 2)
{
titleScreen.clear();
titleScreen.setForegroundColour(RED);
titleScreen.print("WARNING ");
setBackgrounds();
delay(10);
}
else if (operationStatus == 3)
{
titleScreen.clear();
titleScreen.setForegroundColour(BLUE);
titleScreen.print(" WAIT");
setBackgrounds();
delay(10);
}
else
{
titleScreen.clear();
titleScreen.setForegroundColour(RED);
titleScreen.print("UNDEFINED");
setBackgrounds();
delay(10);
}
}
void setTimeGPS()
{
updateRTC();
operationStatus = 1; //Adjust Title to suit
setTitle();
while (GPS_SYNCED != TRUE)
{
//Update RTC
updateRTC();
mainScreen.print(GPSINSTRUC);
mainScreen.reset();
delay(interval);
//Turn GPS ON or OFF
if ((GPS_ACTIVE != TRUE) && (t.year < 2010))
{
Serial.println("Powering GPS ON and Syncing");
//mainScreen.clear();
//mainScreen.println("Sync Started!");
LGPS.powerOn();
GPS_ACTIVE = TRUE;
delay(interval);
}
if ((GPS_ACTIVE != TRUE) && (t.year >= 2015))
{
Serial.println("Already Synced");
//mainScreen.clear();
//mainScreen.println("Already Synced");
GPS_SYNCED = TRUE;
LGPS.powerOff();
GPS_ACTIVE = FALSE;
delay(interval);
}
if ((GPS_ACTIVE == TRUE) && (t.year >= 2015))
{
Serial.println("Synced! Powering OFF GPS. Wait...");
mainScreen.clear();
//mainScreen.println("Synced! GPS");
//mainScreen.println("Powering OFF...");
LGPS.powerOff();
GPS_ACTIVE = FALSE;
GPS_SYNCED = TRUE;
delay(interval);
}
//Report Time Regardless if set or not
//mainScreen.println("Waiting for GPS...");
//reportTimeAll();
}
//Report Synced Time
mainScreen.clear();
mainScreen.println("Device Synced!");
mainScreen.println("Synced Time: ");
reportTimeAll();
delay(interval);
mainScreen.clear();
}
void reportTimeSerial()
{
if (debug == TRUE)
{
Serial.print("Current GMT: ");
Serial.print(getDay);
Serial.print("/");
Serial.print(getMonth);
Serial.print("/");
Serial.print(getYear);
Serial.print("@");
Serial.print(getHour);
Serial.print(":");
Serial.print(getMin);
Serial.print(":");
Serial.println(getSec);
delay(10);
}
}
void reportTimeOLED()
{
String OLEDTimeDate = "";
OLEDTimeDate += getDay;
OLEDTimeDate += "/";
OLEDTimeDate += getMonth;
OLEDTimeDate += "/";
OLEDTimeDate += getYear;
OLEDTimeDate += "@";
OLEDTimeDate += getHour;
OLEDTimeDate += ":";
OLEDTimeDate += getMin;
OLEDTimeDate += ":";
OLEDTimeDate += getSec;
mainScreen.println("Current GMT:");
mainScreen.println(OLEDTimeDate);
delay(10);
//mainScreen.reset();
}
void updateRTC()
{
LDateTime.getTime(&t); //Get Time
LDateTime.getRtc(&rtc);
}
void reportTimeAll()
{
updateRTC();
getDateTimeVars();
reportTimeSerial();
reportTimeOLED();
mainScreen.reset();
delay(400);
}
void getDateTimeVars()
{
getDay = t.day;
getMonth = t.mon;
getYear = t.year;
getHour = t.hour;
getMin = t.min;
getSec = t.sec;
delay(10);
}
void timeSinceLast()
{
updateRTC();
getDateTimeVars();
lastUpdate = (getMin - lastMinute);
String timeSinceLastMes = "Last Log: ";
timeSinceLastMes += lastUpdate;
timeSinceLastMes += " Mins";
mainScreen.println(timeSinceLastMes);
Serial.println(timeSinceLastMes);
delay(10);
}
void pushBatteryLevel()
{
String batteryLevel = "Battery: ";
batteryLevel += LBattery.level();
batteryLevel += "%";
mainScreen.println(batteryLevel);
delay(10);
String batteryCharging = "Charging: ";
if (LBattery.isCharging() == 0)
{
batteryCharging += "NO";
}
else if (LBattery.isCharging() == 1)
{
batteryCharging += "YES";
}
mainScreen.println(batteryCharging);
Serial.println(batteryCharging);
delay(10);
batteryCharging = "";
batteryLevel = "";
}
void batteryLOWCheck()
{
if (LBattery.level() > 20)
{
lowBattery = FALSE;
}
else if ((LBattery.level() <= 20) && (LBattery.isCharging() == 0))
{
lowBattery = TRUE;
}
}
void lowBatteryWarning()
{
batteryLOWCheck();
if (lowBattery == TRUE)
{
operationStatus = 2;
setTitle();
mainScreen.clear();
pushBatteryWarning();
while (lowBattery == TRUE)
{
batteryLOWCheck();
pushBatteryWarning();
mainScreen.reset();
delay(100);
}
delay(10);
}
else if (lowBattery == FALSE)
{
//operationStatus = 0;
//setTitle();
delay(10);
markBattery();
}
}
void markBattery()
{
//Mark Full Recharge Battery
if ((LBattery.level() == 100) && (LBattery.isCharging() == 1))
{
sinceCharge = rtc;
delay(10);
}
}
void screenChange()
{
int buttonPressed = digitalRead(button);
if (buttonPressed == LOW)
{
//When Button is pressed change the screen cycle variable up and set Title
if (buttonCycle == 0)
{
setTitle();
//Serial.println("Pressed: Cycle 0");
mainScreen.clear();
buttonCycle = 1;
screenCycle = 0;
delay(20);
}
else if (buttonCycle == 1)
{
//Serial.println("Pressed: Cycle 1");
mainScreen.clear();
buttonCycle = 2;
screenCycle = 1;
delay(20);
}
else if (buttonCycle == 2)
{
//Serial.println("Pressed: Cycle 2");
mainScreen.clear();
buttonCycle = 3;
screenCycle = 2;
delay(20);
}
else if (buttonCycle == 3)
{
//Serial.println("Pressed: Cycle 3");
mainScreen.clear();
buttonCycle = 4;
screenCycle = 3;
delay(20);
}
else if (buttonCycle == 4)
{
//Serial.println("Pressed: Cycle 4");
pushBlankScreen();
mainScreen.clear();
buttonCycle = 0;
screenCycle = 4;
delay(20);
}
}
pushScreenChanges();
}
void pushScreenChanges()
{
//Check which cycle is in and push screen accordingly
if (screenCycle == 0)
{
//Serial.println("Act: Cycle 0");
pushScreen0();
mainScreen.reset();
delay(10);
}
else if (screenCycle == 1)
{
//Serial.println("Act: Cycle 1");
pushScreen1();
mainScreen.reset();
delay(10);
}
else if (screenCycle == 2)
{
//Serial.println("Act: Cycle 2");
pushScreen2();
mainScreen.reset();
delay(10);
}
else if (screenCycle == 3)
{
//Serial.println("Act: Cycle 3");
pushScreen3();
mainScreen.reset();
delay(10);
}
else if (screenCycle == 4)
{
//Serial.println("Act: Cycle 4");
//pushBlankScreen();
delay(10);
}
}
void pushScreen0()
{
//SYSINFO 1
pushBatteryLevel();
timeSinceLast();
if (lowBattery == FALSE)
{
mainScreen.println("BATTERY OK");
}
reportTimeAll();
}
void pushScreen1()
{
//SYSINFO 2 - Uptime and last full battery charge
uptime();
batteryFullUptime();
mainScreen.print("GPS Active: ");
if (GPS_ACTIVE == TRUE)
{
mainScreen.println("TRUE");
}
else
{
mainScreen.println("FALSE");
}
mainScreen.print("GPS Synced: ");
if (GPS_SYNCED == TRUE)
{
mainScreen.println("TRUE");
}
else
{
mainScreen.println("FALSE");
}
}
void pushScreen2()
{
calculateAll();
//Channel 1 Display
mainScreen.print("CH1 RAW: ");
mainScreen.println(CH1_RAW);
mainScreen.print("CH1 CAL: ");
mainScreen.println(CH1_CAL);
//Channel 2 Display
mainScreen.print("CH2 RAW: ");
mainScreen.println(CH2_RAW);
mainScreen.print("CH2 CAL: ");
mainScreen.println(CH2_CAL);
//Channel 3 Display
mainScreen.print("CH3 RAW: ");
mainScreen.println(CH3_RAW);
mainScreen.print("CH3 CAL: ");
mainScreen.println(CH3_CAL);
mainScreen.reset();
delay(10);
}
void pushScreen3()
{
//Force Update
mainScreen.println("Press and Hold");
mainScreen.println("Button to Log.");
mainScreen.println("or short press to");
mainScreen.println("skip.");
int buttonPressed = digitalRead(button); //Check to See if long press or short press
if (buttonPressed == LOW)
{
delay(500);
int buttonPressed = digitalRead(button);
if (buttonPressed == LOW)
{
delay(2000);
int buttonPressed = digitalRead(button);
if (buttonPressed == LOW)
{
//Run Logger
actLogger();
}
else
{
screenCycle = 4;
pushScreenChanges();
}
} else
{
screenCycle = 4;
pushScreenChanges();
}
}
}
void pushBlankScreen()
{
mainScreen.clear();
titleScreen.clear();
delay(10);
}
void pushBatteryWarning()
{
mainScreen.println("LOW BATTERY");
pushBatteryLevel();
batteryFullUptime();
//uptime();
//mainScreen.println("");
mainScreen.println("Plug in Charger!");
mainScreen.reset();
markBattery();
delay(10);
}
void calculateAll()
{
CH1_Calculations();
CH2_Calculations();
CH3_Calculations();
}
void CH1_Calculations()
{
CH1_RAW = analogRead(CH1);
float CH1_VOLTAGE = CH1_RAW * 5.0;
CH1_VOLTAGE /= 1024.0;
CH1_CAL = (CH1_VOLTAGE - 0.5) * 100 ;
}
void CH2_Calculations()
{
CH2_RAW = analogRead(CH2);
float CH2_VOLTAGE = CH2_RAW * 5.0;
CH2_VOLTAGE /= 1024.0;
CH2_CAL = (CH2_VOLTAGE - 0.5) * 100 ;
}
void CH3_Calculations()
{
CH3_RAW = digitalRead(CH3);
CH3_CAL != CH3_RAW; //Invert Input for calculation
}
void showDebugInfo()
{
if (debug == TRUE)
{
mainScreen.println("DEBUG: TRUE");
}
else
{
mainScreen.println("DEBUG: FALSE");
}
}
void uptime()
{
long days = 0;
long hours = 0;
long mins = 0;
long secs = 0;
currentmillis = millis();
secs = currentmillis / 1000; //convect milliseconds to seconds
mins = secs / 60; //convert seconds to minutes
hours = mins / 60; //convert minutes to hours
days = hours / 24; //convert hours to days
secs = secs - (mins * 60); //subtract the coverted seconds to minutes in order to display 59 secs max
mins = mins - (hours * 60); //subtract the coverted minutes to hours in order to display 59 minutes max
hours = hours - (days * 24); //subtract the coverted hours to days in order to display 23 hours max
//Create String to Display
String uptimeData = "Uptime: ";
if (days > 0)
{
uptimeData += days;
uptimeData += "d ";
}
uptimeData += hours;
uptimeData += "h ";
uptimeData += mins;
uptimeData += "m ";
uptimeData += secs;
uptimeData += "s";
//Display string on OLED and Serial
mainScreen.println(uptimeData);
Serial.println(uptimeData);
delay(10);
}
void batteryFullUptime()
{
//Calculate time since battery was last fully charged
long days1 = 0;
long hours1 = 0;
long mins1 = 0;
long secs1 = 0;
long cacuSec1 = 0;
updateRTC();
cacuSec1 = rtc - sinceCharge;
secs1 = cacuSec1; //convect milliseconds to seconds
mins1 = secs1 / 60; //convert seconds to minutes
hours1 = mins1 / 60; //convert minutes to hours
days1 = hours1 / 24; //convert hours to days
secs1 = secs1 - (mins1 * 60); //subtract the coverted seconds to minutes in order to display 59 secs max
mins1 = mins1 - (hours1 * 60); //subtract the coverted minutes to hours in order to display 59 minutes max
hours1 = hours1 - (days1 * 24); //subtract the coverted hours to days in order to display 23 hours max
//Create String to Display
mainScreen.println("Since Recharge:");
String uptimeData = "";
if ((days1 > 0) && (days1 < 16600))
{
uptimeData += days1;
uptimeData += "d ";
}
else if (days1 >= 16600)
{
uptimeData += "N/A";
}
else
{
uptimeData += hours1;
uptimeData += "h ";
uptimeData += mins1;
uptimeData += "m ";
uptimeData += secs1;
uptimeData += "s";
//Display string on OLED and Serial
mainScreen.println(uptimeData);
Serial.println(uptimeData);
delay(10);
}
}
void runLogger()
{
updateRTC();
getDateTimeVars();
//Enter conditions for logging to automatically begin
if ((getMin == 00) || (getMin == 30) && (lastMinute != getMin))
{
actLogger();
}
}
void actLogger()
{
updateRTC();
getDateTimeVars();
operationStatus = 1;
setTitle();
mainScreen.clear();
mainScreen.println("Logging Data...");
mainScreen.println("Do NOT power off");
mainScreen.println("or Disconnect");
delay(interval);
calculateAll();
makeDataString();
delay(waitInterval);
calculateAll();
makeDataString();
mainScreen.clear();
operationStatus = 0;
setTitle();
}
void makeDataString()
{
//Make String - Make the CSV File
String datalogWrite = "";
datalogWrite += getDay;
datalogWrite += "/";
datalogWrite += getMonth;
datalogWrite += "/";
datalogWrite += getYear;
datalogWrite += "@";
datalogWrite += getHour;
datalogWrite += ":";
datalogWrite += getMin;
datalogWrite += ",";
datalogWrite += CH1_CAL;
datalogWrite += ",";
datalogWrite += CH2_CAL;
LFile dataFile = Drv.open("dataloggerReadings.csv", FILE_WRITE); //Open File
if (dataFile)
{
dataFile.println(datalogWrite);
dataFile.close();
mainScreen.clear();
mainScreen.println("Success!");
mainScreen.println("Log Written");
//lastMinute
lastMinute = getMin;
delay(interval);
}
else
{
mainScreen.clear();
operationStatus = 2;
setTitle();
mainScreen.println("ERROR!");
mainScreen.println("Failed to Log!");
mainScreen.println("");
mainScreen.println("Select to continue...");
int buttonPressed = digitalRead(button);
while (button == HIGH)
{ //Do nothing until button is pressed to acknowledge
}
mainScreen.clear();
}
}