Permalink
Browse files

can use with leonardo, fixed examples

  • Loading branch information...
ladyada committed Aug 6, 2012
1 parent bd7b667 commit b63cc4db77b8f2f75f390019f4c67204c78c6186
@@ -6,15 +6,17 @@
Very useful for testing a card when you're not sure whether its working or not.
The circuit:
- * SD card attached to SPI bus as follows:
- ** MOSI - pin 11 on Arduino Uno/Duemilanove/Diecimila
- ** MISO - pin 12 on Arduino Uno/Duemilanove/Diecimila
- ** CLK - pin 13 on Arduino Uno/Duemilanove/Diecimila
- ** CS - depends on your SD card shield or module
+ * SD card attached to SPI bus as follows:
+ ** UNO: MOSI - pin 11, MISO - pin 12, CLK - pin 13, CS - pin 4 (CS pin can be changed)
+ and pin #10 (SS) must be an output
+ ** Mega: MOSI - pin 51, MISO - pin 50, CLK - pin 52, CS - pin 4 (CS pin can be changed)
+ and pin #52 (SS) must be an output
+ ** Leonardo: Connect to hardware SPI via the ICSP header
+ Pin 4 used here for consistency with other Arduino examples
- created 28 Mar 2011
- by Limor Fried
+ created 28 Mar 2011 by Limor Fried
+ modified 9 Apr 2012 by Tom Igoe
*/
// include the SD library:
#include <SD.h>
@@ -32,27 +34,30 @@ const int chipSelect = 4;
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("\nInitializing SD card...");
// On the Ethernet Shield, CS is pin 4. It's set as an output by default.
// Note that even if it's not used as the CS pin, the hardware SS pin
// (10 on most Arduino boards, 53 on the Mega) must be left as an output
// or the SD library functions will not work.
- pinMode(10, OUTPUT); // change this to 53 on a mega
+ pinMode(SS, OUTPUT);
// we'll use the initialization code from the utility libraries
// since we're just testing if the card is working!
- if (!card.init(SPI_HALF_SPEED, chipSelect)) {
+ while (!card.init(SPI_HALF_SPEED, chipSelect)) {
Serial.println("initialization failed. Things to check:");
Serial.println("* is a card is inserted?");
Serial.println("* Is your wiring correct?");
Serial.println("* did you change the chipSelect pin to match your shield or module?");
- return;
- } else {
- Serial.println("Wiring is correct and a card is present.");
- }
-
+ }
+
// print the type of card
Serial.print("\nCard type: ");
switch(card.type()) {
@@ -5,16 +5,16 @@
to an SD card using the SD library.
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
+ ** UNO: MOSI - pin 11, MISO - pin 12, CLK - pin 13, CS - pin 4 (CS pin can be changed)
+ and pin #10 (SS) must be an output
+ ** Mega: MOSI - pin 51, MISO - pin 50, CLK - pin 52, CS - pin 4 (CS pin can be changed)
+ and pin #52 (SS) must be an output
+ ** Leonardo: Connect to hardware SPI via the ICSP header
+ Pin 4 used here for consistency with other Arduino examples
created 24 Nov 2010
- updated 2 Dec 2010
- by Tom Igoe
+ modified 9 Apr 2012 by Tom Igoe
This example code is in the public domain.
@@ -28,21 +28,37 @@
// functions will not work.
const int chipSelect = 4;
+File dataFile;
+
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...");
// make sure that the default chip select pin is set to
// output, even if you don't use it:
- pinMode(10, OUTPUT);
+ pinMode(SS, OUTPUT);
// 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;
+ while (1) ;
}
Serial.println("card initialized.");
+
+ // Open up the file we're going to log to!
+ dataFile = SD.open("datalog.txt", FILE_WRITE);
+ if (! dataFile) {
+ Serial.println("error opening datalog.txt");
+ // Wait forever since we cant write data
+ while (1) ;
+ }
}
void loop()
@@ -59,21 +75,21 @@ void loop()
}
}
- // 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);
-
- // if the file is available, write to it:
- if (dataFile) {
- dataFile.println(dataString);
- dataFile.close();
- // print to the serial port too:
- Serial.println(dataString);
- }
- // if the file isn't open, pop up an error:
- else {
- Serial.println("error opening datalog.txt");
- }
+ dataFile.println(dataString);
+
+ // print to the serial port too:
+ Serial.println(dataString);
+
+ // The following line will 'save' the file to the SD card after every
+ // line of data - this will use more power and slow down how much data
+ // you can read but it's safer!
+ // If you want to speed up the system, remove the call to flush() and it
+ // will save the file only every 512 bytes - every time a sector on the
+ // SD card is filled with data.
+ dataFile.flush();
+
+ // Take 1 measurement every 500 milliseconds
+ delay(500);
}
View
@@ -4,14 +4,15 @@
This example shows how to create and destroy an SD card file
The circuit:
* SD card attached to SPI bus as follows:
- ** MOSI - pin 11
- ** MISO - pin 12
- ** CLK - pin 13
- ** CS - pin 4
+ ** UNO: MOSI - pin 11, MISO - pin 12, CLK - pin 13, CS - pin 4 (CS pin can be changed)
+ and pin #10 (SS) must be an output
+ ** Mega: MOSI - pin 51, MISO - pin 50, CLK - pin 52, CS - pin 4 (CS pin can be changed)
+ and pin #52 (SS) must be an output
+ ** Leonardo: Connect to hardware SPI via the ICSP header
created Nov 2010
by David A. Mellis
- updated 2 Dec 2010
+ modified 9 Apr 2012
by Tom Igoe
This example code is in the public domain.
@@ -21,17 +22,29 @@
File myFile;
+// change this to match your SD shield or module;
+// Arduino Ethernet shield: pin 4
+// Adafruit SD shields and modules: pin 10
+// Sparkfun SD shield: pin 8
+const int chipSelect = 4;
+
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...");
// On the Ethernet Shield, CS is pin 4. It's set as an output by default.
// Note that even if it's not used as the CS pin, the hardware SS pin
// (10 on most Arduino boards, 53 on the Mega) must be left as an output
// or the SD library functions will not work.
- pinMode(10, OUTPUT);
+ pinMode(SS, OUTPUT);
- if (!SD.begin(4)) {
+ if (!SD.begin(chipSelect)) {
Serial.println("initialization failed!");
return;
}
@@ -4,15 +4,15 @@
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
- ** CS - pin 4
+ ** UNO: MOSI - pin 11, MISO - pin 12, CLK - pin 13, CS - pin 4 (CS pin can be changed)
+ and pin #10 (SS) must be an output
+ ** Mega: MOSI - pin 51, MISO - pin 50, CLK - pin 52, CS - pin 4 (CS pin can be changed)
+ and pin #52 (SS) must be an output
+ ** Leonardo: Connect to hardware SPI via the ICSP header
+
- created Nov 2010
- by David A. Mellis
- updated 2 Dec 2010
- by Tom Igoe
+ created Nov 2010 by David A. Mellis
+ modified 9 Apr 2012 by Tom Igoe
This example code is in the public domain.
@@ -22,17 +22,29 @@
File myFile;
+// change this to match your SD shield or module;
+// Arduino Ethernet shield: pin 4
+// Adafruit SD shields and modules: pin 10
+// Sparkfun SD shield: pin 8
+const int chipSelect = 4;
+
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...");
// On the Ethernet Shield, CS is pin 4. It's set as an output by default.
// Note that even if it's not used as the CS pin, the hardware SS pin
// (10 on most Arduino boards, 53 on the Mega) must be left as an output
// or the SD library functions will not work.
- pinMode(10, OUTPUT);
+ pinMode(SS, OUTPUT);
- if (!SD.begin(4)) {
+ if (!SD.begin(chipSelect)) {
Serial.println("initialization failed!");
return;
}
@@ -4,15 +4,15 @@
This example shows how to create and destroy an SD card file
The circuit:
* SD card attached to SPI bus as follows:
- ** MOSI - pin 11
- ** MISO - pin 12
- ** CLK - pin 13
- ** CS - pin 4
-
- created Nov 2010
- by David A. Mellis
- updated 2 Dec 2010
- by Tom Igoe
+ ** UNO: MOSI - pin 11, MISO - pin 12, CLK - pin 13, CS - pin 4 (CS pin can be changed)
+ and pin #10 (SS) must be an output
+ ** Mega: MOSI - pin 51, MISO - pin 50, CLK - pin 52, CS - pin 4 (CS pin can be changed)
+ and pin #52 (SS) must be an output
+ ** Leonardo: Connect to hardware SPI via the ICSP header
+
+ created Nov 2010 by David A. Mellis
+ modified 9 Apr 2012 by Tom Igoe
+ modified 13 June 2012 by Limor Fried
This example code is in the public domain.
@@ -21,17 +21,29 @@
File root;
+// change this to match your SD shield or module;
+// Arduino Ethernet shield: pin 4
+// Adafruit SD shields and modules: pin 10
+// Sparkfun SD shield: pin 8
+const int chipSelect = 4;
+
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...");
// On the Ethernet Shield, CS is pin 4. It's set as an output by default.
// Note that even if it's not used as the CS pin, the hardware SS pin
- // (10 on most Arduino boards, 53 on the Mega) must be left as an output
+ // (10 on Arduino Uno boards, 53 on the Mega) must be left as an output
// or the SD library functions will not work.
- pinMode(10, OUTPUT);
+ pinMode(SS, OUTPUT);
- if (!SD.begin(10)) {
+ if (!SD.begin(chipSelect)) {
Serial.println("initialization failed!");
return;
}
@@ -50,18 +62,22 @@ void loop()
}
void printDirectory(File dir, int numTabs) {
- while(true) {
-
+ // Begin at the start of the directory
+ dir.rewindDirectory();
+
+ while(true) {
File entry = dir.openNextFile();
if (! entry) {
// no more files
//Serial.println("**nomorefiles**");
break;
}
for (uint8_t i=0; i<numTabs; i++) {
- Serial.print('\t');
+ Serial.print('\t'); // we'll have a nice indentation
}
+ // Print the 8.3 name
Serial.print(entry.name());
+ // Recurse for directories, otherwise print the file size
if (entry.isDirectory()) {
Serial.println("/");
printDirectory(entry, numTabs+1);
@@ -70,6 +86,7 @@ void printDirectory(File dir, int numTabs) {
Serial.print("\t\t");
Serial.println(entry.size(), DEC);
}
+ entry.close();
}
}
Oops, something went wrong.

0 comments on commit b63cc4d

Please sign in to comment.