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");
}
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.
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!
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.
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:
}