How to convert a string in a decimal value "12345" to 12345

Nello,
I have a project with an arduino and a nextion screen (Oled interactif) the nextion comunicate with the arduino using only a string of characters (no choice).
I input a number (integer part and decimal part); The nextion concat both with a dot between integer and decimal, and send a string to the arduino like that: "+12345.123" or "-12345.123" plus some control characters. I receive it by the TX, and RX and I can remove the control characters to obtain "+00001.001" or - with the non significatif 0 (mandatory).
I need to convert this reveived string to a floating val on which I must realize some calculation 1,45 or 12345,001 ,etc.. All my tests are not working. I obtain values but nothing correct.
Somebody can help me?
Thanks
Daniel

String wodinv="123.456";
float bling=0.0;

bling = (float)woodinv;

don't works?

Humm.

What mean

?

The data type String and the data type string are different things and those terms cannot be used interchangeably. What, exactly, is the data type that you are receiving?

If the data is in a string (null terminated character array) the atof() function will convert the ASCII string to a float number.

If you still have trouble post the test code and tell is what you expect to obtain and what you really obtain.

This works:

void setup() {
  Serial.begin(115200);
  while (!Serial); //wait for connection
  char s[] = "1234.56";
  Serial.print("value is ");
  Serial.println(atof(s));

}

void loop() {
}

in my contrxt, the number comes from the nextion and is received in a String.
The compiler does not like the conversion to char[].
Daniel

Receive it in a character array (C-string) instead.

The compiler does not like the conversion to char[].

The String class, which should not be used with AVR based Arduinos, includes a function to perform that conversion.

Why have you not posted your code?

I did not posted the code because it is on another pc. I will come back tomorrow with it

If I want to receive the data from the nextion in an array, I have to receive character by character (Serail.read), and I cannot use the Serial.readString() command ?

Take a look at this nice tutorial: Serial Input Basics - updated

+1 for the serial input basics tutorial.

--- bill

So:

  String s1 = "+00001.001";
  Serial.print("value is ");
  Serial.println(s1.toFloat(), 3);

Output is:
10:33:18.561 -> value is 1.001

after so many test, I found a working solution:
Here is it:

String fromnextion=" ";
String signe=" ";
String entier=" ";
String decimal=" ";
String dot="";
char str[6];
float depval;
float pas;
String deplacement;
int filetage=8;
// include the library code:
#include <LiquidCrystal.h>
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);

void setup() {
Serial.begin(9600);
Serial.setTimeout(100);
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
}
String fromnextion=" ";
String signe=" ";
String entier=" ";
String decimal=" ";
String dot="";
char str[6];
float depval;
float pas;
String deplacement;
int filetage=8;
// include the library code:
#include <LiquidCrystal.h>
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);

void setup() {
Serial.begin(9600);
Serial.setTimeout(100);
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
}
void loop() {
lcd.setCursor(0, 0);
if (Serial.available() > 0)
{
fromnextion=Serial.readString();
signe=fromnextion.substring(0,1);
entier=fromnextion.substring(1,6);
decimal=fromnextion.substring(7,10);
deplacement.concat(signe);
deplacement.concat(entier);
deplacement.concat(decimal);
depval=deplacement.toFloat(),0;
depval=depval/1000;
pas=depval*200/8;
lcd.print("Depl: ");
lcd.print(depval);
lcd.setCursor(0,1);
lcd.print("Pas: ");
lcd.print(floor(pas));
}
}

Thanks a lot to all.

Please format code before posting in a code block (<?>).

Now, this part is working, but I have another problem:
I unput data on the nextion
the nextion send the correct data + her own control character
I receive the string on the arduino
i make some convertion and calculation on the arduino
I print it the result o a LCD.
This work one time.
When I try to resend data from the nextion, nothing happen.

Do you remember to clear 'deplacement' before you add text to the end? Or you could change that line to:
deplacement = signe;

Good evening and thank you,
in fact, I had found that it was my lcd that was causing a lag.
I had tried to reset all the data, and I had forgotten this one.
You gave me the trigger!
I still had to print a complete blank line before printing the next data set.
This works.
You know what, I am happy :slight_smile: :slight_smile::slight_smile:

that's very nice to found popeple taking their time to help another one!

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