Serial Monitor Help

Hello All! Stumped enough that I created an account to see if any gurus out there can help me out.
The breakdown of what I am trying to do;

  1. Using two arduino 2560 Megas communication over I2C protocol.
  2. Using I2c scanner, I am trying to get the address of a sensor connected to the slave device
  3. Send this address over I2C to my master arduino to then display on the serial monitor
    Eventually I will want to send the sensor data over I2C as well, but one step at a time.

I will link both master and slave code.

Master;

#include <Wire.h>
int x = 0;
void setup()
{
  // Start the I2C Bus as Master
  Serial.begin(9600);
  Wire.begin(9600);

  Wire.requestFrom(9, 100);

  while (Wire.available())
  {
    char c = Wire.read();
    Serial.print(c);
  }
}
void loop()
{
  while (Serial.available())
  {
    char c = Serial.read();

    if (c == 'H')
    {
      Wire.beginTransmission(9);
      Wire.write('H');
      Wire.endTransmission();
    }
    else if (c == 'L')
    {
      Wire.beginTransmission(9);
      Wire.write('L');
      Wire.endTransmission();
    }
  }
}

And Next is the slave code;

#include <Wire.h>

void setup() {
  Wire.begin(9);
  Wire.onReceive(receiveEvent);
  Wire.onRequest(i2cscan);

  pinMode(13, OUTPUT);
  digitalWrite(13, LOW);
}

void loop()
{
  delay(100);
}

void i2cscan()
{


  while (Wire.available())
  {
    byte error, address;
    int nDevices;
    Wire.begin(9600);
    Serial.begin(9600);
    Serial.println("Scanning...");

    nDevices = 0;
    for (address = 1; address < 127; address++ )
    {
      // The i2c_scanner uses the return value of
      // the Write.endTransmisstion to see if
      // a device did acknowledge to the address.
      Wire.beginTransmission(address);
      error = Wire.endTransmission();

      if (error == 0)
      {
        Serial.print("I2C device found at address 0x");
        if (address < 16)
          Serial.print("0");
        Serial.print(address, HEX);
        Serial.println("  !");

        nDevices++;
      }
      else if (error == 4)
      {
        Serial.print("Unknown error at address 0x");
        if (address < 16)
          Serial.print("0");
        Serial.println(address, HEX);
      }
    }
    if (nDevices == 0)
      Serial.println("No I2C devices found\n");
    else
      Serial.println("done\n");

    delay(5000);           // wait 5 seconds for next scan
  }
}
void requestEvent()
{
  Wire.write("123");
}

void receiveEvent(int howMany)
{
  while (Wire.available())
  {

    char c = Wire.read();

    if (c == 'H')
    {
      digitalWrite(13, HIGH);
    }
    else if (c == 'L')
    {
      digitalWrite(13, LOW);
    }
  }
}

I am receiving question marks in my serial terminal, this is a sign I assume I am sending the data over, its just in the wrong format or something such. Attached is a picture for reference.

Thanks in advance,
Russell

Baud rate?

SteveMann:
Baud rate?

This is why I have my serial.begin and Wire.begin at 9600. Is there another place I might need to check?

Thanks

Yes, the terminal. Do you see text on the terminal screen from your Serial.println statements?

SteveMann: Yes, the terminal. Do you see text on the terminal screen from your Serial.println statements?

The only thing I see is the backwards questions marks ass shown above in my attached photo. IF i run the scanner on the master board, I can see both the address of the slave and the sensor. Seems I may not be implementing the correct use of it when creating my own function.

Wire.begin() does not take a baudrate as a parameter.

You will need to be an I2C master to be able to start communication with slaves.

Check Wire.begin() reference.

Okay thanks, I will remove the baud rate from Wire.begin. But what else do I need to change? I did a simple experiment using the char c to call to the slave, and turn on the onboard LED. (As with pinMode 13 = High)

Also I went through an example of the Wire.requestFrom to send a simple string of "123". What I believe is going on is that I am trying to send a data value that my master cant interpret?

You have an I2C slave Arduino and an I2C slave sensor connected to an Arduino that acts as an I2C master. Why do you want the slave Arduino to communicate with the slave sensor? It would be far easier if the master Arduino would run the scanner.

As I said, only I2C masters can initiate communication. As it stands now, your slave Arduino has to become an I2C master first to run the scan, next your original master Arduino has to become an I2C slave so the ‘new’ master can send the data to it.