Help converting strings from bluetooth(Android) to floats


I’m trying to send the coordinates (latitude and longitude) as strings from an android device to an arduino through Bluetooth. So far, I have been able to display these coordinates in the serial monitor using the following code:

  Example Bluetooth Serial Passthrough Sketch
 by: Jim Lindblom
 SparkFun Electronics
 date: February 26, 2013
 license: Public domain
 This example sketch converts an RN-42 bluetooth module to
 communicate at 9600 bps (from 115200), and passes any serial
 data between Serial Monitor and bluetooth module.
#include <SoftwareSerial.h>  

int bluetoothTx = 2;  // TX-O pin of bluetooth mate, Arduino D2
int bluetoothRx = 3;  // RX-I pin of bluetooth mate, Arduino D3
String readString;

SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);

void setup()
  Serial.begin(9600);  // Begin the serial monitor at 9600bps

  bluetooth.begin(115200);  // The Bluetooth Mate defaults to 115200bps
  bluetooth.print("$");  // Print three times individually
  bluetooth.print("$");  // Enter command mode
  delay(100);  // Short delay, wait for the Mate to send back CMD
  bluetooth.println("U,9600,N");  // Temporarily Change the baudrate to 9600, no parity
  // 115200 can be too fast at times for NewSoftSerial to relay the data reliably
  bluetooth.begin(9600);  // Start bluetooth serial at 9600

void loop()
  if(bluetooth.available())  // If the bluetooth sent any characters

    // Send any characters the bluetooth prints to the serial monitor

    if(Serial.available())  // If stuff was typed in the serial monitor
      // Send any characters the Serial monitor prints to the bluetooth
    // and loop forever and ever!

However, I would like to convert each string of characters into a number of type float, so that I can use it in a GPS program which I have already coded. Each string that I am receiving from the android device is separated by a comma. Each string that is separated by a comma represents a waypoint, which I need to use in the GPS program. Therefore, I need to separate each string so that I can work on it independently. Would you mind helping me with which functions to use, and how to go about this? I am a relative beginner to Arduino, so any help would be greatly appreciated.



Check out strtok() and dbltostr().

Each string that I am receiving from the android device is separated by a comma.

You aren't receiving a string. You are dealing with each individual character as if it was the only thing in the world.

The first thing that you need to do is collect the data in an array of char, keeping the array NULL terminated, until you get a comma. Then, convert the string to a value (atof()) and store the value somewhere. Then, reset the string to empty, by putting a NULL in position 0.