Float to string conversion problem in MKR 1010 board

Hi, I have a question regarding the Mrk 1010 wifi board together with the float to string conversion. I tried to convert the float to sting using the dtostrf () function, but it's still not working. Its keep on showing the error as (expected constructor and desctructor).......something like this. I have been trying to find out this error for past few weeks already. Nothing works out.

The error problem occurs when we upload the code to the board. When we try to verify/run the code it's fine. Below is the code I used. Can anyone help me to figure this out?

float V = 0;
float Vrms = 0;
float Amps = 0;

char Vchar[6];
char Vrmschar[6];
char Ampschar[6];

dtostrf(V,3,2,Vchar);
dtostrf(Vrms,3,2,Vrmschar);
dtostrf(Amps,3,2,Ampschar);

Please post a complete program that compiles and demonstrates the problem.

Post the complete error message.

Code snippets suck.

Is any of this code in a function?

You may need to add this to your sketch, dtostrf() is not a standard function and is not included by default in the SAMD core.

#include "avr/dtostrf.h"

Have you tried sprintf() to see if it supports floats?

const int sensorin = A0;
const int sensorin1 = A1;
int mVamps = 100; 


float V = 0;
float Vrms = 0;
float Amps = 0;

char Vchar[6];
char Vrmschar[6];
char Ampschar[6];

dtostrf(V,3,2,Vchar);
dtostrf(Vrms,3,2,Vrmschar);
dtostrf(Amps,3,2,Ampschar);

void service()
{
 V = getVPP();
 Vrms = (V/2.0) *0.707;  
 Amps = (Vrms * 1000)/mVamps;

}

void SubmitData()
{    
strcpy(json, "\",\"Amps\":\"");
 strcat(json, Ampschar());
      
}

the error occured in the conversion line dtostrf as below:
exit status 1
expected constructor, destructor, or type conversion before '(' token

Yes, and it still shows the same error

exit status 1
expected constructor, destructor, or type conversion before '(' token

That is because, as post #3 questioned, you put a function call to dtostrf() outside of a function.

Where are setup() and loop()? You need either those or main().

This will at least compile and give an idea of the correct syntax, does not have any code to actually print the results, and I don't have a SAMD board handy for testing.

#include "avr/dtostrf.h"

const int sensorin = A0;
const int sensorin1 = A1;
int mVamps = 100;


float V = 0;
float Vrms = 0;
float Amps = 0;

char Vchar[6];
char Vrmschar[6];
char Ampschar[6];

float getVPP() { //function to allow sketch to compile
  return (0.1 * random(100));
}

void service()
{
  V = getVPP();
  Vrms = (V / 2.0) * 0.707;
  Amps = (Vrms * 1000) / mVamps;
  dtostrf(V, 3, 2, Vchar);
  dtostrf(Vrms, 3, 2, Vrmschar);
  dtostrf(Amps, 3, 2, Ampschar);
}

void SubmitData()
{
  char json[20];
  strcpy(json, "\",\"Amps\":\"");
  strcat(json, Ampschar);

  //this should do the same thing as the strcpy() and strcat()
  sprintf(json, "\",\"Amps\":\"%3.2f", Amps);
}

void setup() {

}

void loop() {

}
1 Like

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