the code bellow works but prints zeros instead of date and time. It reads the sensor and prints it well enough. Sensor is an ldr using the ds1307. rtc pins are data = 7, clk = 6, rst = 8, gnd = gnd and the ldr, vcc = 3.3v ( i tried 5v same deal), the example will show a year and weekday, rest zeros.
// mosi = 11
//miso = 12
//clk = 13
//cs = 10
//rtc data pin 7
//rtc clk pin 6
#include <SD.h>
#include <SPI.h>
#include <RTCDS1307.h>
RTCDS1307 rtc(0x68);
uint8_t hour, minute, second, day, weekday, month, year;
String m[12] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
String w[7] = {"Mon", "Tues", "Wed", "Thurs", "Fri", "Sat", "Sun"};
File myFile;
const int chipSelect = 10;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
rtc.begin();
rtc.setDate(7, 31, 24);
rtc.setTime(8,30, 00);
while(!Serial) {
;
}
Serial.print("SD Ready");
if(!SD.begin(chipSelect)) {
Serial.println("SD Card Failed");
return;
}
Serial.println("SD Good");
}
void loop() {
// put your main code here, to run repeatedly:
myFile = SD.open("lightS", FILE_WRITE);
rtc.getDate(year, month, day, weekday);
rtc.getTime(hour, minute, second);
if (!(second % 3)) rtc.setMode(1 - rtc.getMode());
rtc.getTime(hour, minute, second);
myFile.print(hour);
myFile.print('-');
myFile.print(minute);
myFile.print('-');
myFile.print(second);
myFile.print(' ');
myFile.print(day);
myFile.print('-');
myFile.print(weekday);
myFile.print(':');
myFile.print(month);
myFile.print(':');
myFile.print(year);
myFile.write(" ");
int A0 = analogRead(A0);
myFile.print("LDR =");
myFile.print(A0);
myFile.write ("\n"); //new line
myFile.close();
delay(1000);
}