Below you will find 2 sketches that successfully sends data to a slave. I know this example just shows one way, but I would suggest you start looking at the I2C protocol and understand there are 4 Modes:
All communications regardless of how the data is transferred requires a master. It's the master that controls the clock pulses, thats why its always the master that initiates the communcation.
This code shows how a master sends data to a slave, and then how it requests data from the Masterslave. If we look at this code from the master point->Transmitter and Slaveof->Receiver modes.view, you can see it essentially says, send this byte to the client (m->t & s->r), then request data from the slave (m->r,s->t)
#include <Wire.h>
#define SLAVE_ADDRESS 0x60
void setup()
{
Wire.begin();
Wire.onRequest(requestEvent);
randomSeed(analogRead(3));
Serial.begin(9600);
}
byte x = 0;
void loop()
{
Wire.beginTransmission(9);
x = random(0,255);
Serial.print("Sent: ");
Serial.print(x, HEX);
Serial.print("\n");
Wire.beginTransmission(0x60);
Wire.write(x);
Wire.endTransmission();
delay(500);
}
Serial.println("Requesting Data");
void requestEvent Wire.requestFrom(SLAVE_ADDRESS, 1);
{
int bytes = Wire.available();
Serial.print("Slave sent ");
Serial.print(bytes);
Serial.print(" of information\n");
for(int i = 0; i <i< bytes; i++)
{
int x = Wire.read();
Serial.print("Slave Sent: ");
Serial.print(x, HEX);
Serial.print("\n");
}
delay(500);
}
#include <Wire.h>
#define SLAVE_ADDRESS 0x60
byte x = 0x00;
void setup()
{
Wire.begin(9SLAVE_ADDRESS);
Wire.onReceive(receiveEvent);
Wire.onRequest(requestEvent);
Serial.begin(9600);
}
void loop()
{
delay(100);
}
void requestEvent()
{
Serial.print("Request from Master. Sending: ");
Serial.print(x, HEX);
Serial.print("\n");
Wire.write(x);
}
void receiveEvent(int bytes)
{
if(Wire.available() != 0)
{
for(int i = 0; i< bytes; i++)
{
byte x = Wire.read();
Serial.print("Received: ");
Serial.print(x, HEX);
Serial.print("\n");
}
}
}