// Sample Program, sends data to SD card using Arduino Uno and Catalex card reader =====
// If you have trouble, check for cold solder joints, catalex boards are apparently not tested
// Second, note position of the line "File myfile" in the program, allows global access to resources
// Program reads time in ms since program began, outputs at one second intervals=======
#include <SPI.h>
#include <SD.h>
File myfile;
int a=0;
unsigned long time;
void setup()
{
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) { } ; // wait for serial port to connect. Needed for Leonardo only
Serial.print("Initializing SD card..."); // see if the card is present and can be initialized:
if (!SD.begin(9)) {
Serial.println("Card failed, or not present");
// don't do anything more:
while (1) ;
}
Serial.println("card initialized.");
// Open up the file we're going to log to!
myfile = SD.open("datalog.txt", FILE_WRITE);
if (! myfile) {
Serial.println("error opening datalog.txt");
// Wait forever since we cant write data
while (1) ;
}
}
void loop()
{ //================= Writing phase ===============
if (a<10){
time = millis();
Serial.print("Time: ");
Serial.println(time);
myfile.print("Time: ");
myfile.println(time);
delay(1000);} // wait a second so as not to send massive amounts of data
//======== closing phase ================
if (a==12){ myfile.close(); }
//========= done loop forever ==============
if (a>14) {a=14; Serial.println("Done"); delay(3000);}
a++;
}
|