jacksonliam

Untitled

Dec 21st, 2013
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <Wire.h>
  2. #define BUFFER_SIZE (192)
  3.  
  4. unsigned char buf[BUFFER_SIZE];
  5.  
  6. void setup()
  7. {
  8.   Wire.begin(4);                // join i2c bus with address #4
  9.   Wire.onReceive(receiveEvent); // register event
  10. }
  11.  
  12. void loop()
  13. {
  14.   //Do whatever you need to with buf in here.
  15. /* 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);
  16. Then the second 96 bytes are passed like this whateverFunction(buf + 96, 96) */
  17.  
  18.   delay(100); //remove this when you have code in this loop
  19. }
  20.  
  21. // function that executes whenever data is received from master
  22. // this function is registered as an event, see setup()
  23. // this is blocking, so no other code will run while this is receiving,
  24. // this will only work if you send all 192 bytes at once.
  25. void receiveEvent(int howMany)
  26. {
  27.   unsigned char i = 0;
  28.   while(Wire.available() && (i < BUFFER_SIZE))
  29.   {
  30.     buf[i++] = Wire.read(); // receive byte
  31.   }
  32. }
Advertisement
Add Comment
Please, Sign In to add comment