Using IIC/I2C/TWI to transfer floating point Between 2 Arduino UNO R3

Hello All,

I have managed to send some floating point from one Arduino to the others using IIC. But I have a small problem. The decimal part of my floating point number is truncated.

I am sending 30.345511 from the master Arduino and at the slave Arduino I only receive 30.34

I have tried to changed the Buffer length in

C:\ProgramData\Arduino\libraries\Wire\utility
define TWI_BUFFER_LENGTH 64

and

C:\ProgramData\Arduino\libraries\Wire
#define BUFFER_LENGTH 64

It did not help.

I know, I am not too far to my goal therefore any input will be appreciated.

Please see the code I am using on the Master side

#include <Wire.h>
#include "I2C_Anything.h"

const byte SLAVE_ADDRESS = 42;

void setup()
{
Wire.begin ();
} // end of setup

void loop()
{

long foo = 42;
float fnumP = 30.343511 ;
float fnumN = -25.236582 ;

// for (float fnum = 1; fnum <= 10; fnum += 0.0015)
for (int i = 1; i <= 10; i += 1)
{
Wire.beginTransmission (SLAVE_ADDRESS);
I2C_writeAnything (fnumP);
I2C_writeAnything (foo++);
I2C_writeAnything (fnumN);
Wire.endTransmission ();
if (i == 9) i = 0 ;
delay (200);
} // end of for

} // end of loop

On the Slave side

#include <Wire.h>
#include "I2C_Anything.h"

const byte MY_ADDRESS = 42;

void setup()
{
Wire.begin (MY_ADDRESS);
Serial.begin (115200);
Wire.onReceive (receiveEvent);
} // end of setup

volatile boolean haveData = false;
volatile float fnumP;
volatile long foo;
volatile float fnumN ;

void loop()
{
if (haveData)
{
Serial.print ("Received fnumP = ");
Serial.println (fnumP);
Serial.print ("Received foo = ");
Serial.println (foo);
Serial.print ("Received fnumN = ");
Serial.println (fnumN);

haveData = false;
} // end if haveData

} // end of loop

// called by interrupt service routine when incoming data arrives
void receiveEvent (int howMany)
{
if (howMany >= (sizeof fnumP) + (sizeof foo) + (sizeof fnumN))
{
I2C_readAnything (fnumP);
I2C_readAnything (foo);
I2C_readAnything (fnumN);
haveData = true;
} // end if have enough data

} // end of receiveEvent

=============

How should I change those codes above to send 30.345511 from the Master and Receive 30.345511 at the slave Arduino?

Many thanks
Kind Regards

Maybe the problem is because Serial.print() defaults to 2 decimal places for floats.

Try this:

Serial.println (fnumP, 6);

Hello Hackscribble ;

Thank you very much, you have solved my problem.

Cheers.

PS: If anybody has seen anywhere a code demonstrating the transfer of floating point data between two Arduino using the SPI Please send me the link.
I want to compare the IIC and SPI Performance and complexity.

Scroll down to "Send and receive any data type" in this excellent SPI resource: Gammon Forum : Electronics : Microprocessors : SPI - Serial Peripheral Interface - for Arduino

Hackscribble

You are a blessing to this community.

Thank you.

I think Nick Gammon who wrote that resource (and much, much else) is the one you should thank :slight_smile: