2010-11-29 21:20:30 +01:00
|
|
|
/*
|
|
|
|
SD card datalogger
|
2013-10-21 09:58:40 +02:00
|
|
|
|
|
|
|
This example shows how to log data from three analog sensors
|
2010-11-29 21:20:30 +01:00
|
|
|
to an SD card using the SD library.
|
2013-10-21 09:58:40 +02:00
|
|
|
|
2010-11-29 21:20:30 +01:00
|
|
|
The circuit:
|
|
|
|
* analog sensors on analog ins 0, 1, and 2
|
|
|
|
* SD card attached to SPI bus as follows:
|
|
|
|
** MOSI - pin 11
|
|
|
|
** MISO - pin 12
|
|
|
|
** CLK - pin 13
|
|
|
|
** CS - pin 4
|
2013-10-21 09:58:40 +02:00
|
|
|
|
2010-11-29 21:20:30 +01:00
|
|
|
created 24 Nov 2010
|
2012-04-09 15:51:35 +02:00
|
|
|
modified 9 Apr 2012
|
2010-11-29 21:20:30 +01:00
|
|
|
by Tom Igoe
|
2013-10-21 09:58:40 +02:00
|
|
|
|
2010-11-29 21:20:30 +01:00
|
|
|
This example code is in the public domain.
|
2013-10-21 09:58:40 +02:00
|
|
|
|
2010-11-29 21:20:30 +01:00
|
|
|
*/
|
|
|
|
|
2012-09-25 18:29:19 +02:00
|
|
|
#include <SPI.h>
|
2010-11-29 21:20:30 +01:00
|
|
|
#include <SD.h>
|
|
|
|
|
|
|
|
const int chipSelect = 4;
|
|
|
|
|
2015-07-06 15:18:33 +02:00
|
|
|
void setup() {
|
2013-10-21 09:58:40 +02:00
|
|
|
// Open serial communications and wait for port to open:
|
2010-11-29 21:20:30 +01:00
|
|
|
Serial.begin(9600);
|
2013-10-21 09:58:40 +02:00
|
|
|
while (!Serial) {
|
2012-04-09 15:51:35 +02:00
|
|
|
; // wait for serial port to connect. Needed for Leonardo only
|
|
|
|
}
|
|
|
|
|
2012-04-02 15:07:58 +02:00
|
|
|
|
2010-11-29 21:20:30 +01:00
|
|
|
Serial.print("Initializing SD card...");
|
2013-10-21 09:58:40 +02:00
|
|
|
|
2010-11-29 21:20:30 +01:00
|
|
|
// see if the card is present and can be initialized:
|
|
|
|
if (!SD.begin(chipSelect)) {
|
|
|
|
Serial.println("Card failed, or not present");
|
|
|
|
// don't do anything more:
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Serial.println("card initialized.");
|
|
|
|
}
|
|
|
|
|
2015-07-06 15:18:33 +02:00
|
|
|
void loop() {
|
2010-11-29 21:20:30 +01:00
|
|
|
// make a string for assembling the data to log:
|
|
|
|
String dataString = "";
|
|
|
|
|
|
|
|
// read three sensors and append to the string:
|
|
|
|
for (int analogPin = 0; analogPin < 3; analogPin++) {
|
|
|
|
int sensor = analogRead(analogPin);
|
|
|
|
dataString += String(sensor);
|
|
|
|
if (analogPin < 2) {
|
2013-10-21 09:58:40 +02:00
|
|
|
dataString += ",";
|
2010-11-29 21:20:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-12-22 22:52:09 +01:00
|
|
|
// open the file. note that only one file can be open at a time,
|
|
|
|
// so you have to close this one before opening another.
|
|
|
|
File dataFile = SD.open("datalog.txt", FILE_WRITE);
|
2010-11-29 21:20:30 +01:00
|
|
|
|
|
|
|
// if the file is available, write to it:
|
|
|
|
if (dataFile) {
|
|
|
|
dataFile.println(dataString);
|
|
|
|
dataFile.close();
|
|
|
|
// print to the serial port too:
|
|
|
|
Serial.println(dataString);
|
2013-10-21 09:58:40 +02:00
|
|
|
}
|
2010-11-29 21:20:30 +01:00
|
|
|
// if the file isn't open, pop up an error:
|
|
|
|
else {
|
|
|
|
Serial.println("error opening datalog.txt");
|
2013-10-21 09:58:40 +02:00
|
|
|
}
|
2010-11-29 21:20:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|