// Include the required Wire library for I2C
#include
int x = 0;
int ledPin1 = 9;
int ledPin2 = 10;
int ledPin3 = 11;
void setup() {
// Define the LED pin as Output
pinMode (LED_BUILTIN, OUTPUT);
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);
Serial.begin(9600);
// Start the I2C Bus as Slave on address 9
Wire.begin(9);
// Attach a function to trigger when something is received.
Wire.onReceive(receiveEvent);
}
void receiveEvent(int bytes) {
x = Wire.read(); // read one character from the I2C
}
void loop() {
//If value received is 0 blink LED for 200 ms
Serial.println(x);
if (x == 0) {
digitalWrite(ledPin3, HIGH);
delay(400);
digitalWrite(ledPin3, LOW);
delay(400);
}
//If value received is 3 blink LED for 400 ms
if (x == 2) {
digitalWrite(ledPin2, HIGH);
delay(400);
digitalWrite(ledPin2, LOW);
delay(400);
}
if (x == 4) {
digitalWrite(ledPin1, HIGH);
delay(400);
digitalWrite(ledPin1, LOW);
delay(400);
}
else
{ digitalWrite(ledPin1, HIGH);
digitalWrite(ledPin2, HIGH);
digitalWrite(ledPin3, HIGH);
delay(400);
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, LOW);
digitalWrite(ledPin3, LOW);
delay(400);
}
}