And below is the Master code running on the Uno. It alternates between sending code 30d and 31d to the slave to toggle the slave LED, and echos what was sent and received to the serial monitor.
#include <Wire.h>
// Arduino Uno I2C: A4 (SDA) and A5 (SCL). Support I2C (TWI) communication using the Wire library
#define I2C_SLAVE_ADDR 0x10 // I2C slave address
#define LED_PIN 13 // Uno LED Pin
byte x = 31;
byte rdata = 0;
void setup(){
Wire.begin(); // start I2C Bus as Master
Serial.begin(38400); // start serial to send test output
Serial.println("I2C Test Application"); // print out heading string to COM
pinMode(LED_PIN, OUTPUT); // set up LED for visual output
LED_Blink(LED_PIN, 1); // show it's alive with 2 long flashes
}
void loop(){
Serial.print("Sending x = ");
Serial.print(x, DEC);
LED_Flash(LED_PIN, 3);
Wire.beginTransmission(I2C_SLAVE_ADDR); // transmit to slave device
Wire.send(x); // sends x
Wire.endTransmission(); // stop transmitting
Wire.requestFrom(I2C_SLAVE_ADDR, 1); // request single byte from slave
if (Wire.available()){
rdata = Wire.receive();
LED_Flash(LED_PIN, 2); // flash out slave byte on LED
Serial.print(" | Received x = ");
Serial.println(rdata, DEC); // print out slave byte to Serial monitor
}
else{
Serial.println(" | Received nothing");
}
if (x == 31) // toggle x control code
x = 30;
else
x = 31;
delay(500); // brief pause then loop again
}
// LED blinker for status
void LED_Blink(byte pin, byte times){
for (byte i=0; i< times; i++){
digitalWrite(pin,HIGH);
delay (1000);
digitalWrite(pin,LOW);
delay (1000);
}
}
// LED flash for data (faster blinks than LED_blink)
void LED_Flash(byte pin, byte times){
for (byte i=0; i< times; i++){
digitalWrite(pin,HIGH);
delay (250);
digitalWrite(pin,LOW);
delay (175);
}
}