-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathfileread.ino
119 lines (101 loc) · 3.2 KB
/
fileread.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
// Function to read a text file one field at a time.
//
#include <SPI.h>
#include <SD.h>
#define CS_PIN 10
File file;
/*
* Read a file one field at a time.
*
* file - File to read.
*
* str - Character array for the field.
*
* size - Size of str array.
*
* delim - String containing field delimiters.
*
* return - length of field including terminating delimiter.
*
* Note, the last character of str will not be a delimiter if
* a read error occurs, the field is too long, or the file
* does not end with a delimiter. Consider this an error
* if not at end-of-file.
*
*/
size_t readField(File* file, char* str, size_t size, char* delim) {
char ch;
size_t n = 0;
while ((n + 1) < size && file->read(&ch, 1) == 1) {
// Delete CR.
if (ch == '\r') {
continue;
}
str[n++] = ch;
if (strchr(delim, ch)) {
break;
}
}
str[n] = '\0';
return n;
}
//------------------------------------------------------------------------------
#define errorHalt(msg) {Serial.println(F(msg)); while(1);}
//------------------------------------------------------------------------------
void setup() {
Serial.begin(9600);
// Initialize the SD.
if (!SD.begin(CS_PIN)) errorHalt("begin failed");
// Create or open the file.
file = SD.open("alt-az-O482020.txt", FILE_WRITE);
if (!file) errorHalt("open failed");
// Rewind file so test data is not appended.
file.seek(0);
// Write test data.
// file.print(F(
// "field_1_1,field_1_2,field_1_3\r\n"
// "field_2_1,field_2_2,field_2_3\r\n"
// "field_3_1,field_3_2\r\n" // missing a field
// "field_4_1,field_4_2,field_4_3\r\n"
// "field_5_1,field_5_2,field_5_3" // no delimiter
// ));
// Rewind the file for read.
file.seek(0);
size_t n; // Length of returned field with delimiter.
char str[20]; // Must hold longest field with delimiter and zero byte.
int count=0; // to add the serial floats
float values[4]; //to make an array of 4 values
// Read the file and print fields.
while (true) {
n = readField(&file, str, sizeof(str), "\n- "); //added the - and a space too cause that is counted as a delimitor here
// done if Error or at EOF.
if (n == 0) break;
// Print the type of delimiter.
if (str[n-1] == '-' || str[n-1] == '\n' || str[n-1] == ' ') {
// Serial.print(str[n-1] == ',' ? F("comma: ") : F("endl: ")); not printing what it is
// Remove the delimiter.
str[n-1] = 0;
} else {
// At eof, too long, or read error. Too long is error.
Serial.print(file.available() ? F("error: ") : F("eof: "));
}
// Print the field.
Serial.println(str);
if (count % 3 !=0){
strf = (float) str //converting the string to float
cord[count]=strf; //the values keep o gettig added to the cord string
}
}
// the expected output should be
// 6:25:1
// 40.192... (the initial value of theta)
// 283.482... (the initial value of phi)
// 6:50:41
// 46.18300932842953 (the final value of theta)
// 286.7100693897322 (the final value of phi)
// and the array will be the set of values
file.close();
}
//------------------------------------------------------------------------------
void loop() {
}