Char[] manipulation and Passing ByRef for atTiny

I'm new to this, as that number to the left of this posts clearly indicates.

Struggling to find a small footprint function to truncate a float into a 4-5 char array for later display on LCD.

Currently working on an uno, but will be migrated to an atTiny, so want to avoid the sprintf variants, String objects.

Here is current code:

void printData() {

char tval = "    ";

  Serial.print(rotZ);

  Left_Val(rotX, 4, tval); 
  Serial.print("Return of s_out: ");
  Serial.print(tval);
  Serial.print(" strlen(tval): ");
  Serial.println(strlen(tval));

}

void Left_Val(float Input_Val, int Chars, char* buf) {
  char buffer[8];
  char s_out[4];
  
  // dtostrf(floatVar, minStringWidthIncDecimalPoint, numVarsAfterDecimal, charBuf);
  
  dtostrf(Input_Val, 2, Chars, buffer);
  Serial.print("buffer: ");
  Serial.println(buffer);

  for (unsigned int i = 0; i < Chars; i++)
  {
      s_out[i] = buffer[i];
  }  
  Serial.print("s_out: ");
  Serial.println(s_out);
  Serial.print("strlen(s_out): ");
  Serial.println(strlen(s_out));

  return s_out;

  Serial.print("s_out: ");
  Serial.println(s_out);
}

Serial out:

14:41:26.842 -> -238.01
14:41:26.911 -> buffer: -0.7023
14:41:26.944 -> s_out: -0.7
14:41:26.944 -> strlen(s_out): 4
14:41:26.978 -> Return of s_out: 0 strlen(tval): 11
14:41:27.047 -> Gyro (deg) X=-0.45 Y=-113.35 Z=246.62	Accel (g) X=0.00 Y=0.88 Z=-0.93	 Temp=29.24
14:41:27.149 -> buffer: -0.4504
14:41:27.149 -> s_out: -0.4
14:41:27.183 -> strlen(s_out): 4
14:41:27.183 -> Return of s_out: 0 strlen(tval): 11

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.

Have you tried something as simple as

float v = 12.3456789;
void setup()
{
  Serial.begin(115200);
  Serial.println(v, 2);
  Serial.println(v, 3);
  Serial.println(v, 4);
}

void loop() {}

Thanks @J-M-L,

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);

}

No, you can not capture it.

But which library do you use for the display? Most libraries support the .print() function from the Stream-class and thus float as well.

And if you really care about footprint you drop the float all together :wink: A float isn't simply a decimal point.

No, you can not capture it.

Thanks for the confirmation @septillion

But which library do you use for the display? Most libraries support the .print() function from the Stream-class and thus float as well.

And if you really care about footprint you drop the float all together :wink: A float isn't simply a decimal point.

The only library I am currently including is wire.h.

Understood on floats, workin' on it where I can, and limiting lifespan where I can't.

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.

Spereicle:
Struggling to find a small footprint function to truncate a float into a 4-5 char array for later display on LCD.

dtostrf()

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.