Imagine i have a code, that loops, and at one point there is i2c request.
Is loop() paused right away when there is Wire.onReceive() or loop is finished then Wire.onReceive() is called?
Arduino Stack Exchange is a question and answer site for developers of open-source hardware and software that is compatible with Arduino. It only takes a minute to sign up.Sign up to join this community
The onReceive callback is called from an interrupt service routine (ISR). As the name says, interrupts are "interrupting" the currently executing code almost immediately. And "almost immediately" means that it only will take a few clock cycles. (This is the reason why changing data in an ISR can corrupt data, since the ISR may change a variable, when the main code tries to read it)
So the Arduino will not wait for the
loop() function to end. It will interrupt in the line, where it was when the interrupt occured, and return to it after the ISR returns.