How to convert String to Float?

hey guys, i have a question. is it possible to convert from String to float data?

here is my receiver program:

// receiver Programming

#include <VirtualWire.h>
#include <L298N.h>


const int receive_pin = 12;

String message;
String myString;


float nilaisatu;
int nilaidua;
int nilaitiga;
int nilaiempat;

float satuV0;
#define ena  5
#define enb  6

#define in1  8
#define in2  9
#define in3  10
#define in4  11

L298N motor(ena,in1,in2);
L298N motor2(enb, in3,in4);

void setup()
{
    delay(50);
    Serial.begin(9600); // Debugging only
  pinMode(ena, OUTPUT);
  pinMode(enb, OUTPUT);
  pinMode(receive_pin, INPUT);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(in3, OUTPUT);
  pinMode(in4, OUTPUT);
 
 analogWrite(ena, 0);
 analogWrite(enb, 0);
 delay(50);

   
    Serial.println("setup");

    // Initialise the IO and ISR

    vw_set_rx_pin(receive_pin);

    vw_set_ptt_inverted(true); // Required for DR3100
    vw_setup(2000);  // Bits per sec

    vw_rx_start();       // Start the receiver PLL running

}

void loop()
{
  diam();
  kanan();
      uint8_t buf[VW_MAX_MESSAGE_LEN];
    uint8_t buflen = VW_MAX_MESSAGE_LEN;

    if (vw_get_message(buf, &buflen)) // Non-blocking
    {
 int i;

    
 // Message with a good checksum received, dump it.
 Serial.print("Got: ");
 
 for (i = 0; i < buflen; i++)
 {
     char c = (buf[i]);
            message = message + c ; // make a message from the received characters        
 }
  myString = message;
 Serial.println(message);

String l = getValue(myString, ',', 0); // nilaisatu


satuV0 = l.toInt();

Serial.println("*********from Transmitter************");
Serial.println(satuV0);
delay(100); 


 


     message = "";
    }
   

}
 void kanan(){
  if (satuV0>=2){
    motor.setSpeed(200);
    motor2.setSpeed(200);
    motor.forward();
    motor2.backward();
    delay(50);
  }
 }
 void diam(){
  if (satuV0<2){
    motor.setSpeed(0);
    motor2.setSpeed(0);
    motor.forward();
    motor2.forward();
    delay(50);
  }
 }


String getValue(String data, char separator, int index)
{
    int found = 0;
    int strIndex[] = { 0, -1 };
    int maxIndex = data.length() - 1;

    for (int i = 0; i <= maxIndex && found <= index; i++) {
        if (data.charAt(i) == separator || i == maxIndex) {
            found++;
            strIndex[0] = strIndex[1] + 1;
            strIndex[1] = (i == maxIndex) ? i+1 : i;
        }
    }
    return found > index ? data.substring(strIndex[0], strIndex[1]) : "";
}

Although usage of "String" is highly discouraged with Arduinos: https://www.arduino.cc/en/Tutorial/StringToFloatExample

Don't bother with Strings, they cause the Arduino to crash.

Take the character buffer as it is received (but make sure it is properly zero terminated) and use the atof() function to produce a float.

if (vw_get_message(buf, &buflen)) // Non-blocking
    { 
     buf[buflen]=0; //make sure zero terminated
     float x = atof(buf);  //may need to write atof( (char*) buf);

Better, don't send floats as text. Send and receive them in binary and skip the conversion. Discussed on this forum many, many times.

jremington:
Don't bother with Strings, they cause the Arduino to crash.

Take the character buffer as it is received (but make sure it is properly zero terminated) and use the atof() function to produce a float.

if (vw_get_message(buf, &buflen)) // Non-blocking

{
    buf[buflen]=0; //make sure zero terminated
    float x = atof(buf);  //may need to write atof( (char*) buf);




Better, don't send floats as text. Send and receive them in binary and skip the conversion. Discussed on this forum many, many times.

appreciate man, do you have link of how to send and receive float in binary?

arfbdrto:
appreciate man, do you have link of how to send and receive float in binary?

Through which communication link -- UART, I2C, or SPI?

BTW:
Binary refers to 'natural binary'; for example: the 8-bit binary code for A is -- 00001010 (0x0A). Binary data are usually exchanged uising I2C and SPI link,

ASCII or 'ASCII Coded Binary' refers to '8-bit of the ASCII Code'; for example: the 8-bit ASCII code of A is 01000001 (0x41). 'ASCII Coded Binary' is also known as 'Text Code' or simply 'ASCII'. ASCII data are usually exchanged by UART link,

GolamMostafa:
Through which communication link -- UART, I2C, or SPI?

BTW:
Binary refers to 'natural binary'; for example: the 8-bit binary code for A is -- 00001010 (0x0A). Binary data are usually exchanged uising I2C and SPI link,

ASCII or 'ASCII Coded Binary' refers to '8-bit of the ASCII Code'; for example: the 8-bit ASCII code of A is 01000001 (0x41). 'ASCII Coded Binary' is also known as 'Text Code' or simply 'ASCII'. ASCII data are usually exchanged by UART link,

i use rf 433 Mhz for communication. so i use flex sensor with float data and send them to control a motor DC. i just don't know how to receive that with float number.

arfbdrto:
i use rf 433 Mhz for communication. so i use flex sensor with float data and send them to control a motor DC. i just don't know how to receive that with float number.

y not simply send the analog read interger value for the flex sensor instead and do the 'float' calculation on the receiver side?

I would like to assume that you are using HC12 433 MHz Radio Moule for data exchange between Arduino Sender and Receiver. HC12s are UART based communicaton devices. So, you are essentially using UART link to exchange data. Here is an example of how to send/receive float data using UART link. (Data would be sent in ASCII.)

Setup:


Figure-1: Connection between UNOs using Software UART Port based HC12 Modules

Sender Codes: (tested using direct wiring connection and without HC12)

#include <SoftwareSerial.h>
SoftwareSerial SUART(10, 11); //STX = DPin-10, SRX = DPin=11
float myNumber = 12.45;

void setup()
{
  Serial.begin(9600);             // Harware Serial port to computer
  SUART.begin(9600);               // Software Serial port to HC12
}

void loop()
{
  SUART.print(myNumber);   //12.45 goes as: 0x31, 0x32, 0x2E, 0x34m 0x35
  SUART.print('\n');      //end mark of message; it goes as 0x0A
  delay(1000);            //1-sec test interval
}

Receiver Codes:

#include <SoftwareSerial.h>
SoftwareSerial SUART(10, 11); //STX = DPin-10, SRX = DPin=11

char myArray[20] = "";
float myNumber;

void setup()
{
  Serial.begin(9600);             // Harware Serial port to computer
  SUART.begin(9600);               // Software Serial port to HC12
}

void loop()
{
  byte n = SUART.available();
  if (n != 0)
  {
    SUART.readBytesUntil('\n', myArray, 20); //recive and store except \n
    myNumber = atof(myArray); //convert received ASCII to original float
    Serial.println(myNumber, 2);  //show on receiver's SM
    char myArray[20] = ""; //reset array
  }
}

Screenshot at Receiver:
sm168.png

sm168.png

do you have link of how to send and receive float in binary?

google "virtualwire send receive float binary" 2nd hit

Simplest method to transmit:

float x=3.14;
vw_send((uint8_t *)&x, 4);

Receive:

float x=0;
uint8_t buflen=4;

if (vw_get_message((uint8_t *)&x, &buflen)) // Non-blocking
    {
     Serial.println(x);