SD card : read & write

//Write to SD card
#include  <SD.h>
//Set by default for the SD card library
//MOSI = pin 11
//MISO = pin 12
//SCLK = pin 13
//We always need to set the CS Pin
const int CS_PIN = 10;
void setup()
              {
Serial.begin(9600);
Serial.println("Initializing Card");
//CS pin is an output
pinMode(CS_PIN, OUTPUT);
if (!SD.begin(CS_PIN))
                        {
                          Serial.println("Card Failure");
                          return;
                          }
Serial.println("Card Ready");
                }

void loop()
           {
long timeStamp = millis();
String dataString = "Hello There!";

 //Open a file and write to it.
File dataFile = SD.open("log.csv", FILE_WRITE);
if (dataFile)
              {
                dataFile.print(timeStamp);
                dataFile.print(",");
                dataFile.println(dataString);
                dataFile.close(); //Data isn't actually written until we close the connection!
             
                //Print same thing to the screen for debugging
                Serial.print(timeStamp);
                Serial.print(",");
                Serial.println(dataString);
                  }
else
    {
      Serial.println("Couldn't open log file");
    }
            delay(5000);
            }
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//SD Read and Writw
#include  <SD.h>
//Set by default for the SD card library
//MOSI = pin 11
//MISO = pin 12
//SCLK = pin 13
//We always need to set the CS Pin
const int CS_PIN = 10;
const int POW_PIN = 8;
int refresh_rate = 5000;

void setup()
{
Serial.begin(9600);
Serial.println("Initializing Card");
//CS pin is an output
pinMode(CS_PIN, OUTPUT);

//card will draw power from pin 8, so set it high
pinMode(POW_PIN, OUTPUT);
digitalWrite(POW_PIN, HIGH);

if (!SD.begin(CS_PIN))
                        {
                          Serial.println("Card Failure");
                          return;
                         }
Serial.println("Card Ready");

//read the configuration info (speed.txt
File commandFile = SD.open("speed.txt");

if (commandFile)
                        {
                        Serial.println("Reading Command File");

                        while(commandFile.available())
                              {
                                refresh_rate = commandFile.parseInt();
                              }
                       
                     Serial.print("Refresh Rate = ");
                     Serial.print(refresh_rate);
                     Serial.println("ms");
                     commandFile.close(); //close the file when finished
                        } 
else{
                     Serial.println("could not read command File.");
                     return;
                     }
 }

void loop()
           {
long timeStamp = millis();
String dataString = "Hello There!";

 //Open a file and write to it.
File dataFile = SD.open("log.csv", FILE_WRITE);
if (dataFile)
              {
                dataFile.print(timeStamp);
                dataFile.print(",");
                dataFile.println(dataString);
                dataFile.close(); //Data isn't actually written until we close the connection!
               
                //Print same thing to the screen for debugging
                Serial.print(timeStamp);
                Serial.print(",");
                Serial.println(dataString);
                  }
else
    {
      Serial.println("Couldn't open log file");
    }
            delay(refresh_rate);
            }

Comments

Popular posts from this blog

pointers code no lb

real time & EEPROM lab

interrup and sound lab