| /* | /* | ||||
| 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 using the SD library. | to an SD card using the SD library. | ||||
| The circuit: | The circuit: | ||||
| by Tom Igoe | by Tom Igoe | ||||
| This example code is in the public domain. | This example code is in the public domain. | ||||
| */ | */ | ||||
| #include <SD.h> | #include <SD.h> | ||||
| //UNCOMMENT THESE TWO LINES FOR TEENSY AUDIO BOARD: | //UNCOMMENT THESE TWO LINES FOR TEENSY AUDIO BOARD: | ||||
| //SPI.setMOSI(7); // Audio shield has MOSI on pin 7 | //SPI.setMOSI(7); // Audio shield has MOSI on pin 7 | ||||
| //SPI.setSCK(14); // Audio shield has SCK on pin 14 | //SPI.setSCK(14); // Audio shield has SCK on pin 14 | ||||
| // Open serial communications and wait for port to open: | |||||
| // Open serial communications and wait for port to open: | |||||
| Serial.begin(9600); | Serial.begin(9600); | ||||
| while (!Serial) { | |||||
| while (!Serial) { | |||||
| ; // wait for serial port to connect. | ; // wait for serial port to connect. | ||||
| } | } | ||||
| Serial.print("Initializing SD card..."); | Serial.print("Initializing SD card..."); | ||||
| // see if the card is present and can be initialized: | // see if the card is present and can be initialized: | ||||
| if (!SD.begin(chipSelect)) { | if (!SD.begin(chipSelect)) { | ||||
| Serial.println("Card failed, or not present"); | Serial.println("Card failed, or not present"); | ||||
| // don't do anything more: | |||||
| return; | |||||
| while (1) { | |||||
| // No SD card, so don't do anything more - stay stuck here | |||||
| } | |||||
| } | } | ||||
| Serial.println("card initialized."); | Serial.println("card initialized."); | ||||
| } | } | ||||
| int sensor = analogRead(analogPin); | int sensor = analogRead(analogPin); | ||||
| dataString += String(sensor); | dataString += String(sensor); | ||||
| if (analogPin < 2) { | if (analogPin < 2) { | ||||
| dataString += ","; | |||||
| dataString += ","; | |||||
| } | } | ||||
| } | } | ||||
| dataFile.close(); | dataFile.close(); | ||||
| // print to the serial port too: | // print to the serial port too: | ||||
| Serial.println(dataString); | Serial.println(dataString); | ||||
| } | |||||
| // if the file isn't open, pop up an error: | |||||
| else { | |||||
| } else { | |||||
| // if the file isn't open, pop up an error: | |||||
| Serial.println("error opening datalog.txt"); | Serial.println("error opening datalog.txt"); | ||||
| } | |||||
| } | |||||
| delay(100); // run at a reasonable not-too-fast speed | |||||
| } | } | ||||