Why dtostrf ignoring two values?

Hello, i am trying to convert floats by rounding and converting values to one decimal point. like 1.66 to 1.7 somehow values 9 and 10 are empty and i dont know where is my error here? my setup esp32 dev board and arduino 1.8.19 ide.

char value1[4];
char value2[4];
char value3[4];
char value4[4];
char value5[4];
char value6[4];
char value7[4];
char value8[4];
char value9[4];
char value10[4];
char value11[4];

unsigned long serialLastUpdate;
const unsigned long serialRefreshIntervalue = 500;
String values; // all values combined together

void setup() {
  Serial.begin(115200);
}
void loop() {
  float roundedFloat1 = round(1.12 * 10) / 10.0;       dtostrf(roundedFloat1, 3, 1, value1);  
  float roundedFloat2 = round(2.12 * 10) / 10.0;       dtostrf(roundedFloat2, 3, 1, value2);
  float roundedFloat3 = round(3.12 * 10) / 10.0;       dtostrf(roundedFloat3, 3, 1, value3);
  float roundedFloat4 = round(4.12 * 10) / 10.0;       dtostrf(roundedFloat4, 3, 1, value4);
  float roundedFloat5 = round(5.12 * 10) / 10.0;       dtostrf(roundedFloat5, 3, 1, value5);
  float roundedFloat6 = round(6.12 * 10) / 10.0;       dtostrf(roundedFloat6, 3, 1, value6);
  float roundedFloat7 = round(7.12 * 10) / 10.0;       dtostrf(roundedFloat7, 3, 1, value7);
  float roundedFloat8 = round(8.12 * 10) / 10.0;       dtostrf(roundedFloat8, 3, 1, value8);
  float roundedFloat9 = round(9.12 * 10) / 10.0;       dtostrf(roundedFloat9, 3, 1, value9);
  float roundedFloat10 = round(10.12 * 10) / 10.0;     dtostrf(roundedFloat10, 3, 1, value10);
  float roundedFloat11 = round(11.12 * 10) / 10.0;     dtostrf(roundedFloat11, 3, 1, value11);
  values = String(value1) + "|  " + String(value2) + "|  " + String(value3) + "|  " + String(value4) + "|  " + String(value5) + "|  " + String(value6) + "|  " + String(value7) + "|  " + String(value8) + "|  " + String(value9) + "|  " + String(value10) + "|  " + String(value11);

  serial();
}


void serial()
{
  if (millis() - serialLastUpdate >= serialRefreshIntervalue) { 
    serialLastUpdate = millis();
    Serial.println(values); 
  }
}

serial output i am getting is
"1.1| 2.1| 3.1| 4.1| 5.1| 6.1| 7.1| 8.1| | | 11.1"

dtostrf adds a null character to the end of the character string it creates. So 10.1 requires an array of 5 to store. It's possible that one of the conversions is overwriting its array and you are corrupting memory on the other one.

Try increasing the 4 to 5 in the char array sizes and see if that changes anything.

1 Like

yes, that worked, thanks for quick replay. Confusing was that value 11 worked.

Buffer overruns are undefined behavior. So anything can happen. In the case of 11, it was the last one so there may not have been anything in memory behind it and it got a free extra slot. What gets really fun is when you make some completely unrelated change way off somewhere else in the code and it ends up changing the way things are arranged in memory and suddenly something that used to work now doesn't. Always a good hint to look for buffer overrun.

2 Likes

Your topic has been moved to a more suitable location on the forum.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.