Compilation error dtostrf

Compilation error: 'dtostrf' was not declared in this scope

it is on avr lib, but not with samd lib ?

Not an expert for samd but I found this via Google:

#include <avr/dtostrf.h>

Sources:

dtostrf is not available for ARM Cortex M4 (but should be by default) · Issue #9308 · arduino/Arduino (github.com)

dtostrf function not working in Arduino Zero - Hardware / Arduino Zero - Arduino Forum

Test sketch from (https://github.com/esp8266/Arduino/issues/6695)

#include <avr/dtostrf.h>

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  delay(1000);
  Serial.println();
  
  float f=1234.567;
  char s[20]="";
  dtostrf(f,8,3,s);
  Serial.println(f);
  Serial.println(s);
  Serial.println();
  
}

void loop() {
  // put your main code here, to run repeatedly:

}

Good luck!

Check whether the samd library sprintf() supports float variables. Try this example, which works on ESP32 and ARM Cortex like nRF52840:

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  while (!Serial) yield();
  Serial.println("sprintf float test");

  float x=1.234;
  char buf[40];
  sprintf(buf,"x = %6.4f, cos(x) = %6.4f",x, cos(x));
  Serial.println(buf);

}

void loop() {
  // put your main code here, to run repeatedly:

}

That's worth a try!

In the meanwhile I found this

https://github.com/107-systems/107-Arduino-Debug/blob/main/src/dtostrf.c

/*
  dtostrf - Emulation for dtostrf function from avr-libc
  Copyright (c) 2016 Arduino LLC.  All rights reserved.

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public
  License along with this library; if not, write to the Free Software
  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/

#if defined(ARDUINO_ARCH_SAMD)

#include <stdio.h>

char *dtostrf (double val, signed char width, unsigned char prec, char *sout)
{
  asm(".global _printf_float");

  char fmt[20];
  sprintf(fmt, "%%%d.%df", width, prec);
  sprintf(sout, fmt, val);
  return sout;
}

#endif /* defined(ARDUINO_ARCH_SAMD) */

thank you, it works fine !

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