forked from sebzuddas/SunRideUKSEDS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDatalogger1.ino
69 lines (62 loc) · 1.69 KB
/
Datalogger1.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
#include <SPI.h>
#include <SdFat.h>//Same as SD.h but allows longer file names
SdFat SD;
#define baseName "LaunchData"
const int CSPin = 4;
const int baseNameSize = sizeof(baseName) - 1;
char fileName[] = baseName "00.txt";
unsigned long t;
unsigned long Previous_time = 0;
void setup() {
Serial.begin(9600);
Serial.println("Initializing SD card");
if (!SD.begin(CSPin)) {
Serial.println("Initialization failed/card not present");
while (1);
}
while (SD.exists(fileName)) {
if (fileName[baseNameSize + 1] != '9') {
fileName[baseNameSize + 1]++;
} else if (fileName[baseNameSize] != '9') {
fileName[baseNameSize + 1] = '0';
fileName[baseNameSize]++;
} else {
Serial.println("Can't create file name");
return;
}
}
Serial.println("Card initialized");
Serial.print("Created: ");
Serial.println(fileName);
Serial.println("");
}
void loop() {
String data = "";
//Add the other data here and add to data string
t = millis();
data += String(t) + ", " ;
if (millis() - Previous_time >= 30000) {
Previous_time = millis();
while (SD.exists(fileName)) {
if (fileName[baseNameSize + 1] != '9') {
fileName[baseNameSize + 1]++;
} else if (fileName[baseNameSize] != '9') {
fileName[baseNameSize + 1] = '0';
fileName[baseNameSize]++;
} else {
Serial.println("Can't create file name");
return;
}
}
}
File dataFile = SD.open(fileName, FILE_WRITE);
if (dataFile) {
dataFile.println(data);
dataFile.close();
Serial.println(data);
}
else {
Serial.print("Error opening ");
Serial.println(fileName);
}
}