this code initiates the connection between the Master board and the slave board. then it send “x” to turn on the LED and then sends “y” to turn of the LED connected to slave board.
#include <Wire.h>
void setup()
{
Wire.begin(); // join i2c bus
}
void loop()
{
Wire.beginTransmission(1); // transmit to device #1
Wire.write("x");// sends x to turn on LED
delay(750);
Wire.write("y");// sends y to turn off LED
delay(500);
Wire.endTransmission(); // stop transmitting
}
this code defines the pins for LED output then begins connection upon recieving the signal from the Master. then initiates the value for the led to low when starting. then in the loop it says when receiving “x” turn on LED and when receiving “y” from Master turn off the LED.
#include <TinyWire.h>
int LED = 3;
void setup() {
pinMode(LED, OUTPUT); //sets LED as output
TinyWire.begin(1); //stablish an I2C connection on slave #1
TinyWire.onReceive( onI2CReceive );//register event
digitalWrite(LED,LOW);// Turn LED off
}
void loop() {
}
void onI2CReceive(){
while(TinyWire.available()){ // while the connection is availabe
if(TinyWire.read()=='x') // // check if the signal is x
{
digitalWrite(LED,HIGH ); // Turn LED on
}
else{
digitalWrite(LED,LOW);// Turn LED off
}
}
}