Arduino Forum

Products => Arduino Zero => Topic started by: kinematik on Feb 13, 2016, 12:39 pm

Title: dtostrf function not working in Arduino Zero
Post by: kinematik on Feb 13, 2016, 12:39 pm
I am trying to write float variables in my ST7565 display using a Zero Board.

I am aware fo the Arduino libraries limitations with the sprintf function, It does not work using floats in both architectures AVR and SAMD, because it requires much memory resources in an embedded environment.

The usual solution for displaying floats in Serial/Displays are working with integer values, or using the dtostrf to convert the floats into char.

The dtostrf function is not available for the Arduino SAMD architecture and you need to include a bizarre include in the code: avr/dtostrf.h, as reported in this forum.

My problem is that I can not convert floats into char using dtostrf. As an example, the following code returns empty spaces in the place of the float variables:

Code: [Select]
#include <avr/dtostrf.h>        // dtostrf missing in Arduino Zero/Due

char buffer[20];

void setup() {}

void loop() {
  double a = 12.345;
  char aChar[8]="";
  dtostrf(a, 2, 2, aChar);
 
  sprintf(buffer, "LCD Value: %s", aChar);
 
  SerialUSB.print(a);
  SerialUSB.print("\t");
  SerialUSB.print(aChar);
  SerialUSB.print("\t");
  SerialUSB.println(buffer);
  delay(1000);
}


What Am I doing wrong?
Title: Re: dtostrf function not working in Arduino Zero
Post by: kinematik on Feb 17, 2016, 09:29 pm
OK, here in the Zero forum I found the solution:
http://forum.arduino.cc/index.php?topic=368720.0

The link above shows a working dtostrf function for the Zero architecture. What I donĀ“t understand is why this function is not included by default in the Arduino Zero core librarie.
Title: Re: dtostrf function not working in Arduino Zero
Post by: david_prentice on Feb 18, 2016, 10:03 am
Because it is a non-standard function that does not come with the ARM toolchain libraries.
It is only available for the AVR because it happens to be in libc.a

From an Arduino point of view,    you would be expected to use the print(double)  or print(doublevalue, precision) methods.

Yes,   adding your own dstrtof makes it easier to port non-standard AVR sketches.

David.