Convert char array to float - atof() not working

Hello,

I am trying to convert data sent from a pi-hosted Web-app. If I am correct, the data being sent is string type. I want to convert that data into a float value so I can manipulate it later on. The problem is how to do it.

Here is the code I am using.

String StreamX;
char xFltArr[10];
int SpeedX;

void setup() 
{  
  // start serial port at 9600 bps:
  Serial.begin(9600);

}

void loop() 
{
    StreamX = "";
    // if signal from joystick sent, run code
    if (Serial.available())
    {
      while (Serial.available()) 
      {  
        char inByte = Serial.read();

        if (inByte == 'x' || inByte == 'y' || inByte == ' ' )
        {
         inByte = '0'; 
        }
        
        StreamX += inByte;
      }
       
      StreamX.toCharArray(xFltArr,10);
  
      SpeedX = float(xFltArr); 
         
      Serial.print(SpeedX);
    }
}
  • float() gives me this error: invalid cast from type 'char*' to type 'float'
  • Serial.parseFloat() doesn't give an error, however, the pi console seems to crash.

Which leaves atof(). It seems to work fine with this code until I run some math on it, which is where the code goes wrong.

if i type in a number in the serial console on the IDE, the math runs on each digit and then prints it out. For example, if i enter 123 and the code says to multiply SpeedX by 100, it will print 100200300 rather than 12300.

I have been looking up similar problems with atof(), but all i can find is questions about the number of decimals displayed, which is not my problem.

Its working now, thank you. :slight_smile: