Okay so here is a little background on my project: I am building a data logger, complete with sd card, a real time clock, and a few analog sensors. I initially got my project working, but after a while it would stop writing to the sd card and I presume it was because I was using String objects to make write the timestamp and sensor data to the sd card all at once.
Now I am trying to change it to use c strings and not Strings to prevent the RAM from filling up? (I read that somewhere)
Here is my code:
#include <SPI.h>
#include <Wire.h>
#include <RTClib.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <SD.h>
#define LED 13
#define DS3231_I2C_ADDRESS 0x68
#define ONE_WIRE_BUS 2
File myFile;
OneWire ourWire(ONE_WIRE_BUS);
DallasTemperature sensors(&ourWire);
DeviceAddress thermometer = { 0x28, 0xDC, 0x90, 0xE0, 0x08, 0x00, 0x00, 0x7C };
// Convert normal decimal numbers to binary coded decimal
byte decToBcd(byte val)
{
return ( (val / 10 * 16) + (val % 10) );
}
// Convert binary coded decimal to normal decimal numbers
byte bcdToDec(byte val)
{
return ( (val / 16 * 10) + (val % 16) );
}
void setDS3231time(byte second, byte minute, byte hour, byte dayOfWeek, byte
dayOfMonth, byte month, byte year)
{
// sets time and date data to DS3231
Wire.beginTransmission(DS3231_I2C_ADDRESS);
Wire.write(0); // set next input to start at the seconds register
Wire.write(decToBcd(second)); // set seconds
Wire.write(decToBcd(minute)); // set minutes
Wire.write(decToBcd(hour)); // set hours
Wire.write(decToBcd(dayOfWeek)); // set day of week (1=Sunday, 7=Saturday)
Wire.write(decToBcd(dayOfMonth)); // set date (1 to 31)
Wire.write(decToBcd(month)); // set month
Wire.write(decToBcd(year)); // set year (0 to 99)
Wire.endTransmission();
}
void setup() {
pinMode(LED, OUTPUT);
digitalWrite(LED, HIGH);
Serial.begin(9600);
Wire.begin();
while (!Serial) {
;
}
// set the initial time here:
// DS3231 seconds, minutes, hours, day, month, day of month, year
//setDS3231time(0, 48, 10, 5, 13, 7, 17);
pinMode(LED, OUTPUT);
Serial.println("It should work");
delay(1000);
byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
// retrieve data from DS3231
readDS3231time(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month,
&year);
char TimeDate[12];
sprintf (TimeDate, "%02u/%20u %20u:%20u", month, dayOfMonth, hour, minute);
for(int i = 0; i < 12; i++)
{
Serial.println(TimeDate[i]);
}
delay(5000);
digitalWrite(LED, LOW);
} /*----end setup---*/
void loop() {
}
void readDS3231time(byte *second,
byte *minute,
byte *hour,
byte *dayOfWeek,
byte *dayOfMonth,
byte *month,
byte *year)
{
Wire.beginTransmission(DS3231_I2C_ADDRESS);
Wire.write(0); // set DS3231 register pointer to 00h
Wire.endTransmission();
Wire.requestFrom(DS3231_I2C_ADDRESS, 7);
// request seven bytes of data from DS3231 starting from register 00h
*second = bcdToDec(Wire.read() & 0x7f);
*minute = bcdToDec(Wire.read());
*hour = bcdToDec(Wire.read() & 0x3f);
*dayOfWeek = bcdToDec(Wire.read());
*dayOfMonth = bcdToDec(Wire.read());
*month = bcdToDec(Wire.read());
*year = bcdToDec(Wire.read());
}
/*
}
I've tried to strip my program down and focus on getting the time stamp "MM/DD/YY HH:MM" working.
The above code compiles fine, lights up the led to begin, prints my "this should work statement", SKIPS the timestamp print, and then shut off the LED. It appears to just skip right over the sprintf() statement.
Any wizards out there want to brutally explain what is wrong with my code?