77 lines
2.0 KiB
C++
77 lines
2.0 KiB
C++
/*
|
|
SD card basic file example
|
|
|
|
This example shows how to create and destroy an SD card file.
|
|
The circuit. Pin numbers reflect the default
|
|
SPI pins for Uno and Nano models:
|
|
SD card attached to SPI bus as follows:
|
|
** SDO - pin 11
|
|
** SDI - pin 12
|
|
** CLK - pin 13
|
|
** CS - depends on your SD card shield or module.
|
|
Pin 10 used here for consistency with other Arduino examples
|
|
(for MKR Zero SD: SDCARD_SS_PIN)
|
|
|
|
created Nov 2010
|
|
by David A. Mellis
|
|
modified 24 July 2020
|
|
by Tom Igoe
|
|
|
|
This example code is in the public domain.
|
|
*/
|
|
#include <SD.h>
|
|
|
|
const int chipSelect = 10;
|
|
File myFile;
|
|
|
|
void setup() {
|
|
// Open serial communications and wait for port to open:
|
|
Serial.begin(9600);
|
|
// wait for Serial Monitor to connect. Needed for native USB port boards only:
|
|
while (!Serial);
|
|
|
|
Serial.print("Initializing SD card...");
|
|
|
|
if (!SD.begin(chipSelect)) {
|
|
Serial.println("initialization failed. Things to check:");
|
|
Serial.println("1. is a card inserted?");
|
|
Serial.println("2. is your wiring correct?");
|
|
Serial.println("3. did you change the chipSelect pin to match your shield or module?");
|
|
Serial.println("Note: press reset button on the board and reopen this serial monitor after fixing your issue!");
|
|
while (1);
|
|
}
|
|
Serial.println("initialization done.");
|
|
|
|
if (SD.exists("example.txt")) {
|
|
Serial.println("example.txt exists.");
|
|
} else {
|
|
Serial.println("example.txt doesn't exist.");
|
|
}
|
|
|
|
// open a new file and immediately close it:
|
|
Serial.println("Creating example.txt...");
|
|
myFile = SD.open("example.txt", FILE_WRITE);
|
|
myFile.close();
|
|
|
|
// Check to see if the file exists:
|
|
if (SD.exists("example.txt")) {
|
|
Serial.println("example.txt exists.");
|
|
} else {
|
|
Serial.println("example.txt doesn't exist.");
|
|
}
|
|
|
|
// delete the file:
|
|
Serial.println("Removing example.txt...");
|
|
SD.remove("example.txt");
|
|
|
|
if (SD.exists("example.txt")) {
|
|
Serial.println("example.txt exists.");
|
|
} else {
|
|
Serial.println("example.txt doesn't exist.");
|
|
}
|
|
}
|
|
|
|
void loop() {
|
|
// nothing happens after setup finishes.
|
|
}
|