modified the Datalogger example

This commit is contained in:
Fede85 2013-07-01 17:58:03 +02:00
parent d8138ad8ba
commit 2b125a6529
1 changed files with 24 additions and 15 deletions

View File

@ -2,11 +2,15 @@
SD card datalogger SD card datalogger
This example shows how to log data from three analog sensors This example shows how to log data from three analog sensors
to an SD card mounted on the Arduino Yun using the Bridge library. to an SD card mounted on the Arduino Yún using the Bridge library.
The circuit: The circuit:
* analog sensors on analog ins 0, 1, and 2 * analog sensors on analog pins 0, 1 and 2
* SD card attached to SD card slot of the Arduino Yun * SD card attached to SD card slot of the Arduino Yún
Prepare your SD card creating an empty folder in the SD root
named "arduino". This will ensure that the Yún will create a link
to the SD to the "/mnt/sd" path.
You can remove the SD card while the Linux and the You can remove the SD card while the Linux and the
sketch are running but be careful not to remove it while sketch are running but be careful not to remove it while
@ -15,7 +19,7 @@
created 24 Nov 2010 created 24 Nov 2010
modified 9 Apr 2012 modified 9 Apr 2012
by Tom Igoe by Tom Igoe
adapted to the Yun Bridge library 20 Jun 2013 adapted to the Yún Bridge library 20 Jun 2013
by Federico Vanzati by Federico Vanzati
modified 21 Jun 2013 modified 21 Jun 2013
by Tom Igoe by Tom Igoe
@ -25,7 +29,6 @@
*/ */
#include <FileIO.h> #include <FileIO.h>
#include <Serial.h>
void setup() { void setup() {
// Initialize the Bridge and the Serial // Initialize the Bridge and the Serial
@ -34,14 +37,14 @@ void setup() {
FileSystem.begin(); FileSystem.begin();
while(!Serial); // wait for Serial port to connect. while(!Serial); // wait for Serial port to connect.
Serial.println("Filesystem datalogger"); Serial.println("Filesystem datalogger\n");
} }
void loop () { void loop () {
// make a string that start with a timestamp for assembling the data to log: // make a string that start with a timestamp for assembling the data to log:
String dataString = ""; String dataString;
dataString += addTimeStamp(); dataString += getTimeStamp();
dataString += " = "; dataString += " = ";
// read three sensors and append to the string: // read three sensors and append to the string:
@ -49,14 +52,14 @@ void loop () {
int sensor = analogRead(analogPin); int sensor = analogRead(analogPin);
dataString += String(sensor); dataString += String(sensor);
if (analogPin < 2) { if (analogPin < 2) {
dataString += ","; dataString += ","; // separate the values with a comma
} }
} }
// open the file. note that only one file can be open at a time, // open the file. note that only one file can be open at a time,
// so you have to close this one before opening another. // so you have to close this one before opening another.
// The FileSystem card is mounted at the following "/mnt/FileSystema1" // The FileSystem card is mounted at the following "/mnt/FileSystema1"
File dataFile = FileSystem.open("/mnt/sda1/datalog.txt", FILE_APPEND); File dataFile = FileSystem.open("/mnt/sd/datalog.txt", FILE_APPEND);
// if the file is available, write to it: // if the file is available, write to it:
if (dataFile) { if (dataFile) {
@ -74,17 +77,23 @@ void loop () {
} }
// This function append a time stamp to the string passed as argument // This function return a string with the time stamp
String addTimeStamp() { String getTimeStamp() {
String result; String result;
Process time; Process time;
time.begin("date"); // date is a command line utility to get the date and the time
time.addParameter("+%D-%T"); // in different formats depending on the additional parameter
time.run(); time.begin("date");
time.addParameter("+%D-%T"); // parameters: D for the complete date mm/dd/yy
// T for the time hh:mm:ss
time.run(); // run the command
// read the output of the command
while(time.available()>0) { while(time.available()>0) {
char c = time.read(); char c = time.read();
if(c != '\n') if(c != '\n')
result += c; result += c;
} }
return result;
} }