Ok, I tried another example, one where I'm relying on LED's instead of println statements to debug whether the I2C is working or not. In addition, I'm also tying the 5V line as well as pin4, pin5, and GND together now. And the two Arduinos are running off the exact same 5V DC adapter, one being powered off the other. But still no joy. The Reader always sees X is 0.
Writer Arduino:
#include <Wire.h>
#define LED_PIN 13
byte x = 0;
void setup()
{
Wire.begin(); // Start I2C Bus as Master
pinMode(LED_PIN, OUTPUT);
digitalWrite(LED_PIN, LOW);
}
void loop()
{
Wire.beginTransmission(9); // transmit to device #9
Wire.write(x); // sends x
Wire.endTransmission(); // stop transmitting
x++;
if (x > 5) x=0;
if (x == 0) digitalWrite(LED_PIN, HIGH);
if (x > 0) digitalWrite(LED_PIN, LOW);
delay(2000);
}
Reader/Receiver Arduino:
#include <Wire.h>
#define LED_PIN 13
int x;
void setup() {
Wire.begin(9); // Start I2C Bus as a Slave (Device Number 9)
Wire.onReceive(receiveEvent); // register event
pinMode(LED_PIN, OUTPUT);
pinMode(LED_1, OUTPUT);
pinMode(LED_2, OUTPUT);
digitalWrite(LED_PIN, HIGH);
digitalWrite(LED_1, LOW);
digitalWrite(LED_2, LOW);
x = 0;
}
void loop() {
//If value received is 0 blink LED 1
if (x == 0) {
digitalWrite(LED_PIN, HIGH);
delay(200);
digitalWrite(LED_PIN, LOW);
delay(200);
}
}
void receiveEvent(int howMany) {
x = Wire.read(); // receive byte as an integer
}