/* SD card write This example shows how to read and write data to and from an SD card file The circuit: SD card attached to SPI bus as follows: ** MOSI - pin 11 ** MISO - pin 12 ** CLK - pin 13 created Nov 2010 by David A. Mellis modified 9 Apr 2012 by Tom Igoe This example code is in the public domain. March 2018 - Modified Gord Payne Read from 3 resistors on A0,A1,A2 Write the values to file, tab-delimited */ #include //#include #include "SdFat.h" SdFat SD; #define SD_CS_PIN SS int sen1 = A0; int sen2 = A1; int sen3 = A2; int r1Val = 0; int r2Val = 0; int r3Val = 0; int i = 1; File myFile; void setup() { // Open serial communications and wait for port to open: Serial.begin(9600); while (!Serial) { ; // wait for serial port to connect. Needed for native USB port only } Serial.print("Initializing SD card..."); if (!SD.begin(SD_CS_PIN)) { Serial.println("initialization failed!"); return; } Serial.println("initialization done."); // open the file. note that only one file can be open at a time, // so you have to close this one before opening another. myFile = SD.open("datalog.txt", FILE_WRITE); } void loop() { delay(1000); 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) { dataString += "\t"; // add tab delimiter } } // if the file is available, write to it: if (myFile) { myFile.println(dataString); // print to the serial port too: Serial.print("line "); Serial.print("\t"); Serial.print(i); Serial.print("\t"); Serial.println(dataString); i++; } // if the file isn't open, pop up an error: else { Serial.println("error opening datalog.txt"); } if (Serial.available()) { char theVal = Serial.read(); if (theVal == 'q') myFile.close(); } } // after 'q', the file isn't open anymore, you'l get file open error