Hi, I was having issues with SD card initializing and now it works but when monitoring, it is able to initialize the card just gets stuck on reading saved configuration. Is there anything in this code (not ALL code for brevity's sake) that you think could be leading to this issue? It is not showing anything on the adafruit touchscreen even though IDE says done uploading. Happy to elaborate if needed, and thanks so much for any help!
bool initSDCard() {
if (!SD.begin(chipSelect)) {
#ifdef DEBUGGING
Serial.println("[SD Card] failed, or not present");
#endif
isSDCard_Working = false;
}
else{
#ifdef DEBUGGING
Serial.println("[SD Card] initialized.");
#endif
isSDCard_Working = true;
}
return isSDCard_Working;
}
String readFile(const char *filename) {
file = SD.open(filename);
String filedata = "";
if(file) {
filedata = file.readStringUntil('\n');
}
else {
file = SD.open(filename, FILE_WRITE);
}
file.close();
return filedata;
}
bool readSavedConfiguration() {
#ifdef DEBUGGING
Serial.println("[EEPROM] Reading saved configuration");
#endif
String conf = readFile(CONFIG_FILE); //average days, period state, total logs
if(conf != "") {
int index1 = 0;
int index2 = 0;
index1 = conf.indexOf(',', 0);
AveragePeriodDuration = (conf.substring(0, index1)).toInt();
index2 = conf.indexOf(',', index1+1);
int ps = (conf.substring(index1+1, index2)).toInt();
index1 = conf.indexOf(',', index2+1);
LogId = (conf.substring(index2+1, index1)).toInt();
conf = readFile(CURRENT_CYCLE_FILE);
if(conf != "") {
if(ps == 1){
PeriodState = PERIOD_STARTED;
index1 = conf.indexOf(',', 0);
CurrentCycle.StartTime = (conf.substring(0, index1)).toInt();
String syms = readFile(CURRENT_SYMP_FILE);
if(syms != "") {
uint8_t y=0;
for(uint16_t i=0; i<syms.length(); i++) {
if(syms[i] != ',') {
CurrentCycle.symptoms[y] += syms[i];
}
else {
y+= 1;
}
}
}
}
else if(ps == 2){
PeriodState = PERIOD_ENDED;
index1 = conf.indexOf(',', 0);
index2 = conf.indexOf(',', index1+1);
CurrentCycle.EndTime = conf.substring(index1+1, index2).toInt();
CurrentCycle.Next = CurrentCycle.EndTime + (AveragePeriodDuration * 24 * 60 * 60);
}
}
if(LogId>0)
readLogFile();
return true;
}
else{
saveDefaultConf();
return false;
}
}
Is the touchscreen working?
I would sprinkle serial prints in sensible spaces to see what is happening. E.g.
conf = readFile(CURRENT_CYCLE_FILE);
Serial.print(F("conf = "));
Serial.println(conf);
and
index1 = conf.indexOf(',', 0);
Serial.print(F("index1 = "));
Serial.println(index1);
etc etc
Full code or representative example will help. Wich Arduino?
thanks for the quick reply! for the first question, the touchscreen was working fine until adding the SD component but in previous iterations, it was as expected. for the second question, I am using adafruit metro m4. I implemented your suggestions this way and nothing changed on the serial monitor. please let me know if I misunderstood your advice!
bool readSavedConfiguration() {
#ifdef DEBUGGING
Serial.println("[EEPROM] Reading saved configuration");
#endif
String conf = readFile(CONFIG_FILE); //average days, period state, total logs
if(conf != "") {
int index1 = 0;
int index2 = 0;
index1 = conf.indexOf(',', 0);
AveragePeriodDuration = (conf.substring(0, index1)).toInt();
index2 = conf.indexOf(',', index1+1);
int ps = (conf.substring(index1+1, index2)).toInt();
index1 = conf.indexOf(',', index2+1);
LogId = (conf.substring(index2+1, index1)).toInt();
conf = readFile(CURRENT_CYCLE_FILE);
//arduino q recommendation
Serial.print(F("conf = "));
Serial.println(conf);
//////////
if(conf != "") {
if(ps == 1){
PeriodState = PERIOD_STARTED;
index1 = conf.indexOf(',', 0);
//arduino q recommendation
Serial.print(F("index1 = "));
Serial.println(index1);
//////////
CurrentCycle.StartTime = (conf.substring(0, index1)).toInt();
String syms = readFile(CURRENT_SYMP_FILE);
if(syms != "") {
uint8_t y=0;
for(uint16_t i=0; i<syms.length(); i++) {
if(syms[i] != ',') {
CurrentCycle.symptoms[y] += syms[i];
}
else {
y+= 1;
}
}
}
}
else if(ps == 2){
PeriodState = PERIOD_ENDED;
index1 = conf.indexOf(',', 0);
index2 = conf.indexOf(',', index1+1);
CurrentCycle.EndTime = conf.substring(index1+1, index2).toInt();
CurrentCycle.Next = CurrentCycle.EndTime + (AveragePeriodDuration * 24 * 60 * 60);
}
}
if(LogId>0)
readLogFile();
return true;
}
else{
saveDefaultConf();
return false;
}
}
No, you did not misunderstand.
Does it print "conf ="?
If yes, does it print the line containing conf?
Draw your conclusions based on that.
Next you can e.g. modify to see what happens when you read the file.
Add serial prints in there, e.g.
if(file) {
Serial.print(F("Reading file "));
Serial.println(filename);
filedata = file.readStringUntil('\n');
Serial.print(F("filedata = "));
Serial.println(filedata);
}
else {
Serial.print(F("Writing file "));
Serial.println(filename);
file = SD.open(filename, FILE_WRITE);
}
If you see anything unexpected, you know where to look.
Your topic was MOVED to its current forum category which is more appropriate than the original as it has nothing to do with Installation and Troubleshooting of the IDE
system
Closed
March 8, 2023, 5:17am
6
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.