I’ve also had some trouble with this library recently. In a few cases, I’ll upload the code, the code will run correctly on the first shot, and then I’ll unplug and plug the Arduino (connected to an ATtiny via i2c) back in and the code wont run. Kinda wierd, but I suspect that this has to do with the lack of onRequest() functionality.
Right now, I’ve just had success with some example code from the usiTwiSlave library, that I discovered here:
I discovered it from Jochen Toppe’s blog.http://jtoee.com/2009/12/connecting-multiple-avrarduinos-via-i2ctwi/ Jochen’s kindly posted some awesome example code where he’s communicating with an Arduino and an ATtiny2313 via i2c.
Getting the code to compile from the repository (the first link) was a bit tricky, though. The two files you’ll want are in this directory:
I created a new library in my Arduino sketchbook called usiTwiSlave and dropped the two files: usiTwiSlave.c and usiTwiSlave.h inside there. Then, I changed the .c extension to a .cpp extension on usiTwiSlave.c . (The compiler complains about some of the library’s methods being “undefined” otherwise, since it’s not actually compiling the .c file.)
(Don’t forget to restart the IDE after adding a new library!)
To get Jochen’s Arduino code working, I changed Wire.send to Wire.write an Wire.receive to Wire.read**.**
To get his ATtiny2313 working on an '85, I added these headers:
**__ <strong>**#include <avr/io.h> #include <avr/interrupt.h> #include <avr/pgmspace.h> #include <usiTwiSlave.h>**</strong> __**
Note: some weirdness that I don’t really understand, hehe. I noticed that the compiler complains at me if I put #include <usiTwiSlave.h> at the top of these headers, but it’s A-ok with compiling this code if that statement is at the bottom of the list. :~
One final note: to upload this, I configured my Arduino IDE to support the ATtiny85 using the High-Low Tech tutorial for Arduino 1.0. Lastly, I’m using a Sparkfun “AVR pocket programmer” to upload code to the ATtinys (although I don’t think that matters).
**All-in-all, I got Jochen’s examples to work (Thanks, Jochen)! I hope yours go just as well. **
Here’s my working modified version of Jochen’s code below for the ATtiny85:
(The Arduino version is just “copy ‘n’ pasted” from his blog with #include <Wire.h> at the top!)
#include <usiTwiSlave.h> // Why on earth this has to be at the bottom, I have no idea.
// attiny85 code
// initialize as slave with id 1
// enable interrupts (must be there, i2c needs them!)
// handle commands
// check if data is in the i2c receive buffer
// get it
uint8_t b = usiTwiReceiveByte();
// echo it back
// Do something else while waiting for the TWI transceiver to complete.
asm volatile (“NOP” ::);