I'm trying to connect two Arduinos according to the example in Example
I added a bit of debug prints so the code looks like:
#include <Wire.h>
// Include the required Wire library for I2C<br>#include <Wire.h>
int LED = 13;
int x = 0;
void setup() {
// Define the LED pin as Output
pinMode (LED, OUTPUT);
// Start the I2C Bus as Slave on address 9
Wire.begin(9);
// Attach a function to trigger when something is received.
Wire.onReceive(receiveEvent);
Serial.begin(9600);
}
void receiveEvent(int bytes) {
x = Wire.read(); // read one character from the I2C
Serial.println(x);
}
void loop() {
//If value received is 0 blink LED for 200 ms
Serial.print("x = '");
Serial.print(x);
Serial.println("'");
if (x == '0') {
Serial.println("Do 0");
digitalWrite(LED, HIGH);
delay(200);
digitalWrite(LED, LOW);
delay(200);
}
//If value received is 3 blink LED for 400 ms
if (x == '3') {
Serial.println("Do 3");
digitalWrite(LED, HIGH);
delay(400);
digitalWrite(LED, LOW);
delay(400);
}
}
The strange thing is, the terminal gives the following information:
...
x = '2'
x = '2'
x = '2'
x = '2'
x = '2'
x3= '3'
x = '3'
x = '3'
x = '3'
x = '3'
...
However, I never see the expected "Do 3" lines. Why is that? I assume if (x == '3) would return true when x = '3'?
Also I see the x3 = '3'instead of x = '3'... now I print x also while an event is received, but are those in two different threads since the expected text would be "3x = '3'") or "x = '3'3"
if (x == 3)?