-
Notifications
You must be signed in to change notification settings - Fork 0
/
sd.ino
224 lines (194 loc) · 5.41 KB
/
sd.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
#define NUMBER_OF_LOG_FILES 11
#define ROWS_SD 4
String inString = "";
int counter = 0;
int lognumber = 0;
int pointer = 0;
String logname;
const unsigned long SDdoNotWriteTime = 1UL * 1000UL * 30UL * 1UL; //30 sec
bool dataIsWritted = false;
unsigned long writtedTime;
unsigned long start3;
unsigned long timeout3 = 10000;
unsigned long elapsed3;
unsigned long now3;
void initialize_SD(void) {
//pinMode(SS_SD_CARD, OUTPUT);
//digitalWrite(SS_ETHERNET, HIGH);
display.clearDisplay();
display.display();
display.setTextSize(1);
display.setTextColor(WHITE);
start3 = millis();
while (!SD.begin(SS_SD_CARD) and (elapsed3 < timeout3)) {
now3 = millis();
elapsed3 = now3 - start3;
display.clearDisplay();
display.setCursor(0, 0);
display.display();
display.setTextSize(1);
Serial.println("Card failed, or not present");
display.println("Den yparxei SD ");
display.display();
delay(2000);
//return;
}
display.clearDisplay();
display.display();
display.setCursor(0, 0);
//display.println("card initialized");
display.display();
Serial.println("card initialized.");
pointer_file();
digitalWrite(SS_SD_CARD, HIGH);
}
void check_time_and_write_on_SD(double *tempC) {
//pinMode(SS_SD_CARD, OUTPUT);
//digitalWrite(SS_ETHERNET, HIGH);
unsigned long passedTime = millis() - writtedTime;
if (passedTime > SDdoNotWriteTime) {
Serial.println("EDw midenizete tis sd");
dataIsWritted = false;
}
if (!dataIsWritted) {
for (int i = 0; i < NUMBER_OF_SENSORS; i++) {
write_data_to_SD(tempC[i], i);
}
dataIsWritted = true;
writtedTime = millis();
}
digitalWrite(SS_SD_CARD, HIGH);
}
void write_data_to_SD(double dataString, int i) {
String logname = String(lognumber + String(".txt"));
if (counter >= ROWS_SD) // 10000 rows is 250kb
{
pointer = lognumber++;
if (lognumber > NUMBER_OF_LOG_FILES) lognumber = 0; // 10 log files
File dataFile1 = SD.open("pointea.txt", FILE_WRITE);
Serial.print("\nUpdate to pointer ");
dataFile1.seek(0);
dataFile1.println(lognumber);
Serial.print("\npointea.txt is ");
while (dataFile1.available()) {
Serial.write(dataFile1.read());
}
Serial.print("\n");
int nextlognumber = lognumber + 1;
if (nextlognumber > (NUMBER_OF_LOG_FILES)) nextlognumber = 0;
String nextlogname = String(nextlognumber + String(".txt"));
Serial.print("\n lognumber: "); Serial.print(lognumber);
Serial.print("\n nextlognumber: "); Serial.print(nextlognumber);
SD.remove(nextlogname);
dataFile1.close();
// logname = String(lognumber + String(".txt"));
counter = 0;
}
File dataFile = SD.open(logname, FILE_WRITE);
// if the file is available, write to it:
if (dataFile) {
Serial.print("\nSaved on ");
Serial.print(logname);
Serial.print("\n");
if (read_from_eeprom(i * 2) == 0) {
dataFile.print("Σε συντηρηση");
}
else {
dataFile.print(dataString); // write to sd temp and time
dataFile.print("C");
}
dataFile.print(' ');
dataFile.print(i);
dataFile.print(' ');
DateTime now = rtc.now();
dataFile.print(' ');
dataFile.print(now.year(), DEC);
dataFile.print('/');
dataFile.print(now.month(), DEC);
dataFile.print('/');
dataFile.print(now.day(), DEC);
dataFile.print(' ');
dataFile.print(now.hour(), DEC);
dataFile.print(':');
dataFile.print(now.minute(), DEC);
dataFile.println();
dataFile.close();
dataIsWritted = true;
// print to the serial port too:
dataFile.close();
counter++;
}
// if the file isn't open, pop up an error:
else {
dataFile.close();
display.clearDisplay();
display.display();
display.setCursor(0, 0);
Serial.println("error opening datalog.txt");
display.println("Sfalma anigamtos arxeiou log");
display.display();
SD.begin(SS_SD_CARD);
delay(2000);
}
dataFile.close();
}
void pointer_file() {
if (SD.exists("pointea.txt"))
{
File dataFile1 = SD.open("pointea.txt", FILE_READ);
if (dataFile1)
{
Serial.println("reading pointer");
display.clearDisplay();
display.display();
display.setCursor(0, 0);
display.println("Diavasma kartas sd");
display.display();
delay(100);
while (dataFile1.available())
{
int pointer = (dataFile1.read());
if (isDigit(pointer))
{
// convert the incoming byte to a char
// and add it to the string:
inString += (char)pointer;
Serial.print("Value:");
Serial.println(lognumber = inString.toInt());
String inString = "";
}
}
}
dataFile1.close();
}
else
{
File dataFile1 = SD.open("pointea.txt", FILE_WRITE);
if (dataFile1)
{
display.clearDisplay();
display.display();
display.setCursor(0, 0);
display.println("reading pointer");
display.display();
delay(500);
Serial.print("\nWrite to pointer ");
dataFile1.println(lognumber);
Serial.print("\nPointer is ");
Serial.print(lognumber);
Serial.print("\n");
dataFile1.close();
}
else
{
dataFile1.close();
Serial.println("Error opening pointer");
display.clearDisplay();
display.display();
display.setCursor(0, 0);
display.println("Error opening pointer");
display.display();
delay(2000);
}
}
}