Float to char conversion

Hey there,
I wish to take a GPS coordinates, calculate some stuff, and send Bearing float to another Arduino using I2C. For that reason I need to master I2C, and especially float into char conversion, and vise verso. So I did Google a bit on the subject, and going around the forum came up with this:

float FltValue = 1.34567890;
char ChValue[10];

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
}

void loop() {

  Serial.println("");
  Serial.println("The value is: ");
  dtostrf(FltValue, 10, 8, ChValue);
  for (int i = 0; i < 10; i++)
  {
    Serial.print(ChValue[i]);
  }
  delay (1000);
}

So far it works as expected. Serial do print 1.34567890. However when I try to execute some code after, or before delay function it starts to behave oddly.

For example Serial.print("Some text"); is getting completely ignored. What am I doing wrong?

float FltValue = 1.34567890;
char ChValue[10];

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);

}

void loop() {

  Serial.println("");
  Serial.println("The value is: ");
  dtostrf(FltValue, 10, 8, ChValue);
  for (int i = 0; i < 10; i++)
  {
    Serial.print(ChValue[i]);
  }
  delay (1000);
 Serial.print("Some text");
}

Up the baud rate to 115200 and try Serial.println("sometext").

or

Serial.print("efvedfvdfv");
Serial.println();

works.

a float uses 4 byte on the Arduino UNO.
You could just send these 4 bytes with Wire.h.

But the first question might be: why do you use 2 Arduinos? Why not one Arduino?

1 Like

Why do you want first convert float to char, send it and then convert vice verso? Why do not send float as float?

1 Like

Thanks IdahoWalker! , I did try to change the baud rate to 115200 , as recommended, but still the code after delay is getting ignored.

You just need to change:
char ChValue[11];

If your string is 10 characters long, you can't size your buffer to 10 because of the the terminating byte, resulting in a bad program behavior flooding out of the assigned memory.

1 Like

Working with char arrays, do not forget to allocate space for terminator.
To converting something to string with 10 chars you need char array with 11 elements!

1 Like

Change Serial.print("Some text"); to Serial.println("\nSome text"); and see what goes..

EDIT: Added "\n" to make a new line after the for loop..

1 Like

PS: besides, I don't know why you need a for() statement to print a string, just do this:

...
  Serial.println("");
  Serial.println("The value is: ");
  dtostrf(FltValue, 10, 8, ChValue);
  Serial.println(ChValue);
...
1 Like

and what about using Serial.println() did that work?

Serial.print() and Serial.println() do different things.

1 Like

Please read my post #6...

1 Like

Cat helping you??

@valkan why two Arduinos, and why I2C?

a7

1 Like

good one, thanks for the laugh.

Tx noiasca! It is a project, that runs a lot of things. My plan is to get dedicated Arduinos, one for managing the actuators, other for managing access to internet, and bearing calculations. So I plan when have the course established, to send it to the Arduino that will control a rudder. I will try to find out how to work whit these 4 bytes with wire.h.

I was not aware that I can send float directly via I2C. Still learning :slight_smile:

When I can give you an advise: Stay on one Controller.

"Access Internet" - honestly ... use something like an ESP8266 or an ESP32 on makerfriendly boards.
If you run out of pins - use port expanders.

here is a short demo to "transmit" a float

/*
   send a flaot bytwise to another arduino

*/
float fsend;
char csend[4];

char creceived[4];
float freceived;

void setup() {
  Serial.begin(115200);

  // "Sender Side"
  fsend = 12.34;
  Serial.println(fsend);

  memcpy(csend, &fsend, sizeof (fsend));    // send data

  for (int i = 0; i < 4; i++)
  {
    Serial.print(byte(csend[i]), HEX);
    Serial.print(' ');
  }
  Serial.println();      // // what have we got

  // transmit
  memcpy(creceived, csend, 4); // simulate received bytes

  // "Receiver Side"
  for (int i = 0; i < 4; i++)
  {
    Serial.print(byte(creceived[i]), HEX);  // what have we got
    Serial.print(' ');
  }
  Serial.println();

  memcpy(&freceived, creceived, sizeof (creceived));    // receive data
  Serial.println(freceived);
}

void loop() {
  // put your main code here, to run repeatedly:
}

2 minutes research

this is the output:

12.34
A4 70 45 41 
A4 70 45 41 
12.34
1 Like

DocDoc, and b707 thanks. What you offered seems to be just the solution.

Tx again noiasca, I will try to understand your example, and implement it. What do would you recommend for internet via GSM card?

It is OK, thanks again!

I would say that my advice is not to use the Internet via GSM as the first project.

Are there really places where there is no wi-fi?