Ok, I’ve run into what appears to be a simple roadblock once found, and encountered something really strange! I’m building a volts/amps/temps datalogger that will log six different values. The hardware consists of an Arduino Mega Pro, HC-06 Bluetooth, MicroSD card, an RTC clock module, a ZMPT101B AC voltage sensor, an ACS712 current sensor, and an SCT-013-030 current sensor. I’ve set it up so that it will store six values in a file on the SD card that indicate which of the values to actually log, along with calibration values for some of the sensors.
A function named “loadConfig()” was added to load the float values from the “/CONFIG.TXT” file on the SD card into variables, and it’s called early on in setup(). When it runs, it’s unable to open the “/CONFIG.TXT” file. There’s another routine that opens the same config file when I send it a command via Bluetooth to send the contents of the config file to my phone, and that one has worked without fail. So, as usual, I copied the bare basics into a new sketch, compiled it, and it runs perfectly. So, the first question is, why would the app open the config file when sending the data via Bluetooth but not when it attempts to load the files in the loadConfig routine? The second question is, why would the exact same code run in the standalone sketch and not as part of the main application?
When the main program is compiled, it shows using 19% of the program memory and 53% of the dynamic memory, so I’m feeling that memory isn’t an issue:
Sketch uses 50138 bytes (19%) of program storage space. Maximum is 253952 bytes.
Global variables use 4411 bytes (53%) of dynamic memory, leaving 3781 bytes for local variables. Maximum is 8192 bytes.
Here’s the small sketch containing the code that works. Everything was copied and pasted from the main application:
#define sdCSPin 53
#include <SD.h>
File configFile;
const int numFields=34;
float numValue[numFields];
float SENSITIVITY;
double ampsCalibrationValue;
float R1, R2;
void setup() {
Serial.begin(115200);
SD.begin(sdCSPin);
loadConfig();
}
void loop() {
// put your main code here, to run repeatedly:
}
void loadConfig() {
configFile = SD.open("/CONFIG.TXT");
Serial.println(configFile);
if (configFile) {
while (configFile.available()) {
numValue[27]=configFile.parseFloat();
numValue[28]=configFile.parseFloat();
numValue[29]=configFile.parseFloat();
numValue[30]=configFile.parseFloat();
numValue[31]=configFile.parseFloat();
numValue[32]=configFile.parseFloat();
SENSITIVITY=configFile.parseFloat();
ampsCalibrationValue=configFile.parseFloat();
R1=configFile.parseFloat();
R2=configFile.parseFloat();
Serial.println(numValue[27]);
Serial.println(numValue[28]);
Serial.println(numValue[29]);
Serial.println(numValue[30]);
Serial.println(numValue[31]);
Serial.println(numValue[32]);
Serial.println(SENSITIVITY);
Serial.println(ampsCalibrationValue);
Serial.println(R1);
Serial.println(R2);
}
configFile.close();
} else {
Serial.println("Error opening /CONFIG.TXT");
}
while (1);
}
When it runs, it displays the status of “configFile” along with the ten values in the config file:

Now the strange part! Approximately 9-10 seconds after the ten values are displayed, ten more values of “0.00” appear right below the first ten values. The file is closed after the contents have been read, so I have no idea what’s generating the second set of values. I occasionally forget to dimension arrays large enough and that definitely generates some interesting results, but I’ve never come across this situation before.
Anyone care to take a swing at this? Thanks!
Update: I added a line of code to verify that the file was closed before the second set of values was displayed, and it wasn’t!
#define sdCSPin 53
#include <SD.h>
File configFile;
const int numFields=34;
float numValue[numFields];
float SENSITIVITY;
double ampsCalibrationValue;
float R1, R2;
void setup() {
Serial.begin(115200);
SD.begin(sdCSPin);
loadConfig();
}
void loop() {
// put your main code here, to run repeatedly:
}
void loadConfig() {
configFile = SD.open("/CONFIG.TXT");
Serial.println(configFile);
if (configFile) {
while (configFile.available()) {
numValue[27]=configFile.parseFloat();
numValue[28]=configFile.parseFloat();
numValue[29]=configFile.parseFloat();
numValue[30]=configFile.parseFloat();
numValue[31]=configFile.parseFloat();
numValue[32]=configFile.parseFloat();
SENSITIVITY=configFile.parseFloat();
ampsCalibrationValue=configFile.parseFloat();
R1=configFile.parseFloat();
R2=configFile.parseFloat();
Serial.println(numValue[27]);
Serial.println(numValue[28]);
Serial.println(numValue[29]);
Serial.println(numValue[30]);
Serial.println(numValue[31]);
Serial.println(numValue[32]);
Serial.println(SENSITIVITY);
Serial.println(ampsCalibrationValue);
Serial.println(R1);
Serial.println(R2);
}
configFile.close();
Serial.println("file closed");
} else {
Serial.println("Error opening /CONFIG.TXT");
}
while (1);
}
Still doesn’t explain with the second set of values came from. I have very limited experience with using SD cards with the Arduino, so I’m totally open to any suggestions on how to make improvements!

