[Solved] Master Reader not displaying correct values received from Slave Sender

Hello,
I’m using an Arduino Uno to interface with a current transformer for monitoring current. It is correctly reading and printing the current values in the Serial Monitor. I am then using the Uno as a Slave Sender to pass this data to an Arduino MEGA 2560 when it requests as a Master Reader. The Serial Monitor for the Master displays completely different information than what the Serial Monitor for the Slave displays. I’m wondering if I’m not using a correct data type when sending or receiving the information. The following is my code.

Slave Sender:

void setup() {
  Serial.begin(115200);
  Wire.begin(8); // join i2c bus
  Wire.onRequest(requestEvent); // register event
  emon1.current(2, 64.3); // Current: input pin, calibration for first sensor.
  emon2.current(3, 64.3); // Current: input pin, calibration for second sensor.
}

void requestEvent() {
  float Irms1 = emon1.calcIrms(1480);  // Calculate Irms based on first current sensor
  float Irms2 = emon2.calcIrms(1480); // Calculate Irms based on second current sensor
  Serial.println(Irms1);  
  Wire.write((byte)Irms1); // respond with current 1
  Wire.write((byte)Irms2); // respond with current 2
  // as expected by master
}

Master Reader (within the Void loop):

Wire.requestFrom(8, 6);    // request 6 bytes from slave device #8
        while (Wire.available())
        {
          // slave may send less than requested
          float Irms1 = Wire.read(); // receive a byte as float
          float Irms2 = Wire.read();
          genie.WriteObject(GENIE_OBJ_METER, 0, Irms1);
          genie.WriteObject(GENIE_OBJ_METER, 1, Irms2);
          Serial.println(Irms1);

I’ve attached images of the Serial Monitors so that you can see the current values from the Slave and the values that were read and displayed by the Master. The current values are very low because it’s not reading any current right now (the sensor is going to read amperage up to 150A AC).

Thanks for any help.

Master_Serial.JPG

Slave_Serial.JPG

Sketch_RevB_Master.ino (17.7 KB)

Sketch_RevB_Slave.ino (2.33 KB)

Post complete programs. Not snippets.

...R

You have floats, which are 4 bytes. You send a single byte, likely the low byte, which is not going to contain anything you or the computer can reasonably interpret without the other three bytes when you try and convert it back to the float.

Send and read the full 4 bytes.

Robin2: Post complete programs. Not snippets.

...R

Posting the whole code would exceed the character limit of the forum. Instead, I've attached my sketches to the original post.

cattledog: You have floats, which are 4 bytes. You send a single byte, likely the low byte, which is not going to contain anything you or the computer can reasonably interpret without the other three bytes when you try and convert it back to the float.

Send and read the full 4 bytes.

I didn't know that byte would only send 1 byte. I expected it to store this information and send it as requested. Any suggestions on how to send the entire current value through?

Any suggestions on how to send the entire current value through?

  Wire.write((byte *)Irms1, 4); // respond with current 1

Sends all 4 bytes that make up the float.

PaulS:

  Wire.write((byte *)Irms1, 4); // respond with current 1

Sends all 4 bytes that make up the float.

Yes, this worked. Thank you. Simple solution.