highByte and lowByte removing leading zeros

I am trying to send an integer (10000) over I2C between two arduino Uno's. However, the leading zeros are being removed, resulting in an incorrect reconstruction. is there a way to keep the leading zeros?

MASTER

// Wire Master Writer
// by Nicholas Zambetti <http://www.zambetti.com>

// Demonstrates use of the Wire library
// Writes data to an I2C/TWI slave device
// Refer to the "Wire Slave Receiver" example for use with this

// Created 29 March 2006

// This example code is in the public domain.


#include <Wire.h>

void setup() {
  Wire.begin(); // join i2c bus (address optional for master)
}


int data = 10000;

byte High = highByte(data);
byte Low = lowByte(data);

void loop() {
  Wire.beginTransmission(8); // transmit to device #8
  Wire.write(High);        // sends five bytes
  Wire.write(Low);              // sends one byte
  Wire.endTransmission();    // stop transmitting

 
  delay(500);
}

SLAVE

// Wire Slave Receiver
// by Nicholas Zambetti <http://www.zambetti.com>

// Demonstrates use of the Wire library
// Receives data as an I2C/TWI slave device
// Refer to the "Wire Master Writer" example for use with this

// Created 29 March 2006

// This example code is in the public domain.


#include <Wire.h>

void setup() {
  Wire.begin(8);                // join i2c bus with address #8
  Wire.onReceive(receiveEvent); // register event
  Serial.begin(9600);           // start serial for output


}

void loop() {
}

// function that executes whenever data is received from master
// this function is registered as an event, see setup()
void receiveEvent(int howMany) {
  while (1 < Wire.available()) { // loop through all but the last
    byte High = Wire.read(); // receive byte as a character
    Serial.println(High, BIN);         // print the character
  
  byte Low = Wire.read();    // receive byte as an integer
  Serial.println(Low, BIN);         // print the integer

  int data_rec = word(High>>8, Low);
  Serial.println(data_rec, BIN);
  Serial.println(data_rec);

}
}

Thanks

Wire.write(High); // sends five bytes

No it does not, that sends one byte, the high part of data (0x27)

So your sending two bytes, 0x27 and 0x10, the high and low parts of the integer data ..........

And rather than leave us guessing, show us what you mean by 'removing leading zeros'

Sorry, that comment should have been removed from the example code after i modified it.

Yes, that is the idea. However, when the code is run, it prints out the following

100111
10000
10000
16

so below is what the code says, what it output, and what i expected;

10000 == 0010011100010000

Serial.println(High, BIN); // OUTPUT - 100111 // expected the first 8 digits of 0010011100010000

Serial.println(Low, BIN); // OUTPUT - 100000 // expected the last 8 digits of 0010011100010000

Serial.println(data_rec, BIN); // OUTPUT - 10000 // expected 0010011100010000 

Serial.println(data_rec); // OUTPUT - 16 // expected 10000

So there was an error in the line

 int data_rec = word(High<<&8, Low);

which i have now changed to

 int data_rec = (High<<8| Low);

and the final output works and gives the correct final integer. However, even then it is ommitting leading zeros with

Serial.println(data_rec, BIN); // OUTPUT - 10011100010000// expected 0010011100010000

so why are leading zeros ommitted and in future how can i stop this?

Thanks

Serial.print() omits the leading zeros in the printout.

If you want to see them you need to write some code that explicitly prints a 0 in the correct place.

...R

Hi Robin2

As long as it is just the Serial.print function omitting them and the value itself doesn't thats fine. Thanks for the clarification

Ross

Ross46:

Serial.println(data_rec, BIN); // OUTPUT - 10011100010000// expected 0010011100010000

so why are leading zeros ommitted and in future how can i stop this?

void setup()
{

  Serial.begin(9600);
  int data_rec = 0x2710; //10000 decimal = 0010 0111 0001 0000 binary
  Serial.println(data_rec, BIN); // OUTPUT - 10011100010000// expected 0010011100010000
  //------------------------------------
  for (int i = 15; i >= 0; i--)
  {
    Serial.print(bitRead(data_rec, i)); //prints: 0010 0111 0001 0000
  }
}

void loop() 
{
  
}

Thanks GolamMostafa. That looks perfect. The only reason i was using Serial.print was to debug and it was confusing me, but this should help in the future. Thanks!

Ross