Skip to content

Instantly share code, notes, and snippets.

@mdmunir
Created December 19, 2017 23:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mdmunir/5950f03b82d0ba576f6532f0424b86b3 to your computer and use it in GitHub Desktop.
Save mdmunir/5950f03b82d0ba576f6532f0424b86b3 to your computer and use it in GitHub Desktop.
/*
SD card read/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
** CS - pin 4 (for MKRZero SD: SDCARD_SS_PIN)
created Nov 2010
by David A. Mellis
modified 9 Apr 2012
by Tom Igoe
This example code is in the public domain.
*/
#include <SPI.h>
#include <SD.h>
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(4)) {
Serial.println("initialization failed!");
return;
}
Serial.println("initialization done.");
myFile = SD.open("test.txt");
if (myFile) {
Serial.println("test.txt:");
String sbuff="";
int value, total=0;
char c;
while (myFile.available()) {
c = myFile.read();
if(c == '\n'){
value = sbuff.toInt();
total += value;
sbuff = "";
Serial.print("value: ");
Serial.println(value);
}else{
sbuff += c;
}
}
// close the file:
myFile.close();
Serial.print("\ntotal: ");
Serial.println(total);
} else {
// if the file didn't open, print an error:
Serial.println("error opening test.txt");
}
}
void loop() {
// nothing happens after setup
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment