I2C interrupts get interrupted by other I2C usage

I would like to be able to setup the interrupts and have my arduino respond to master requests, but I also want it to be able to query sensors on the I2C bus itself. However, when I uncomment the example code for an atlas scientific pH sensor, the interrupts then stop working. Do I need to re enable the interrupts at the end of any other code? Or is this not possible?

For example in my setup I have:

Wire.onReceive(receiveDataPacket); // register talk event
Wire.onRequest(slavesRespond); // register callback event

both set, and this works great unless I add in the atlas scientific code and request pH:

float readpH() {
float internal_ph_float; //float var used to hold the float value of the pH. 
time_=1800;
//Wire.beginTransmission(ezophaddress); //call the circuit by its ID number.
//Wire.write('r'); //transmit the command that was sent through the serial port.
//Wire.endTransmission(); 

full file here:

Since you are new here, I won't chew on you for not posting your code.

Are you calling Wire.begin(xx) in your setup? the "xx" should be the I2C device address you are wanting. Insure there is no other device with that I2C address.

edit: If you are calling the setup function in that library, it is setting the I2C address as 107.

The basic Arduino library doesn't allow you to be a slave and a master. If you give Wire.begin() an address, then you are a slave, always a slave.

You could simply call Wire.begin() again each time you want to switch from slave to master. Depending on your plans this may or may not work well for you.

MorganS:
The basic Arduino library doesn't allow you to be a slave and a master. If you give Wire.begin() an address, then you are a slave, always a slave.

Are you sure? When was this limitation imposed?

I have been using the Wire library to support multiple masters sharing the same I2C bus. I did modify the library to allow multiple Wire.write() operations during the onRequestEvent() callback.

Each of the master uses Wire.begin(slaveAddress) and onReceiveEvent(), onRequestEvent() handlers to pass info to each other. They share control of EEPROMS, RTCC, IOExpanders.

I use the repeated Start protocol to maintain ownership of the bus during transactions. I have to handle collision/retry in my software.

Chuck.

Maybe because I originally tested I2C on a Due and the Due implementation has always been behind the main Arduino track.

@SurferTim I posted the full file here if you'd like to see it:

I'm calling Wire.begin(107) in my setup

@ChuckTodd might you have any example code for this? I'm kind of confused as I only call Wire.begin once so I'm not certain if I should just be spamming it in my loop, or only after the pH code fires off?

My apology. I missed that.

I also missed the part about being a slave and master at the same time. ChuckTodd might be better qualified to cover that. I wasn't aware that was possible, but I can imagine how a person could do that.

completely understandable that you might not even recognize that as an arduino program, it is actually a massive scratchpad with way too much crap copy pasta'd into it. I know refactor, but it's more fun to try and see if you can bolt more crap on before it blows up. Someday I'll refactor it, I promise myself that each time I have to hunt for some obscure thing.

thoth:
@SurferTim I posted the full file here if you'd like to see it:

hydrobot/arduino/hydrobot/hydrobot.ino at master · Bokbot/hydrobot · GitHub

I'm calling Wire.begin(107) in my setup

@ChuckTodd might you have any example code for this? I'm kind of confused as I only call Wire.begin once so I'm not certain if I should just be spamming it in my loop, or only after the pH code fires off?

Attached is a Zip file of my Wire library with examples:

It is a Wire.h replacement. with a lot of new features. Just use the Arduino library manager to install it. After it is installed, you will start receiving this Warning message :

Multiple libraries were found for "Wire.h"
 Used: C:\Users\user\Documents\Arduino\libraries\Wire
 Not used: C:\Program Files\Arduino\hardware\arduino\avr\libraries\Wire

don't be alarmed. It is actually telling you the truth. Because my Wire.h is superseding the Arduino's default Wire.h.

These directories may be different, it depends on where your Arduino is installed.

All you have to do to get rid of my Wire library, is just to delete the subdirectory. It will be in your libraries folder.

In my example I would delete C:\Users\user\Documents\Arduino\libraries\Wire

The Example called 'master.ino' is the one you want to look at. It requires 2 UNO's both powered up, A4-A4, A5-A5, Gnd-Gnd, and ONE of the UNO's needs a jumper from A0 to GND. This jumper is used to select it's I2C address one uses 0x58, the other uses 0x59. If you start two Instances of the Arduino Environment and download the code to each UNO, and open both Serial Monitors, you can see the action, and control each one.

My Modified Wire.h supports better error recovery, and allows simpler multibyte transfers. It uses Nick Gammon's I2C anything templates.

Chuck.

P.S. to test the difference between the 'standard' Wire library and mine, short out the SDA(A4) to ground. The standard Wire library will hang until the short is removed, mine will return a bus timeout Error after one second. (I needed this feature for one of my applications were I2C sensors were physically destroyed during normal use.)

Wire.zip (19.7 KB)

Wow great stuff Chuck, do you have this in a public git repo somewhere?

EDIT: found a github repo here:

thoth:
Wow great stuff Chuck, do you have this in a public git repo somewhere?

EDIT: found a github repo here:
GitHub - stickbreaker/Wire: Modified Arduino Wire library

Most newbies have no clue what github is. So, I try to make it simple.

Chuck.

Yes, stickbreaker is me.

Who are you calling N00b? Lol, just kidding.

Unfortunately, I was been programming the ATmega series before there was even a thing called 'arduino'. But most of the code I wrote back then has gone to proprietary graveyards, this is why I'd greatly prefer to use something that is publicly available. Speaking of, I hate to be a stickler, but could you upload a license into that as well? Without a license you fall into the weird 'unlicense' category. Your choice on license of course, github even runs a fun site called:

looks like the original is GPLv2:

probably easiest to keep it that way. Notice, I'm not complaining or bashing, just requesting the license be clear so that I can use it. Regardless your examples helped me greatly and I very much appreciate your input!