Working with floats

Hi everyone, I am doing a project for which I wish to obtain some GPS data, and use the speed measured by the GPS device. For that I first want to turn this speed in knots, given in a character array, into a float, in m/s.

I have found a way that works (speedOL is a character array of 4, with value 0.04 for this example)

Serial.print(speedOL[0]);
  Serial.print(speedOL[1]);
  Serial.print(speedOL[2]);
  Serial.print(speedOL[3]);
  Serial.println("knots");

  //speedOL (knots) to velocity (m/s)
  float speedOL0 = speedOL[0]-'0';
  float speedOL2 = speedOL[2]-'0';
  float speedOL3 = speedOL[3]-'0';
  float velocitytest= (speedOL0+0.1*speedOL2+0.01*speedOL3)*0.514; //m/s
  Serial.print(velocitytest);
  Serial.println(" m/s");

with output:

0.04knots
0.02 m/s

exactly what I need.

Now I thought I could make this shorter, and with less variables, by writing:

Serial.print(speedOL[0]);
  Serial.print(speedOL[1]);
  Serial.print(speedOL[2]);
  Serial.print(speedOL[3]);
  Serial.println("knots");

float velocity = ((speedOL[0]-'0')+(0.1*speedOL[2]-'0')+(0.01*speedOL[3]-'0'))*0.514; //m/s
  Serial.print(velocity);
  Serial.println(" m/s");

but this has output:

0.04knots
-46.61 m/s

Now I can't really make sense of why the first one works, but the second one does not. Maybe I am missing something, or I am doing something wrong.
All help is appreciated greatly.

How about ?(0.1 * (speedOL[2] - '0'))

Deva_Rishi:
How about ?

(0.1 * (speedOL[2] - '0'))

What an obvious mistake! :astonished:
Thanks for pointing it out! it works great now.

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