I2C Switching from Slave_Receiver to Master_Writer and v.v

For a project I have 2 arduino's. The input/output is serial and the connection is I2C.
So the communication between both boards are if they are connected via serial.
When serial input is coming on #1 the I am writing from #1 the Master_Writer using Write.write.
When there is no input the it must become a Slave_Receiver.

For receiving on #2 I am using Wire.onReceive(). This last one is an interrupt routine.
But...I have also serial input on #2 and this I want to send to #1 via I2C.
So #2 as Slave_receiver must change to Master_Writer.

Now my question is, what is the right way to change from Slave_Receiver to Master_Writer
and otherwise.

The second example in this Arduino to Arduino I2C Tutorial illustrates swapping roles.

...R

owkee, this example had not found yet. But I had already somekind of this. Only problem is the interrupt which is set by onReceive. Just cleared TWCR = 0x00 before wire.transmit. First run does nothing, 2nd run works, then 4th run works. And that is the end. Meanwhile on the other Arduino it works similair like in that example and that’s works perfect. So maybe only setting the TWEN back. Hereby a photo to see where I am busy with. Actually I need adressing to each different protocol for forward and backwards on eventually on request. It works already but I2C is sometimes a hard protocol.

Image from Reply #2 so we don't have to download it. See this Simple Image Posting Guide

...R

Your image is on its side and you have not explained what it is intended to illustrate.

In Reply #2 you say

First run does nothing, 2nd run works, then 4th run works. And that is the end. Meanwhile on the other Arduino it works similair like in that example and that's works perfect.

What program are you referring to? Is this something you experienced when using the program from my Tutorial?

...R

No, it's my own programm. The photo you see is my own private network structure containing a control unit (0x21), 2x uno as hub/forward port(0x10 & 0x15) and Atmega (0x03) as TDM/frequency hopping transmitter. Both UNO's are in one box and the only port left between them both was I2C. Normally you use the serial port. But that I have both already occupied by LORA. The UNO (0x15) is special for local access and for the database of a website. To make an 2 UNO listening if data is availble with all 3 protocols separately is not so difficult. Only the I2C is making problems. So the best way is to lowlevel activate and de-activate the I2C. Or 2 extra datalines like in a way of RTS and CTS could be an solution.

pe1mxp:
So the best way is to lowlevel activate and de-activate the I2C. Or 2 extra datalines like in a way of RTS and CTS could be an solution.

I can't say I understand.

Are you saying that the approach in my Tutorial is not suitable for your project - which would not surprise me. I am new to I2C and I would be interested to know why my system does not suit your requirement.

...R

Well, your programm works very good in a end user configuration. Therefor you have the I2C protocol with both sides detecting the SDL what is hardware controlled. And for 2 masters arbitage is working well. Till now I have only used I2C for an LCD. Actually I want the I2C let things do what you normal should do with serial. It is possible if you directly programm the registers but not in the standard way.

pe1mxp:
Well, your programm works very good in a end user configuration. Therefor you have the I2C protocol with both sides detecting the SDL what is hardware controlled. And for 2 masters arbitage is working well. Till now I have only used I2C for an LCD. Actually I want the I2C let things do what you normal should do with serial. It is possible if you directly programm the registers but not in the standard way.

I'm sorry, but I am still struggling to understand your requirement and your problem.

The purpose of my Tutorial is to show how I2C can be used as an alternative to Serial.

...R