I could really use some help. mega 2560 I2c

So I have been trying to get my 2 mega 2560's to talk for days now and I'm ready to get the shot gun out and teach them what for......

I have tried using many sketches but to no avail. I have even tried the examples that came with sketch_sep10a and just running them as is one in each mega and no work. Then I tried the sketches below and found the mega's lock up just after the: Wire.beginTransmission(5); line. I tried the same code in both my mega 2560 and same poo. Tried it in my Arduino uno and the LEDs turn on and off on the master, but the mega dose not seem to receive anything. I only have one Uno. I'm a a loss here.

wiring diagram:

Master:
#include <Wire.h>

void setup()
{
pinMode(13, OUTPUT);
Serial.begin(9600);

Wire.begin();
}

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

if(c == 'H')
{
digitalWrite(13, HIGH);
Wire.beginTransmission(5);
Wire.write('H');
Wire.endTransmission();
}
else if(c == 'L')
{
digitalWrite(13, LOW);
Wire.beginTransmission(5);
Wire.write('L');
Wire.endTransmission();
}
}
}

/////////////////////////////////////////////////////////////////////////////////
Slave:

#include <Wire.h>

void setup()
{
Wire.begin(5);
Wire.onReceive(receiveEvent);

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

void loop()
{
}

void receiveEvent(int howMany)
{
while(Wire.available())
{
char c = Wire.read();

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

Code tags, please.

Read this before posting a programming question

Have you connected the grounds together?

... and found the mega's lock up just after the: Wire.beginTransmission(5); line.

How do you know it locks up there and not somewhere else?

1). Sorry for lack of tags.

2). Might be something here I missed. Grounds? What grounds?

3). Everything before that line works, nothing happens after that line. This I tested by moving the led on line around in the sketch.

2). Might be something here I missed. Grounds? What grounds?

How many wires are there connecting the two Megas? What wires are they?

There are 3 wires from each mega. Pin # 21 runs to the SCL bus, and 20 runs to the SDA bus. Then a 5 volt feed runs into 2 resistors. 1 resistor connected to each bus. Then after you asked a bought connecting the grounds I added a ground from one of the ground pins on each mega to the other mega. Did not seem to help.

I was playing with the sketch and I got the first one to appear to work. I commented out the lines that said end transmition, and now I can turn on or off the LED'S on the master board. But not on the slave. So there is no way to tell if the signal is even leaving the master board.

But not on the slave. So there is no way to tell if the signal is even leaving the master board.

Can you post a photo? It should work.

O dear a photo, ok sure, first let me explain that this is a vary advanced mess. I'm trying to upgrade my life size R2D2. The mega in the bottom has only 4 empty pins left. The slip wring that takes wires to the head has only 4 wires un used. The bottom mega is well buried sort of and the top one in the head is out in plain view but the buss with the resistors where the buss is feed or starts is on the other side of the head. Ill post some pictures. Sorry I know these R not what you where looking for, But I did not what to be rude and not post anything.

Ok This is the droid before I started with the new projects.

Here is the inside of the body, main panel. The mega is circled in red.

Here is the new mega in the front of the head.

on the backside next to an Arduino Uno is the begging of the I2C buss.

This looks a mite complex to me! More involved than just two mega2560s.

boggle

OK, so you don't have two Megas on the bench connected by 3 wires and are having trouble with that sketch, exactly. You have much, much more.

First, it looks really cool. :slight_smile:

Second this is what Wire.beginTransmission does:

void TwoWire::beginTransmission(uint8_t address)
{
// indicate that we are transmitting
transmitting = 1;
// set address of targeted slave
txAddress = address;
// reset tx buffer iterator vars
txBufferIndex = 0;
txBufferLength = 0;
}

It sets 4 variables.

Slee_The_Sloth:
Then I tried the sketches below and found the mega's lock up just after the: Wire.beginTransmission(5); line.

Now the code above is hardly going to fail. So your deduction that it locks up on the Wire.beginTransmission(5) just can't be right. On the endTransmission, that I could believe.

To say that there might conceivably be a bit of noise in your system there would be an understatement, I think.

You could try dropping the I2C clock speed. My page gives example settings for that:

Is there any good reason you can't use one of the Mega's serial ports to talk from one to the other, since I2C seems to be causing problems?

Thanks. :slight_smile:

The way I determined where the mega was locking up is I moved the digital write statement around. It worked up to one point and then one line passed that the led quite coming on.

I will look into your page. That's worth a try. As for the serial, I think that would work just as well for what I need. I just don't know much about it..YET.... Any good places to point me to?

Thanks for the help :slight_smile:

Thank you for your help. I got the Serial to work just fine. Thanks:) :grin: