1

I am attempting to send a simple 1 byte value from one arduino to another using I2C with the Wire library. From reading I see that this is typically done using the event monitor supplied by the Wire library, but this has caused problems for me because the handler function may only have one argument and it must be a void function or else the library throws errors during compilation. So I have a couple of questions about what is or is not possible with the Wire library short of modifying the source:

  1. Should the Wire.available() function respond to attempted transmissions if the event handler referenced above does not exist? My first idea was to simply write my own function that would be called whenever this value is not 0, but it appears that this value never changes - unless the event handler is used.

  2. Can you update variables which reside outside of a function from inside of the function WITHOUT passing those variables as pointer arguments? Because recall from above that I cannot pass any arguments besides the one specified for the number of bytes received.

I cannot understand how this library could be useful for Arduino to Arduino communication if it is literally not possible to use the transmitted values anywhere other than inside of the event handler. My next step is start modifying the source to suit my needs, but I would imagine what I'm describing is a basic thing and would be shocked to find out that it's not possible without modification.

3
  • 2
    Why do you think it's not possible to use it elsewhere? What about global variables and volatile modifier?
    – KIIV
    Commented Nov 25, 2016 at 19:44
  • One word: Scope. Google it.
    – Majenko
    Commented Nov 25, 2016 at 20:18
  • I have thought about the volatile modifier, but I was under the impression that this simply means the variable can be changed by an interrupt which means the variable can change in the middle of an iteration of the main function. However, I do not believe this translates to a regular void function having access to that variable. Scope is exactly the problem here is it not? If I set variables using Wire.read() inside of the event handler (in the scope of that function) then how am I supposed to get those values into the scope of the main function without passing that function pointers?
    – Josh Wiens
    Commented Nov 25, 2016 at 20:52

2 Answers 2

2

I see now what the issue was. But to answer my questions:

  1. Wire.available(); will not work if called outside of an event handler. I confirmed this while reading through the Wire.cpp source code and it explicitly states that the function should only be called from an event handler.

  2. It is as simple as only a matter of updating global variables. I feel quite silly for not realizing that simply declaring variables outside of a function gives ANY function access to it rather than only the main function.

I see that the other answer mentions applying the volatile modification to the variables, but this is only necessary when the function in question is attached to an interrupt handler.

1
  • this is only necessary when the function in question is attached to an interrupt handler. - yes, which applies in your case. :)
    – Nick Gammon
    Commented Nov 26, 2016 at 8:38
1

Can you update variables which reside outside of a function from inside of the function WITHOUT passing those variables as pointer arguments?

Yes, that's exactly what global variables are for. Make them volatile as they will be updated inside an ISR (or more exactly, a function called from an ISR).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.