Mega (Master Writer)/ UNO (Slave Receiver) I2C Problem

Hi all…

I have been working on the master/slave I2C communication between a MEGA (master) and an UNO (slave) microcontrollers to send a very simple character from the MEGA to the UNO that will trigger a shield on the UNO to carry out its programmed portion. For now I am simply using the example provided in the Wire.h library…

MEGA as MASTER:

//Master_writer using MEGA


#include <Wire.h>

void setup()
{
  Wire.begin(); // join i2c bus (address optional for master)
}

byte x = 0;

void loop()
{
  Wire.beginTransmission(4); // transmit to device #4
  Wire.write("x is ");        // sends five bytes
  Wire.write(x);              // sends one byte  
  Wire.endTransmission();    // stop transmitting

  x++;
  delay(500);
}

UNO as the SLAVE

//Slave_receiver using UNO


#include <Wire.h>

void setup()
{
  Wire.begin(4);                // join i2c bus with address #4
  Wire.onReceive(receiveEvent); // register event
  Serial.begin(9600);           // start serial for output
}

void loop()
{
  delay(100);
}

// function that executes whenever data is received from master
// this function is registered as an event, see setup()
void receiveEvent(int howMany)
{
  while(1 < Wire.available()) // loop through all but the last
  {
    char c = Wire.read(); // receive byte as a character
    Serial.print(c);         // print the character
  }
  int x = Wire.read();    // receive byte as an integer
  Serial.println(x);         // print the integer
}

I have a very silly question when it comes to the serial windows of each device… Do I hit send on the MEGA then plug the UNO to the computer USB port then open its serial window to monitor the result?

I know, I know its silly but I really need to know.

Thanks.
Yesenia

Try it both ways, see what works. Is probably not that critical. If one way doesn't work, use the other.

You never know for sure that master and slave are in sync unless you synchronize on some token.
This token can be a \n (newline) or < or …