the battery in the rtc is a new one, it works but after 4 hours it stopped logging data to the sd card. is there something in the code that would do that? Here is the schematic
// mosi = 11
//miso = 12
//clk = 13
//cs = 10
//rtc data pin 7
//rtc clk pin 6
// rst =8
//rtc
#include <SD.h>
#include <SPI.h>
#include <virtuabotixRTC.h>
virtuabotixRTC myRTC (6, 7, 8);
File myFile;
const int chipSelect = 10;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
//sec, min, day of week, day of month, month, year
myRTC.setDS1302Time(00, 21, 15, 4, 1, 8, 2024);
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);
myRTC.updateTime();
myFile.print("Current Date / Time: "); //|
myFile.print(myRTC.dayofmonth); //|
myFile.print("/"); //|
myFile.print(myRTC.month); //|
myFile.print("/"); //|
myFile.print(myRTC.year); //|
myFile.print(" "); //|
myFile.print(myRTC.hours); //|
myFile.print(":"); //|
myFile.print(myRTC.minutes); //|
myFile.print(":"); //|
myFile.println(myRTC.seconds); //|
myFile.write(" ");
int A0 = analogRead(A0);
myFile.print("LDR =");
myFile.print(A0);
myFile.write ("\n"); //new line
myFile.close();
Serial.println(A0);
delay(1000);
}
That sounds about right I tried it again today for a couple minutes and no data was logged. Would it work if I opened the file in setup and wrote to it in loop?
Maybe you should check if the file was oppened successfully before proceed? If not print something and stay there so you will know it's not openned correctly
myFile = SD.open("lightS", FILE_WRITE);
if(myFile){
//do your logging here...
}
else{
Serial.println("file FAILED to open");
while(1);
}
Did you deal with the close as well? Are you wanting to write the same file over and over, or do you want to append the data. In other words is it simulating a tape drive or a disk drive.
#include <SD.h>// mosi = 11//miso = 12//clk = 13//cs = 10
#include <SPI.h>
#include <virtuabotixRTC.h> //https://github.com/chrisfryer78/ArduinoRTClibrary/tree/master
virtuabotixRTC myRTC (6, 7, 8); //clk, data, rst
void setup() {
Serial.begin(115200);
//sec, min, day of week, day of month, month, year
//myRTC.setDS1302Time(00, 21, 15, 4, 1, 8, 2024); if battery inserted you have need to set same time every start of arduino
while (!Serial);
Serial.print("SD Ready");
if (!SD.begin(10)) {
Serial.println("SD Card Failed");
return;
}
Serial.println("SD Good");
Serial.end(); // no serial need, only for start
}
void loop() {
static char Text[10][30]; // i am not sure if attribut static is really need
static byte index = 0;
myRTC.updateTime();
int readA0 = analogRead(A0);
sprintf(Text[index], "%02u/%02u/%02u %02u:%02u:%02u\t%u\n", myRTC.year, myRTC.month, myRTC.dayofmonth, myRTC.hours, myRTC.minutes, myRTC.seconds, readA0);
index++;
if (index == 10) {
index = 0;
File myFile = SD.open("lightS" + String(myRTC.hours), FILE_WRITE);
for (int i = 0; i < 10; i++) myFile.print(Text[i]);
myFile.close();
}
delay(1000);
}
I'm not sure what you mean by tape or disk drive, but what I'm wanting is a list of data like that below from the ldr like this, this is also what printed before it stopped.
Time 23;54:00 LDR = 5
Time 23:55:00 LDR = 5
And with the delay in the main loop, it won't be logging exactly every second because executing all those instructions takes time.
Maybe you want to use a timer for exact 1 second logging.
Opening a file, writing a tiny bit of data, then closing the file is a common beginner mistake.
This vastly increases the SD card power draw, vastly increases the error rate, and vastly decreases the writing speed. Experienced users open the file once in setup(), write data until finished, then close the file and stop the program.
storage for 10 messages. it will be copied to SD every 10 seconds in file with number of hour at the end of its name, so it will be 24 files total, just to be sure you writing not in same place on SD card.
#include <SD.h>// mosi = 11//miso = 12//clk = 13//cs = 10
#include <SPI.h>
#include <virtuabotixRTC.h> //https://github.com/chrisfryer78/ArduinoRTClibrary/tree/master
virtuabotixRTC myRTC (6, 7, 8); //clk, data, rst
void setup() {
Serial.begin(115200);
//sec, min, day of week, day of month, month, year
//myRTC.setDS1302Time(00, 21, 15, 4, 1, 8, 2024); if battery inserted you have need to set same time every start of arduino
while (!Serial);
Serial.print("SD Ready");
if (!SD.begin(10)) {
Serial.println("SD Card Failed");
return;
}
Serial.println("SD Good");
Serial.end(); // no serial need, only for start
}
void loop() {
const int Period = 1000; // millisec between readings
const byte Num = 10; // how much lines collect before store to SD
static char Text[Num ][30]; // i am not sure if attribut static is really need
static byte index = 0;
static unsigned long oldMillis = millis();
if (millis() - oldMillis >= Period ) {
oldMillis += Period ;
myRTC.updateTime();
int readA0 = analogRead(A0);
sprintf(Text[index], "%02u/%02u/%02u %02u:%02u:%02u\t%u\n", myRTC.year, myRTC.month, myRTC.dayofmonth, myRTC.hours, myRTC.minutes, myRTC.seconds, readA0);
index++;
if (index == Num ) {
index = 0;
File myFile = SD.open("lightS" + String(myRTC.hours % 6), FILE_WRITE); // 6 files total
for (int i = 0; i < Num ; i++) myFile.print(Text[i]);
myFile.close();
}
}
}
placeholder where unsigned number will showed with 2 digits and 0 if needed, after that is symbol '/'. not forget that whole thing is a literal "%02u/" and we know it will ends with invisible symbol 0 ('\0'), it is a terminator, end of line.
know any youtube channels i can learn this from? thanks just tried the code works great. ill check on it tomorrow to see if it has the same problem as i did.