Columns with lines?
The sprintf function can fill data with spaces to align them. However, the Arduino sprintf function does not support floating point variables. The dtostrf function has already a fixed output width by itself.
The sketch below is a combination of UTF-8 characters and the dtostrf and sprintf functions.
void setup() {
Serial.begin(9600);
Serial.println();
Serial.println("┏━━━━━━┳━━━━━━━━┳━━━━━━━━┓");
Serial.println("┃ raw ┃ °C ┃ °F ┃");
Serial.println("┣━━━━━━╋━━━━━━━━╋━━━━━━━━┫");
}
void loop() {
int raw = random(0, 1024);
float c = random(-1000, 1000) / 10.0;
float f = random(-500, 1500) / 10.0;
char buffer_c[20];
char buffer_f[20];
dtostrf(c, 6, 1, buffer_c);
dtostrf(f, 6, 1, buffer_f);
char buffer[80];
sprintf(buffer, "┃ %4d ┃ %s ┃ %s ┃", raw, buffer_c, buffer_f);
Serial.println(buffer);
delay(500);
}
The strlen() function can not be used before printing a floating point number to the serial port, since its length is determined while printing characters to the serial port. The final number of bytes that is written is the return value.
There is a way to use that return value, by using a dummy write to a dummy serial port. After that the number of spaces to align the floating point number can be calculated.
That reduces the code size from 6160 to 4708 for an Arduino Uno.
class DummyClass : public Stream
{
int available() {return 0;}
int read() {return 0;}
int peek() {return 0;}
void flush() {return;}
size_t write(uint8_t) {return 1;}
using Print::write;
};
DummyClass Dummy;
const int width_r = 4;
const int width_c = 6;
const int width_f = 6;
void setup() {
Serial.begin(9600);
Serial.println();
Serial.println("┏━━━━━━┳━━━━━━━━┳━━━━━━━━┓");
Serial.println("┃ raw ┃ °C ┃ °F ┃");
Serial.println("┣━━━━━━╋━━━━━━━━╋━━━━━━━━┫");
}
void loop() {
int raw = random(0, 1024);
float c = random(-1000, 1000) / 10.0;
float f = random(-500, 1500) / 10.0;
int i;
Serial.print("┃ ");
Spaces(width_r - Dummy.print(raw));
Serial.print(raw);
Serial.print(" ┃ ");
Spaces(width_c - Dummy.print(c,1));
Serial.print(c,1);
Serial.print(" ┃ ");
Spaces(width_f - Dummy.print(f,1));
Serial.print(f,1);
Serial.print(" ┃");
Serial.println();
delay(500);
}
void Spaces(int n)
{
for(int i=0; i<n; i++) {
Serial.print(' ');
}
}
I advise not to use this "Dummy" trick. It is mere a coding exercise. I can not find a problem with this trick, but I'm not 100% sure that it is okay.
Raw Value | Sensor 1 | Sensor 2 | Sensor 3?