send 'unsigned long' with Wire.write

Hi guys.
I know my question has been repeated a LOT (!) but I'm despaired to find a solution on this.
I literally tested every code that I found around this but no luck.. I'm stock for a Month now :confused:

I have an 'unsigned long' variable which I got from my RFID scanner and I want to pass it to another arduino by I2C

Please HELP me!

l__Alborz__l:
Hi guys.
I know my question has been repeated a LOT (!) but I'm despaired to find a solution on this.
I literally tested every code that I found around this but no luck.. I'm stock for a Month now :confused:

Please HELP me!

A poster gives a meaningful title to his Thread so that it easily attracts the attentions of passers by. Your title, to me, is good enough!

Then comes the post where you describe your problem with clarity and brevity; unfortunately, you have not told anything in your post about your problem though it is understood that you are looking for a way of transferring an unsigned long number (say: 0x12345678) over the I2C Bus.

1. I2C Bus is a 'byte' oriented communication link. It sends (receives) one byte at a time.

2. Therefore you need to 'somehow' break your given unsigned long (32-bit) data into four separate bytes without destroying the original meaning/weight of your number.

3. The procedures could be:
(1) Use of union data structure.

(2) Use of cast:

unsigned long x = 0x12345678;
byte x1 = (byte)(x>>24);                         //x1 = 0x12
byte x2 = (byte)((x>>16)&0x000000FF);   //x2 = 0x34
..........................

A poster gives a meaningful title to his Thread so that it easily attracts the attentions of passers by. Your title, to me, is good enough!

Then comes the post where you describe your problem with clarity and brevity; unfortunately, you have not told anything in your post about your problem

Yes you’re right… I’m sorry for the poor description of my problem…
thanks for your reply, It did clarify a lot if things for me.

this is my Code:

#include <Wire.h>
#define WireDevice 8
#include "rdm630.h"

rdm630 rfid(6, 0);  

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


void loop() {
  
     byte data[6];
    byte length;
  unsigned long result;
    if(rfid.available() > 0){
        rfid.getData(data,length);

        //concatenate the bytes in the data array to one long which can be 
        //rendered as a decimal number
         result = 
          ((unsigned long int)data[1]<<24) + 
          ((unsigned long int)data[2]<<16) + 
          ((unsigned long int)data[3]<<8) + 
          data[4];              

       if(result != 0) Serial.println(result);
    }


        Wire.beginTransmission(WireDevice);     
        Wire.write(result);
        Wire.endTransmission();    

 delay(100);
}

(1) Use of union data structure.

I saw a couple of examples on union… i wish u would give me ur example about it.

(2) Use of cast:

unsigned long x = 0x12345678;

byte x1 = (byte)(x>>24);                        //x1 = 0x12
byte x2 = (byte)((x>>16)&0x000000FF);  //x2 = 0x34

do u have any example of this? how can I send and receive this chunks?

Thanks in advance

1. Given:

unsigned long x = 0x12345678;

2. How to get 0x12 from the value of x of Step-1?
(1) Shit x to the right by 24-bit positions.

==> we get: 0x00000012      //when we do shift operation, 0 enters from the left

In C, >> is the right shift operator; << is the left shift operator.

(2) Take only 0x12 (1-byte) from the value of Step-2 which is 32-bit. This is done by a process what is called ‘casting’.

(3) Finally, we have these codes:

byte x1 = (byte)(x>>24); //shift does not affect the parent number.

//---------------------------------------------------------------------------
Union Data Structure

union
{
    unsigned long x = 0x12345678;
    byte dataArray[4];
} data;

byte x0 = data.dataArray[0]; //x0 = 0x78
byte x1 = data.dataArray[1]; //x1 = 0x56
byte x2 = data.dataArray[2]; //x2 = 0x34
byte x3 = data.dataArray[3]; //x3 = 0x12

BTW: If you have your data already in this array ‘byte data[6];’, you can execute the following I2C commands to send the data to the destination.

Wire.beginTransmissiion(slaveAddress);
Wire.write(data, sizeof(data));
Wire.endTransmission();

you clarified a lot of things for me… now I can understand most of the things in my sketch.

Wire.write(data, sizeof(data));

I guess it dose actually sends the bytes to the other device but I have no Idea how the Slave arduino can receive and convert the bytes into decimal.
I read alot of threads and this is my final code that I’m pretty sure I messed up the Receive part:

Master:

#include <Wire.h>
#define WireDevice 8
#include "rdm630.h"

rdm630 rfid(6, 0);  //TX-pin of RDM630 connected to Arduino pin 6

void setup() { 
  Serial.begin(9600);
  master();
   rfid.begin();
}

void loop() {
     byte data[6];
    byte length;
  unsigned long result;

  //it will read the tag number with RFID scanner
    if(rfid.available() > 0){
        rfid.getData(data,length);

        //concatenate the bytes in the data array to one long which can be 
        //rendered as a decimal number
         result = 
          ((unsigned long int)data[1]<<24) + 
          ((unsigned long int)data[2]<<16) + 
          ((unsigned long int)data[3]<<8) + 
          data[4];              

//shows the result
       if(result != 0) Serial.println(result);
    }

    //sends 6 bytes to slave arduino
        Wire.beginTransmission(WireDevice); 
        Wire.write(data, sizeof(data));
        Wire.endTransmission();    
  
 delay(100);
}

void master() {
  Wire.begin();
  Serial.println("Master Initialized");
}

Slave:

#include <Wire.h>
#define WireDevice 8

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

void loop() {
  Wire.requestFrom(8, 6);

}

void slave() {
  Wire.begin(8);                
  Wire.onReceive(receiveEvent);
  Serial.println("Slave Initialized");
}

void receiveEvent(int howMany) {

  //reading first four bytes from the 6 incoming bytes
  while (Wire.available()) {
    
    unsigned long result;

  byte data;
  for(int i = 1; i<= howMany; i++)
  {
     data = Wire.read();
    if( i == 1)
    {
       result += ((unsigned long int)data<<24);
    }else  if( i == 2)
    {
       result += ((unsigned long int)data<<16);
    }
     else if( i == 3)
    {
       result += ((unsigned long int)data<<8);
    }
     else if( i == 4)
    {
       result += data;
    }
    Serial.print(result);     
  }
  }
  Serial.println();
  Serial.println();
  delay(1000);
     
}

It dosn’t work and even when I’m not transmitting anything to the slave device, it reads some thing.

Please help me with this…

here is a picture of the slave and the master arduino serial monitors (master is the left one, Slave is the right one).

To get next help, you have to provide these information:

The structure of a 'message frame' you are receiving from the RFID device. (The length in byte, the encoding form (ASCII or binary), any start mark, any end mark, and etc.)

Provide a link to your RFID device.

Provide a link to your RFID device.

this is rdm630 specs:
RDM630

I can't get it..

the Code on my tags are like this: "0013307291" (which '00' will not be read)

Do you understand the following specification of your RFID/RDM Card?
RFID.png

The message (transmission frame) coming from the RFID/RDM Card --
begins with the binary coded value 0x02h (start mark)
contains ASCII coded values (10 charcaters) for data.
contains 2-byte ASCII coded values for the 2-digits (1-byte) of checksum
ends with binary coded value 0x03 (end mark)

The transmission frame length is: 14 bytes.

Write codes for your receiver to comply with above specifications, and you are done.

RFID.png