Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Wire.h>
- #define BUFFER_SIZE (192)
- unsigned char buf[BUFFER_SIZE];
- void setup()
- {
- Wire.begin(4); // join i2c bus with address #4
- Wire.onReceive(receiveEvent); // register event
- }
- void loop()
- {
- //Do whatever you need to with buf in here.
- /* you can access the bytes directly as an array, with the first 96 being buf[0] to buf[95] and second buf[96] to buf[191], or if you need to pass to a function whateverFunction(buf) or with the size whateverFunction(buf, 96);
- Then the second 96 bytes are passed like this whateverFunction(buf + 96, 96) */
- delay(100); //remove this when you have code in this loop
- }
- // function that executes whenever data is received from master
- // this function is registered as an event, see setup()
- // this is blocking, so no other code will run while this is receiving,
- // this will only work if you send all 192 bytes at once.
- void receiveEvent(int howMany)
- {
- unsigned char i = 0;
- while(Wire.available() && (i < BUFFER_SIZE))
- {
- buf[i++] = Wire.read(); // receive byte
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment