-
Notifications
You must be signed in to change notification settings - Fork 0
/
sketch_aug19a.ino
243 lines (207 loc) · 5.92 KB
/
sketch_aug19a.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
#include <TinyGPS.h>
#include <SPI.h>
#include <Adafruit_Sensor.h>
#include "HardwareSerial.h"
#include "SdFat.h"
#include "Adafruit_TSL2591.h"
#include "Adafruit_MCP9808.h"
#include <Wire.h>
#include <stdio.h>
//#include "Buffer.h"
#define LED_PIN 13
#define LIGHT_ERR -520392.0f
//chip for sd io
#define CHIP_SELECT 10
#define SD_SPEED SD_SCK_MHZ(50)
#define DEBUG
#define SD_MISSING_ERR 2
#define FILE_CREATION_ERR 3
//#define BUFFER_SIZE 1000
TinyGPS gps;
//Buffer buffer(BUFFER_SIZE);
Adafruit_TSL2591 tsl;
Adafruit_MCP9808 tempsensor;
File sd;
SdFile logfile;
char name[] = "LOGGER00.CSV";
void initTSL2591();
void initMCP9808();
float tsl2591UnifiedRead();
float mcp9808Read();
void error();
static void smartdelay(unsigned long ms);
static void log_float(float val, float invalid);
static void log_int(unsigned long val, unsigned long invalid);
static void log_date(TinyGPS &gps);
static void log_str(const char *str);
void setup() {
Serial.println(F("Starting eclipse logger."));
initTSL2591();
initMCP9808();
Serial.begin(115200);
Serial1.begin(9600);
pinMode( LED_PIN, OUTPUT );
pinMode( 10, OUTPUT );
Serial.println(F("Starting SD card."));
if (!sd.begin(CHIP_SELECT, SD_SPEED)) {
sd.initErrorHalt();
}
for (uint8_t i = 0; i < 100; i++) {
name[6] = i/10 + '0';
name[7] = i%10 + '0';
if (sd.exists(name)) continue;
break;
}
logfile = sd.open(name, FILE_WRITE);
if (!logfile) {
Serial.print(F("couldn't create "));
Serial.println(name);
error(FILE_CREATION_ERR);
}
Serial.print(F("Writing to "));
Serial.println(name);
}
void error( uint8_t errno ) {
while (1) {
uint8_t i;
for ( i=0;i < errno;i++ ) {
digitalWrite( LED_PIN, HIGH );
delay( 100 );
digitalWrite( LED_PIN, LOW );
delay( 100 );
}
for ( i=errno; i<10; i++ ) {
delay( 200 );
}
}
}
void initTSL2591() {
Serial.println(F("Starting Adafruit TSL2591"));
if (tsl.begin()) {
Serial.println(F("Found a TSL2591 sensor"));
}
else {
Serial.println(F("No sensor found ... check your wiring?"));
while(1);
}
// You can change the gain on the fly, to adapt to brighter/dimmer light situations
//tsl.setGain(TSL2591_GAIN_LOW); // 1x gain (bright light)
tsl.setGain(TSL2591_GAIN_MED); // 25x gain
// tsl.setGain(TSL2591_GAIN_HIGH); // 428x gain
// Changing the integration time gives you a longer time over which to sense light
// longer timelines are slower, but are good in very low light situtations!
tsl.setTiming(TSL2591_INTEGRATIONTIME_100MS); // shortest integration time (bright light)
// tsl.setTiming(TSL2591_INTEGRATIONTIME_200MS);
// tsl.setTiming(TSL2591_INTEGRATIONTIME_300MS);
// tsl.setTiming(TSL2591_INTEGRATIONTIME_400MS);
// tsl.setTiming(TSL2591_INTEGRATIONTIME_500MS);
// tsl.setTiming(TSL2591_INTEGRATIONTIME_600MS); // longest integration time (dim light)
/* Display the gain and integration time for reference sake */
tsl2591Gain_t gain = tsl.getGain();
switch(gain)
{
case TSL2591_GAIN_LOW:
Serial.println(F("1x (Low)"));
break;
case TSL2591_GAIN_MED:
Serial.println(F("25x (Medium)"));
break;
case TSL2591_GAIN_HIGH:
Serial.println(F("428x (High)"));
break;
case TSL2591_GAIN_MAX:
Serial.println(F("9876x (Max)"));
break;
}
}
void initMCP9808() {
Serial.println(F("Starting MCP9808"));
if (!tempsensor.begin()) {
Serial.println(F("Couldn't find MCP9808"));
while (1);
}
}
//recommended reader for tsl2591
float tsl2591UnifiedRead() {
Serial.println(F("Reading from TSL2591."));
sensors_event_t event;
tsl.getEvent( &event );
if ((event.light == 0) | (event.light > 4294966000.0) | (event.light <-4294966000.0))
{
Serial.println(F("Invalid data (adjust gain or timing)"));
}
else
{
return event.light;
}
return LIGHT_ERR;
}
float mcp9808Read() {
return tempsensor.readTempC();
}
int i = 0;
void loop()
{
unsigned long age, date, time, chars = 0;
unsigned short sentences, failed;
float lan, lon;
log_int(gps.satellites(), TinyGPS::GPS_INVALID_SATELLITES);
log_int(gps.hdop(), TinyGPS::GPS_INVALID_HDOP);
gps.f_get_position(&lan, &lon, &age);
log_float(lat, TinyGPS::GPS_INVALID_F_ANGLE);
log_float(lon, TinyGPS::GPS_INVALID_F_ANGLE);
log_int(age, TinyGPS::GPS_INVALID_AGE);
log_date(gps);
log_float(gps.f_altitude(), TinyGPS::GPS_INVALID_F_ALTITUDE);
log_float(gps.f_course(), TinyGPS::GPS_INVALID_F_ANGLE);
log_float(gps.f_speed_kmph(), TinyGPS::GPS_INVALID_F_SPEED);
log_str(gps.f_course() == TinyGPS::GPS_INVALID_F_ANGLE ? "***" : TinyGPS::cardinal(gps.f_course()));
log(tsl2591UnifiedRead(), LIGHT_ERR);
logfile.print(mcp9808Read());
logfile.println();
if (i % 60) {
logfile.close(); logfile = sd.open(name, FILE_WRITE);
}
i++;
smartdelay(1000);
}
static void smartdelay(unsigned long ms) {
unsigned long start = millis();
do {
while(Serial1.available())
gps.encode(Serial1.read());
} while (millis() - start < ms);
}
static void log_float(float val, float invalid) {
if (val == invalid) logfile.print('*');
else logfile.print(val);
logfile.print(',');
smartdelay(0);
}
static void log_int(unsigned long val, unsigned long invalid) {
if (val == invalid) logfile.print('*');
else logfile.print(val);
logfile.print(',');
smartdelay(0);
}
static void log_date(TinyGPS &gps) {
int year;
byte month, day, hour, minute, second, hundredths;
unsigned long age;
gps.crack_datetime(&year, &month, &day, &hour, &minute, &second, &hundredths, &age);
if (age == TinyGPS::GPS_INVALID_AGE) logfile.print('*');
else {
char sz[32];
sprintf(sz, "%02d/%02d/%02d %02d:%02d:%02d ",
month, day, year, hour, minute, second);
logfile.print(sz);
}
logfile.print(',');
log_int(age, TinyGPS::GPS_INVALID_AGE, 5);
logfile.print(',');
smartdelay(0);
}
static void log_str(const char *str) {
logfile.print(str); logfile.print(',');
smartdelay(0);
}