On simultaneous master/slave: Yes and no. You can initiate the
Wire library as slave and despite of than using
Wire.beginTransmission() and it's siblings to send as a master. The library will configure the hardware as master only for the time it needs to send. But there are 2 problems:
- When you have multiple masters on the same bus, they have to continuously watch the bus, so that they are only starting a transmission, when the bus is free. The
Wire library doesn't seem to do that.
- If two masters start their transmission at the same time, the data will be good, as long all transmitted bits are equal. For other cases the I2C protocol has a rule: When 2 masters start a transmission at the same time, the first one to write a
1 to the bus (HIGH value) has to immediately back of to slave mode. (This is because to write a
1 to the data line the pullup resistors are used. When another master tries to write
0 to the bus (LOW value on SDA) he wins against the pullups, effectively writing
0 to the port, and not
As mentioned on this Github issue there are some workaround, though I haven't tested them myself until now. All in all I2C multi-master may be to complex at this point.
As another option, you can use an extra line, connecting master and slaves (use the internal pullup on the masters side). Any slave can at any time write a
LOW to this line, triggering the master to request data from all slaves (maybe via external interrupt or pin change interrupt).
Note: I don't know, for what purposes you used all your timers, but maybe there is a way to share one of them with other hardware, so that you don't have to use the blocking
pulseIn() function. Or maybe you can tweak the timeout value of the function to a fitting value.