I would use sprintf, with a statically allocated buffer.
E.g.
substitute
// Data preparation for file saving:
String dataString = ""; // string for assembling the data to log:
// Add time tag:
dataString += String(Time0); dataString += ",";
// Append the MPU6050 data to the string:
dataString += String(AcX); dataString += ",";
dataString += String(AcY); dataString += ",";
dataString += String(AcZ); dataString += ",";
dataString += String(GyX); dataString += ",";
dataString += String(GyY); dataString += ",";
dataString += String(GyZ);
with
static char pippo[PIPPO_SIZE];
int ret = sprintf(pippo, "%lu,%d,%d,%d,%d,%d,%d",
(uint32_t) Time0, AcX, AcY, AcZ, GyX, GyY, GyZ);
// This is just for debugging purposes to test the code
if (ret < 0) {
// failure: no byte written
} else if (ret >= PIPPO_SIZE) {
// failure: too many bytes written, I did some mistake :(
} else {
// ok
}
The size of the buffer pippo
is computed as follows:
1 byte
for each comma, you have 66 bytes
for eachint16_t
variable (5 bytes
for the digits plus1 byte
for the sign, which however you might not need)10 bytes
forTime0
, which I assume is of typeunsigned long
which in Arduino is 32 bits.1 byte
for closing0円
So it should be 1 * 6 + 6 * 6 + 10 + 1 = 53
unless I did some mistake. Add the following at the top of your program:
#define PIPPO_SIZE 53
The motivation for using statically allocated memory is simple, and is already stated in your question: the concatenation of String
objects results in repeated calls to new
and delete
with subsequent allocation and de-allocation of dynamic memory, which gets fragmented. More over, the String
-based approach is intuitively slower because it requires many more steps to accomplish basically the same simple task.
String
aren't bad per se, but in your application you want to squeeze every last fragment of performance that you can, so there's that.
I would use sprintf, with a statically allocated buffer.
E.g.
substitute
// Data preparation for file saving:
String dataString = ""; // string for assembling the data to log:
// Add time tag:
dataString += String(Time0); dataString += ",";
// Append the MPU6050 data to the string:
dataString += String(AcX); dataString += ",";
dataString += String(AcY); dataString += ",";
dataString += String(AcZ); dataString += ",";
dataString += String(GyX); dataString += ",";
dataString += String(GyY); dataString += ",";
dataString += String(GyZ);
with
static char pippo[PIPPO_SIZE];
int ret = sprintf(pippo, "%lu,%d,%d,%d,%d,%d,%d",
(uint32_t) Time0, AcX, AcY, AcZ, GyX, GyY, GyZ);
// This is just for debugging purposes to test the code
if (ret < 0) {
// failure: no byte written
} else if (ret >= PIPPO_SIZE) {
// failure: too many bytes written, I did some mistake :(
} else {
// ok
}
The size of the buffer pippo
is computed as follows:
1 byte
for each comma, you have 66 bytes
for eachint16_t
variable (5 bytes
for the digits plus1 byte
for the sign, which however you might not need)10 bytes
forTime0
, which I assume is of typeunsigned long
which in Arduino is 32 bits.1 byte
for closing0円
So it should be 1 * 6 + 6 * 6 + 10 + 1 = 53
unless I did some mistake. Add the following at the top of your program:
#define PIPPO_SIZE 53
The motivation for using statically allocated memory is simple, and is already stated in your question: the concatenation of String
objects results in repeated calls to new
and delete
with subsequent allocation and de-allocation of dynamic memory, which gets fragmented. More over, the String
-based approach is intuitively slower because it requires many more steps to accomplish basically the same simple task.
I would use sprintf, with a statically allocated buffer.
E.g.
substitute
// Data preparation for file saving:
String dataString = ""; // string for assembling the data to log:
// Add time tag:
dataString += String(Time0); dataString += ",";
// Append the MPU6050 data to the string:
dataString += String(AcX); dataString += ",";
dataString += String(AcY); dataString += ",";
dataString += String(AcZ); dataString += ",";
dataString += String(GyX); dataString += ",";
dataString += String(GyY); dataString += ",";
dataString += String(GyZ);
with
static char pippo[PIPPO_SIZE];
int ret = sprintf(pippo, "%lu,%d,%d,%d,%d,%d,%d",
(uint32_t) Time0, AcX, AcY, AcZ, GyX, GyY, GyZ);
// This is just for debugging purposes to test the code
if (ret < 0) {
// failure: no byte written
} else if (ret >= PIPPO_SIZE) {
// failure: too many bytes written, I did some mistake :(
} else {
// ok
}
The size of the buffer pippo
is computed as follows:
1 byte
for each comma, you have 66 bytes
for eachint16_t
variable (5 bytes
for the digits plus1 byte
for the sign, which however you might not need)10 bytes
forTime0
, which I assume is of typeunsigned long
which in Arduino is 32 bits.1 byte
for closing0円
So it should be 1 * 6 + 6 * 6 + 10 + 1 = 53
unless I did some mistake. Add the following at the top of your program:
#define PIPPO_SIZE 53
The motivation for using statically allocated memory is simple, and is already stated in your question: the concatenation of String
objects results in repeated calls to new
and delete
with subsequent allocation and de-allocation of dynamic memory, which gets fragmented. More over, the String
-based approach is intuitively slower because it requires many more steps to accomplish basically the same simple task.
String
aren't bad per se, but in your application you want to squeeze every last fragment of performance that you can, so there's that.
I would use sprintf, so you avoid using dynamic memorywith a statically allocated by concatenating multiple String
objectsbuffer.
E.g.
substitute
// Data preparation for file saving:
String dataString = ""; // string for assembling the data to log:
// Add time tag:
dataString += String(Time0); dataString += ",";
// Append the MPU6050 data to the string:
dataString += String(AcX); dataString += ",";
dataString += String(AcY); dataString += ",";
dataString += String(AcZ); dataString += ",";
dataString += String(GyX); dataString += ",";
dataString += String(GyY); dataString += ",";
dataString += String(GyZ);
with
static char pippo[PIPPO_SIZE];
int ret = sprintf(pippo, "%lu,%d,%d,%d,%d,%d,%d",
(uint32_t) Time0, AcX, AcY, AcZ, GyX, GyY, GyZ);
// This is just for debugging purposes to test the code
if (ret < 0) {
// failure: no byte written
} else if (ret >= PIPPO_SIZE) {
// failure: too many bytes written, I did some mistake :(
} else {
// ok
}
The size of the buffer pippo
is computed as follows:
1 byte
for each comma, you have 66 bytes
for eachint16_t
variable (5 bytes
for the digits plus1 byte
for the sign, which however you might not need)10 bytes
forTime0
, which I assume is of typeunsigned long
which in Arduino is 32 bits.1 byte
for closing0円
So it should be 1 * 6 + 6 * 6 + 10 + 1 = 53
unless I did some mistake. Add the following at the top of your program:
#define PIPPO_SIZE 53
The motivation for using statically allocated memory is simple, and is already stated in your question: the concatenation of String
objects results in repeated calls to new
and delete
with subsequent allocation and de-allocation of dynamic memory, which gets fragmented. More over, the String
-based approach is intuitively slower because it requires many more steps to accomplish basically the same simple task.
I would use sprintf, so you avoid using dynamic memory allocated by concatenating multiple String
objects.
E.g.
substitute
// Data preparation for file saving:
String dataString = ""; // string for assembling the data to log:
// Add time tag:
dataString += String(Time0); dataString += ",";
// Append the MPU6050 data to the string:
dataString += String(AcX); dataString += ",";
dataString += String(AcY); dataString += ",";
dataString += String(AcZ); dataString += ",";
dataString += String(GyX); dataString += ",";
dataString += String(GyY); dataString += ",";
dataString += String(GyZ);
with
static char pippo[PIPPO_SIZE];
int ret = sprintf(pippo, "%lu,%d,%d,%d,%d,%d,%d",
(uint32_t) Time0, AcX, AcY, AcZ, GyX, GyY, GyZ);
// This is just for debugging purposes to test the code
if (ret < 0) {
// failure: no byte written
} else if (ret >= PIPPO_SIZE) {
// failure: too many bytes written, I did some mistake :(
} else {
// ok
}
The size of the buffer pippo
is computed as follows:
1 byte
for each comma, you have 66 bytes
for eachint16_t
variable (5 bytes
for the digits plus1 byte
for the sign, which however you might not need)10 bytes
forTime0
, which I assume is of typeunsigned long
which in Arduino is 32 bits.1 byte
for closing0円
So it should be 1 * 6 + 6 * 6 + 10 + 1 = 53
unless I did some mistake. Add the following at the top of your program:
#define PIPPO_SIZE 53
I would use sprintf, with a statically allocated buffer.
E.g.
substitute
// Data preparation for file saving:
String dataString = ""; // string for assembling the data to log:
// Add time tag:
dataString += String(Time0); dataString += ",";
// Append the MPU6050 data to the string:
dataString += String(AcX); dataString += ",";
dataString += String(AcY); dataString += ",";
dataString += String(AcZ); dataString += ",";
dataString += String(GyX); dataString += ",";
dataString += String(GyY); dataString += ",";
dataString += String(GyZ);
with
static char pippo[PIPPO_SIZE];
int ret = sprintf(pippo, "%lu,%d,%d,%d,%d,%d,%d",
(uint32_t) Time0, AcX, AcY, AcZ, GyX, GyY, GyZ);
// This is just for debugging purposes to test the code
if (ret < 0) {
// failure: no byte written
} else if (ret >= PIPPO_SIZE) {
// failure: too many bytes written, I did some mistake :(
} else {
// ok
}
The size of the buffer pippo
is computed as follows:
1 byte
for each comma, you have 66 bytes
for eachint16_t
variable (5 bytes
for the digits plus1 byte
for the sign, which however you might not need)10 bytes
forTime0
, which I assume is of typeunsigned long
which in Arduino is 32 bits.1 byte
for closing0円
So it should be 1 * 6 + 6 * 6 + 10 + 1 = 53
unless I did some mistake. Add the following at the top of your program:
#define PIPPO_SIZE 53
The motivation for using statically allocated memory is simple, and is already stated in your question: the concatenation of String
objects results in repeated calls to new
and delete
with subsequent allocation and de-allocation of dynamic memory, which gets fragmented. More over, the String
-based approach is intuitively slower because it requires many more steps to accomplish basically the same simple task.
I would use sprintf, so you avoid using dynamic memory allocated by concatenating multiple String
objects.
E.g.
substitute
// Data preparation for file saving:
String dataString = ""; // string for assembling the data to log:
// Add time tag:
dataString += String(Time0); dataString += ",";
// Append the MPU6050 data to the string:
dataString += String(AcX); dataString += ",";
dataString += String(AcY); dataString += ",";
dataString += String(AcZ); dataString += ",";
dataString += String(GyX); dataString += ",";
dataString += String(GyY); dataString += ",";
dataString += String(GyZ);
with
static char pippo[PIPPO_SIZE];
int ret = sprintf(pippo, "%lu,%d,%d,%d,%d,%d,%d",
(uint32_t) Time0, AcX, AcY, AcZ, GyX, GyY, GyZ);
// This is just for debugging purposes to test the code
if (ret < 0) {
// failure: no byte written
} else if (ret >= PIPPO_SIZE) {
// failure: too many bytes written, I did some mistake :(
} else {
// ok
}
The size of the buffer pippo
is computed as follows:
1 byte
for each comma, you have 66 bytes
for eachint16_t
variable (5 bytes
for the digits plus1 byte
for the sign, which however you might not need)10 bytes
forTime0
, which I assume is of typeunsigned long
which in Arduino is 32 bits.1 byte
for closing0円
So it should be 1 * 6 + 6 * 6 + 10 + 1 = 53
unless I did some mistake. Add the following at the top of your program:
#define PIPPO_SIZE 53