2
2
SD card datalogger
3
3
4
4
This example shows how to log data from three analog sensors
5
- to an SD card mounted on the Arduino Yun using the Bridge library.
5
+ to an SD card mounted on the Arduino Yún using the Bridge library.
6
6
7
7
The circuit:
8
- * analog sensors on analog ins 0, 1, and 2
9
- * SD card attached to SD card slot of the Arduino Yun
8
+ * analog sensors on analog pins 0, 1 and 2
9
+ * SD card attached to SD card slot of the Arduino Yún
10
+
11
+ Prepare your SD card creating an empty folder in the SD root
12
+ named "arduino". This will ensure that the Yún will create a link
13
+ to the SD to the "/mnt/sd" path.
10
14
11
15
You can remove the SD card while the Linux and the
12
16
sketch are running but be careful not to remove it while
15
19
created 24 Nov 2010
16
20
modified 9 Apr 2012
17
21
by Tom Igoe
18
- adapted to the Yun Bridge library 20 Jun 2013
22
+ adapted to the Yún Bridge library 20 Jun 2013
19
23
by Federico Vanzati
20
24
modified 21 Jun 2013
21
25
by Tom Igoe
25
29
*/
26
30
27
31
#include < FileIO.h>
28
- #include < Serial.h>
29
32
30
33
void setup () {
31
34
// Initialize the Bridge and the Serial
@@ -34,29 +37,29 @@ void setup() {
34
37
FileSystem.begin ();
35
38
36
39
while (!Serial); // wait for Serial port to connect.
37
- Serial.println (" Filesystem datalogger" );
40
+ Serial.println (" Filesystem datalogger\n " );
38
41
}
39
42
40
43
41
44
void loop () {
42
45
// make a string that start with a timestamp for assembling the data to log:
43
- String dataString = " " ;
44
- dataString += addTimeStamp ();
46
+ String dataString;
47
+ dataString += getTimeStamp ();
45
48
dataString += " = " ;
46
49
47
50
// read three sensors and append to the string:
48
51
for (int analogPin = 0 ; analogPin < 3 ; analogPin++) {
49
52
int sensor = analogRead (analogPin);
50
53
dataString += String (sensor);
51
54
if (analogPin < 2 ) {
52
- dataString += " ," ;
55
+ dataString += " ," ; // separate the values with a comma
53
56
}
54
57
}
55
58
56
59
// open the file. note that only one file can be open at a time,
57
60
// so you have to close this one before opening another.
58
61
// The FileSystem card is mounted at the following "/mnt/FileSystema1"
59
- File dataFile = FileSystem.open (" /mnt/sda1 /datalog.txt" , FILE_APPEND);
62
+ File dataFile = FileSystem.open (" /mnt/sd /datalog.txt" , FILE_APPEND);
60
63
61
64
// if the file is available, write to it:
62
65
if (dataFile) {
@@ -74,17 +77,23 @@ void loop () {
74
77
75
78
}
76
79
77
- // This function append a time stamp to the string passed as argument
78
- String addTimeStamp () {
80
+ // This function return a string with the time stamp
81
+ String getTimeStamp () {
79
82
String result;
80
83
Process time ;
81
- time .begin (" date" );
82
- time .addParameter (" +%D-%T" );
83
- time .run ();
84
+ // date is a command line utility to get the date and the time
85
+ // in different formats depending on the additional parameter
86
+ time .begin (" date" );
87
+ time .addParameter (" +%D-%T" ); // parameters: D for the complete date mm/dd/yy
88
+ // T for the time hh:mm:ss
89
+ time .run (); // run the command
84
90
91
+ // read the output of the command
85
92
while (time .available ()>0 ) {
86
93
char c = time .read ();
87
94
if (c != ' \n ' )
88
95
result += c;
89
96
}
97
+
98
+ return result;
90
99
}
0 commit comments