Arduino 2 Arduino - Floating point serial communications

Hi,

I am working with two Arduino Uno boards with one acting as a 16x2 LCD using the Sainsmart LCD shield. I wanting to take a sensor value from arduino A to arduino B ising the I2C bus, where it will be displayed on the LCD. I am able to do this using integer numbers however when I try to send a floating point number to be displayed it simply displays 0.

Any help would be appreciated

Master Code:

#include <Wire.h>

void setup()
{
Wire.begin();

void loop()
{
float sensorValue = analogRead(A0);
float y = sensorValue * (5.0 / 1023.0);

int x = y * 1000;

Serial.println(x);

Wire.beginTransmission(4);
Wire.write("Voltage is ");
Wire.write(x);
Wire.endTransmission();

delay(500);
}

Slave Code:

#include <Wire.h>
#include <LiquidCrystal.h>

LiquidCrystal lcd(8, 9, 4, 5, 6, 7);

void setup()
{
lcd.begin(16, 2);
lcd.setCursor(0,0);
lcd.print(“Voltage:”);
Wire.begin(4);
Wire.onReceive(receiveEvent);
Serial.begin(9600);

}

void loop()
{
delay(100);

}

void receiveEvent(int howMany)
{
while(1 < Wire.available())
{
int c = Wire.read();
Serial.print(c);
}
int y = Wire.read();
float x =y/1000;
Serial.println(x,2);
lcd.setCursor(0,2);
lcd.print(x,2);
delay(100);
}

Cheers

Hint: sizeof (float) != sizeof (byte)

Thanks for the reply. I understand what your saying but I am not sure how to amend this. Sorry I am very new to this.

Regards
J

OK, starting with the transmitter

 float y = sensorValue * (5.0 / 1023.0);
 
  Wire.beginTransmission(4);
  Wire.write(&y, sizeof (y));
  Wire.endTransmission();

, see how you go with that.

I can't see the point of the string "Voltage is" being sent.

Sorry that was taken from a demo but I had forgotten to remove it. With the code in place:

#include <Wire.h>

void setup()
{
Wire.begin();
Serial.begin(9600);
}

void loop()
{
float sensorValue = analogRead(A0);
float y = sensorValue * (5.0 / 1023.0);

Serial.println(y);

Wire.beginTransmission(4);
Wire.write(&y,sizeof(y));
Wire.endTransmission();

delay(500);
}

I get the following error:

no matching function for call to "TwoWire::write(float*,unsigned int)’

Thanks

Oops, sorry

Wire.write((uint8_t*)&y,sizeof(y));

Please use code tags when posting code.

Thanks very much. This code compiled and using the serial monitor it appears to be transmitting the correct values. However, I am still only seeing the value 0.00 on my LCD screen. So i am assuming something is still not right in the receiver code

Cheers

That's the second part of the puzzle.
You should now be sending a four byte float one byte at a time.
You did this by persuading the compiler that the float was, in fact a buffer of four consecutive bytes.
The next part is to read those four bytes, and reassemble them into a float.

Any ideas? I tried modifying the code :

#include <Wire.h>
#include <LiquidCrystal.h>

LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
void setup()
{

lcd.begin(16,2);
lcd.setCursor(0,0);
lcd.print(“Voltage:”);
Wire.begin(4);
Serial.begin(9600);
Wire.onReceive(receiveEvent);

}

void loop()
{
delay(100);
}

float receiveEvent(y)
{

while(1 < Wire.available())
{
int c = Wire.read();
Serial.print(c);
}

float x = Wire.read();
lcd.setCursor(0,2);
lcd.print(x);
delay(100);
}

But having no luck

float x = Wire.read();

I already explained that “Wire.read” returns a byte, and that a float is actually four bytes.
You need to read four bytes, one after the other, and assemble them into a float.

 Wire.onReceive(receiveEvent);
float receiveEvent(y)

I’m a little (ok, ok, a lot) surprised that that compiled.
“receiveEvent” should not return anything, and the parameter should be of type “int”.

Please start using code tags.