Transferring 5 variables or values in I2C connection

Hi all...

I have connected arduino mega and arduino uno through I2C serial communication.
I want mega as the master and uno as the slave.
The master will control the uno with several instructions give by five push buttons. So, there will be more than one digital value that will write and send to drive slave.
Since I have been looking for the guide, there are most tutorials or some like that told only one variable that send from master to slave or vice versa. I can copy that and work it out. But for My chase, there is not any idea yet.
Yes of course I'm still beginer.

Please anyone, give me some guide.

Thank you...

You need to create a protocol or find a library that does that for you.

Have a look at the NMEA 0183 protocol that is used by GPS modules for a general principle.

@Klaus_K, this is about I2C. We send variables, arrays and structs over I2C.
Even a simple checksum is almost never used. The I2C should work 100% reliable and if it does not, then the functions Wire.endTransmission() and Wire.requestFrom() will indicate a problem.

@nurekoprasetyo, you don't need a protocol. I think Klaus_K was half asleep or has been drinking.
It will be easier for us if you show your sketch for the Master and the Slave.
Only 5 pushbuttons ? Then you can use the 8 bits of a byte and transfer that single byte. Or you can use an array of 5 bytes.

Arduino has nice functions to select bits. They are bitRead() and bitWrite().

This is a tutorial: Use I2C for communication between Arduinos - Exhibition / Gallery - Arduino Forum.
You don't have to use a 'struct', you can use an array or a single byte as well. The code will be a little simpler for an array or a byte.

Koepel:
I think Klaus_K was half asleep or has been drinking.

@Koepel Are you always this nice? If you have a better idea just post it and let the users choose what they find helpful.

You need some rules to send data between two entities, which is a protocol.

Here is one protocol.

Koepel:
Then you can use the 8 bits of a byte and transfer that single byte.

Here is another protocol. This protocol is incomplete and could cause synchronization issues.

Koepel:
Or you can use an array of 5 bytes.

It was just a little innocent joke :wink: to see if you would bite :stuck_out_tongue:
You seem to miss that this is about I2C.

Klaus_K:
This protocol is incomplete and could cause synchronization issues.

No, it does not. That is impossible. This is about I2C. Data is transferred in packages. The Wire library uses packages and clears the buffers before starting a new I2C session.
https://www.i2c-bus.org/

Klaus_K:
You need to create a protocol or find a library that does that for you.

Have a look at the NMEA 0183 protocol that is used by GPS modules for a general principle.

NMEA 0183 - Wikipedia

I have to come in on the side of @Koepel.

For a start most newbies won't have a clue what a protocol is, or even what a library is.

Secondly I can't see why a newbie would be recommended to study the NMEA 0183 protocol when their question is about I2C. It's a bit like suggesting that a budding motor racing driver should learn sailboat racing.

...R

It's also very unclear to me what is the OP's issue. Is it that he doesn't know how to send one of 5 different commands based on the button pressed(or one of 32 commands if simultaneous multiple buttons pressed) or how to make the slave switch activities based on those commands?

As @Koepel says

It will be easier for us if you show your sketch for the Master and the Slave.

Perhaps Elvis has left the building to study the NMEA 0183 protocol :confused:

A protocol is just a fancy term for rules.

I'm sure I2C can send one, two, or billion of bytes in one go, but the receiving end must know how to interpret that stream of bytes.

In the OP's case, the protocol is simply, one byte per command, and encoded in the byte are five bits to control the buttons. It may be simple, but still considered a "protocol"

a set of rules governing the exchange or transmission of data between devices.

nurekoprasetyo:
The master will control the uno with several instructions give by five push buttons. So, there will be more than one digital value that will write and send to drive slave.

Use a C 'struct' to bundle your values together. There are some things you have to be careful with if you are using strings, but for just numbers, this will work fine.