Commit 9e057180 authored by Patrick's avatar Patrick

new sd demo

parent ac10376d
/*
SD card read/write
SD card read/write with Red-Peg
This example shows how to read and write data to and from an SD card file
The circuit:
......@@ -9,27 +9,24 @@
** CLK - pin 13
** CS - pin 4
created Nov 2010
by David A. Mellis
modified 9 Apr 2012
by Tom Igoe
This example code is in the public domain.
modified from sketch by
David A. Mellis & Tom Igoe
*/
#define SD_CS_PIN 5
#define SD_CS_PIN 5 //5
#include <SPI.h>
//#include <SD.h>
#include <SdFat.h>
#include <red_peg.h>
SdFat SD;
#include <red_peg.h>
red_peg RP;
File myFile;
void setup()
{
// Open serial communications and wait for port to open:
Serial.begin(115200);
Serial.begin(BAUD); // 115200
RP.begin();
Serial.print("Initializing SD card...");
// On the Ethernet Shield, CS is pin 4. It's set as an output by default.
......@@ -37,6 +34,7 @@ void setup()
// (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(SD_CS_PIN, OUTPUT);
if (!SD.begin(SD_CS_PIN)) {
Serial.println("initialization failed!");
......@@ -52,7 +50,6 @@ void setup()
if (myFile) {
Serial.print("Writing to test.txt...");
myFile.println("testing 1, 2, 3.");
delay(100);
// close the file:
myFile.close();
Serial.println("done.");
......@@ -64,8 +61,7 @@ void setup()
// re-open the file for reading:
myFile = SD.open("test.txt");
if (myFile) {
Serial.println("opening test.txt for reading");
Serial.println("File contains:");
Serial.println("test.txt:");
// read from the file until there's nothing else in it:
while (myFile.available()) {
......@@ -81,7 +77,34 @@ void setup()
void loop()
{
// nothing happens after setup
t_SensorData returned = RP.get(TMP);
if (returned.sensor == TMP) {
myFile = SD.open("readings.csv", FILE_WRITE);
if (myFile) {
Serial.print("Writing to vals.csv: ");
myFile.print(returned.y);
myFile.print("-");
myFile.print(returned.m);
myFile.print("-");
myFile.print(returned.d);
myFile.print("-");
myFile.print("T");
myFile.print(returned.hh);
myFile.print(":");
myFile.print(returned.mm);
myFile.print(":");
myFile.print(returned.ss);
myFile.print("Z, ");
myFile.print(returned.value);
myFile.println();
// close the file:
myFile.close();
// Then print what we sent to the Serial port
RP.print_data(returned);
Serial.println("done.");
delay(2000);
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment