Dear All,
I have some problem to save a unint16_t calue into char.
Here is my function to get the Volt value
(Consider logToSd is egal to 2. It will display in Serial Monitor and write to the SD)
It correctly display in Serial Monitor, but it write bad caracter
Here is my first fonction with comments
// logToSd is egal to 2
void getBatt(int logToSd){
uint16_t vbat;
// Have attention to &vbat
if (! fona.getBattVoltage(&vbat)) {
//sprintln(F("\nBatt Volt Fail"),logToSd);
}
else
{
// Here it display in Serial monitor and write to SD, See below
// It the second sprint() which cause the issue : sprint(vbat,logToSd)
sprint(F("nVBat:\t"),logToSd); sprint(vbat,logToSd); sprintln(F(" mV"),logToSd);
}
if (! fona.getBattPercent(&vbat)) {
sprintln(F("Batt % Fail"),logToSd);
}
else
{
// sprint(F("VPct:\t"),logToSd); sprint(vbat,logToSd); sprintln(F("%"),logToSd);
}
}
Now my problem is in that function with my comment.
As vbat is a uint16_t
void sprint(uint16_t message, int logToSd)
{
if(logToSd == 0 || logToSd == 2)
{
// Here is deiplay the correct value
SerialUSB.print(message);
}
if(logToSd == 1 || logToSd == 2)
{
// Here is display a strong value (continue reading next code, below
sd_write(sd_logFile,(char *)&message);
}
}
(sd_logFile is define as a char (logfile.txt). This works fine)
We have to convert uint16_t to char, because sd_write only accpet char.
Here is my next function
void sd_write(char * fileName, char * text)
{
// As it's sd_write, the is no \n or \r, so the last parameter is false
sd_write(fileName, text, false);
}
And now the sd_write function where the bad caracters is displayed
void sd_write(char * fileName, char * text, bool ln)
{
#ifdef LOGGER
if(isSdReady == true)
{
digitalWrite(PIN_SDLED, HIGH);
logfile = SD.open(fileName, FILE_WRITE);
// if the file opened okay, write to it:
if (logfile)
{
sprint(F("Writing ["),0);
sprint(text,0); // HERE IS DISPLAY THE BAD CARACTER
sprint(F("] to "),0);
sprint(F(fileName),0);
if(ln == true)
{
logfile.println(text);
}
else
{
logfile.print(text);
}
logfile.flush();
// close the file:
logfile.close();
sprintln(F(" => Done."),0);
}
else
{
// if the file didn't open, print an error:
sprint(F("\nError opening "),0); sprintln(fileName,0);
}
digitalWrite(PIN_SDLED, LOW);
// delay(100);
}
#endif
}
And here is the display :
4004
Writing [�D] to ADALOG39.TXT => Done.
Instead of
�D
I should have
4004
I think is a convertion problem from uint16_t to char, and I am saking my self if is due to the & here
if (! fona.getBattVoltage(&vbat))
I hope you could follow my "path" and can help me to correctly save the uint16_t value into my SD file, in char.
Many thnak