My home automation controller has been evolving in the past 5 years and I am now migrating from a MEGA to a zero based system.
After migration i have also added a watchdog function based on the sdtZero library.
For some reason the program resets about once every 24h and after ages of debugging I was able to norrow down the problem in the following function used to log events on an SD card.
So, in other words making EEPROM.read(memLog)=0, the program runs for days with no problem (just like it did on a Mega), otherwise it resets about once every 24h (random times)
bool logSDCard(char *logData, byte addTime){
// addTine ==2 -> include time
// addTine ==0 -> no time add new line
// addTine ==1 -> no time no new line
// show line local & telnet
// Log to SD card
// exit if logging is disabled or Card not valid
if (SDCARDValid==0 || EEPROM.read(memLog)<1 ) { return false; } // logging disabled
selectSDcard();
File dataFile = SD.open(filename, FILE_WRITE);
if (dataFile) { // if the file is available, write to it:
if (addTime==2 || addTime==0) {dataFile.println();}
if (addTime==2) {dataFile.print(timenow());}
dataFile.print (logData);
dataFile.close();
selectEthernet();
return true;
}
else { // if the file isn't open, pop up an error:
digitalWrite (ErrorLed,HIGH);
printMessage(PORT_S,5);
dataFile.close();
selectEthernet();
return false;
}
}
Of course this is not thew complete program which is huge anyway to post here or for anyone to follow, but I thought I d ask for any tips on finding the bug anyway.
Probably one of the used functions is the problem: selectSDcard(), timenow(), selectEthernet(), printMessage(), or logData is missing a zero-terminator.
If opening a new file fails, then you don't have to close it.
Could you try to make the code look better ? Put every space, every comma, every new line at the right place. Use the same style throughout the whole sketch.
Every time the function runs, you are allocating 25 bytes of memory which you never free afterwards, eventually you will run out of memory.
< edit > The variable year in the first if statement should probably not be byte, unless year is always 255 or less.
Are the ethernetSelectPin and SD_CARD_CD_DIO the "SS" or "ChipSelect" pins of the SPI bus ? Then you don't have to do that.
A sketch of more than 6000 lines is long. Can you show us the sketch ? We could ask 1200 times and get 5 lines each time, but that will take longer than a year
It is possible to do something wrong with arrays or timing, without getting into problems. Running the same sketch on a different board might bring those things to the surface.
It is part of the SPI interface and it is inside the libraries.
Do you know a sketch that uses both Ethernet and the SD card that has to select the SPI ChipSelect in the sketch ?
Thanks for pointing this out!
I am using two separate shields for SD card and ethernet .
How do you define then two CS pins to be compatible with the SPI lib?
Well, the sketch is in excess of 5000 lines and spans in 21 tabs (files).
I just thought that no one would ever spend the time to go through it and that’s why I firstly narrowed down the bug within a particular function before asking for help in the forum.
If however you are still willing to make the effort to read it, I would be more than grateful and will post the full sketch, zipped.