Day20
//SD read and write
#include <SD.h>
#include <Wire.h>
#include <RTClib.h>
const int CS_PIN =10;
const int POW_PIN =8;
//Default rate of 5 seconds
int refresh_rate = 5000;
//defining an abject
RTC_DS1307 RTC;
String year, month, day, hour , minute , second, time , date;
void setup()
{
Serial.begin(9600);
Serial.println("Initializing Card");
//CS pin is an output
pinMode(CS_PIN, OUTPUT);
Wire.begin();
RTC.begin();
if (!RTC.isrunning())
{
Serial.println(F("RTC is not runnng"));
RTC.adjust(DateTime(__DATE__,__TIME__));
}
//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 information (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()
{
DateTime datetime = RTC.now();
year = String(datetime.year(), DEC);
month = String(datetime.month(), DEC);
day = String(datetime.day(), DEC);
hour = String(datetime.hour(), DEC);
minute = String(datetime.minute(), DEC);
second = String(datetime.second(), DEC);
long timeStamp = millis();
//concatenat string \
date = year + "/" + month + "/" + day;
time = hour + ":" + minute + ":" + second;
String dataString = "Hello There!";
//Open a file and write to it.
File dataFile = SD.open("log.csv", FILE_WRITE);
if (dataFile)
{
dataFile.print(date);
dataFile.print(F(","));
dataFile.println(time);
dataFile.print(F(","));
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(date);
Serial.print(F(","));Serial.println(time);
Serial.print(F(","));Serial.println(dataString);
}
else
{
Serial.println("Couldn't open log file");
}
delay(refresh_rate);
}
The code above is the sd card reader with the real time clock.
The code was created with half of the class trying to debug the code so this one hsould work correctly.
#include <SD.h>
#include <Wire.h>
#include <RTClib.h>
const int CS_PIN =10;
const int POW_PIN =8;
//Default rate of 5 seconds
int refresh_rate = 5000;
//defining an abject
RTC_DS1307 RTC;
String year, month, day, hour , minute , second, time , date;
void setup()
{
Serial.begin(9600);
Serial.println("Initializing Card");
//CS pin is an output
pinMode(CS_PIN, OUTPUT);
Wire.begin();
RTC.begin();
if (!RTC.isrunning())
{
Serial.println(F("RTC is not runnng"));
RTC.adjust(DateTime(__DATE__,__TIME__));
}
//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 information (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()
{
DateTime datetime = RTC.now();
year = String(datetime.year(), DEC);
month = String(datetime.month(), DEC);
day = String(datetime.day(), DEC);
hour = String(datetime.hour(), DEC);
minute = String(datetime.minute(), DEC);
second = String(datetime.second(), DEC);
long timeStamp = millis();
//concatenat string \
date = year + "/" + month + "/" + day;
time = hour + ":" + minute + ":" + second;
String dataString = "Hello There!";
//Open a file and write to it.
File dataFile = SD.open("log.csv", FILE_WRITE);
if (dataFile)
{
dataFile.print(date);
dataFile.print(F(","));
dataFile.println(time);
dataFile.print(F(","));
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(date);
Serial.print(F(","));Serial.println(time);
Serial.print(F(","));Serial.println(dataString);
}
else
{
Serial.println("Couldn't open log file");
}
delay(refresh_rate);
}
The code above is the sd card reader with the real time clock.
The code was created with half of the class trying to debug the code so this one hsould work correctly.

Comments
Post a Comment