Hi. Please help me with this one. I've tried different approaches to send more than one byte over I2C, but it only works with just one. I must be messing with the data types, but I'm not able to see how.
It works with just one byte without the array (tested with a sensor), but not with >1. In the example I use 4 bytes as int uses 2, but they should be converted to bytes with (byte), shouldn't they?
Apologies if the error is too obvious, still a lot to learn...
It does to me. Remove all Serial.print()s from you interrupt handler on the slave and it will probably work. This usage of the Serial object inside an interrupt handler is a no-go anyway as it's function depends on interrupts to work which isn't the case inside an interrupt handler. If you fill the buffer you'll end in an endless loop.
The ATmega32U4 does clock stretching while the I2C interrupt to gather the data runs. Your example shows that the ESP8266 doesn't support clock stretching and just continues to send clock ticks and read the data.
I would bet that the return value of the Wire.requestFrom() is not 4 as it should be if there was no error in the transfer.
I will remove the Serial.print() from the slave and verify the return value of Wire.requestFrom(). And defenitely will find more info about clock stretching and interrupt handlers, as they are new concepts to me.
Removed the Serial.print() and the issue is fixed. Thank you for that, pylon!
I have no idea about interrupts. It's going to be fun learning about them.
I have applied the same solution to my project, where the values a differential pressure sensor and a MQ-2 sensor are sent to master, and after a lot of tests, I have noticed that the MQ-2 code was causing the same issue, maybe because it was taking 5 reads from the analogue pin connected to the MQ-2 with a delay of 50 in between them, and returning the average. When sent the raw value it worked. I will analyse the code and move it to the ESP8266.
Are functions outside the requestEvent() function part of the interrupts disabled when this one is called, even though they are called from within requestEvent()?
No, the function is enabled, but you have to treat the code in anyFunction() as it would be inside requestEvent(), so no calls to stuff that needs interrupts enabled (p.e. Serial object) because during the complete run of requestEvent() (and anyFunction() is part of that) interrupts are disabled.