I know i've asked similar questions in the past (atleast related to serial parsing), and there are lots of previous questions asked which i have read all i can find so please don't shoot me down,
but i'm trying to read in data of the form (34.4543,100.54930) (numbers are irrelevant) from the serial moniter and store the two values as floating variables.
I can read the data into a buffer that i can now do, its bringing back the data to a float in which i have trouble. From what ive found on the net i think i need too multiply the values by a factor (say a 1000) to bring them to a long (setting a limit on the allowed input number length), and use either the union function, or convert the Ascii to bytes, then multiply the preceeding value by 10, and add the next value similar to Messages TO Arduino from Serial Monitor- FA1smtoard.
Ive also read tutorials on sprintf() and atoi() but i'm just not sure one which i need too work.
this is one of the variations of my code which i know doesn't work, but ive made several attempts, and you can atleast see the basis of my code
float headingLat;
float headingLong;
boolean storeString;
int index_rf = 0;
byte buffer_rf[30];
char startChar_rf = '(';
char endChar_rf = ')';
float lat;
int incomingbyte_rf;
union u_tag {
byte b[30];
long fval;
} u;
void setup() {
// initialize serial communication:
Serial.begin(9600);
}
void loop() {
while(Serial.available() > 0) {
char incomingbyte_rf = Serial.read();
if(incomingbyte_rf == startChar_rf){
index_rf = 0;
storeString = true;
}
if(storeString){
if(incomingbyte_rf==endChar_rf){
buffer_rf[index_rf] = 0;
storeString = false;
Serial.println(buffer_rf[0]);
Serial.println(buffer_rf[1]);
Serial.println(buffer_rf[2]);
Serial.println(buffer_rf[3]); //debugging, just to see if it prints the right order/right no's.
Serial.println(buffer_rf[4]);
Serial.println(buffer_rf[5]);
//Might need ascii to binary converter here first?
u.b[0] = buffer_rf[1];
u.b[1] = buffer_rf[2];
u.b[2] = buffer_rf[3];
u.b[3] = buffer_rf[4];
lat = u.fval;
Serial.println(lat);
}
else{
buffer_rf[index_rf++] = incomingbyte_rf;
}
}
}
}
Thanks for any advice.