While I would love to have the victory of figuring out how to make this specific function work as desired, the end goal of obtaining properly formatted output is more important, feel free to make alternate suggestions.
Eventually moving the code to an atTiny means loss of the Serial.
That said, I'm curious, is there a way to capture the Serial.println(v, 2); outputs into a char array?
The end goal is to send a 16 byte array to an lcd using i2c for display. The array will contain 3 'numerical' values with separating spaces in the form:
char packet = "#### #### ####"
I was able to get everything working, using the following code.
Happy to get ideas on an improved implementation.
void outputData() {
char buffer[6];
// Output to LCD
//ref: dtostrf(floatVar, minStringWidthIncDecimalPoint, numVarsAfterDecimal, charBuf);
lcd_cursor(0x00);
lcd_send_text("X: Y: Z:"); //Output LCD Line 1
lcd_cursor(0x40);
// Load tx_packet with spaces
for (byte i = 0; i < 16; i++)
{
tx_packet[i] = 0x20;
}
// Load X values into packet
dtostrf(rotX, 2, 4, buffer);
for (byte i = 0; i < 4; i++)
{
tx_packet[i] = buffer[i];
}
// Load Y values into packet
dtostrf(rotY, 2, 4, buffer);
for (byte i = 0; i < 4; i++)
{
tx_packet[i+5] = buffer[i];
}
// Load Z values into packet
dtostrf(rotZ, 2, 4, buffer);
for (byte i = 0; i < 4; i++)
{
tx_packet[i+10] = buffer[i];
}
// Output to LCD
send_packet(LCD_addr, 16);
}
You can look at the stream class to see how it handles it. But I would just store (for example) 1234 in an (unsigned) int/long instead of 12,34 in a float. And just add a decimal point while printing it to the display.
Would you just want to decipher the Float ? Under point 4. convert the 23 bits a 32-bit integer, and add the sign and point, (or is that what he means in #5) or avoid using the Float all together yes ! Floating point arithmetic is a huge footprint in general.
The end goal is to send a 16 byte array to an lcd using i2c for display. The array will contain 3 'numerical' values with separating spaces in the form:
char packet = "#### #### ####"
Many LCD will support the print command in the same way your serial console does.