Serial communication used to work fine but now just send corrupt data

Hello this is my first post. I´m new with arduino but i was doing some tests connecting 2 arduino mega2560 with the master.writer example sketch and with wires connected at TX0 and RX0. It was working fine, both arduinos were able to send and receive text en the serial monitor. But today doing the same, in the serial monitor start doing wierd stuff, first corrupt data (text), and then stop sending data (text).
I didn´t change anything i don´t know what happen here.

Here is the code as you can see is the example sketch.

#include <Wire.h>

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

byte x = 0;

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

 x++;
 delay(500);
}

I hope someone give me a hint.
Thanks a lot.

Moderator edit:
</mark> <mark>[code]</mark> <mark>

</mark> <mark>[/code]</mark> <mark>
tags added.

Update 21/08/2015
I try a lot of things and now i make it work but with different wire configuration.
Originaly i connect TX0 and RX0 and work fine. But Now i have to connect TX0/RX0 and SDA/SCL
I don´t know why its now working with 4 wires if in first place it was working with just 2.
I don´t know if the arduinos are broken.

Have you confirmed that the serial monitor is set for the correct baud rate etc?

Weedpharma

If you are using breadboards, maybe a connection became loose. Did you move the unit before the problem started?

Are we to assume, that you are also sending text to the serial terminal on your computer (different wires of course)? Is that still working?

Oronte:
Hello this is my first post. I´m new with arduino but i was doing some tests connecting 2 arduino mega2560 with the master.writer example sketch and with wires connected at TX0 and RX0. It was working fine, both arduinos were able to send and receive text en the serial monitor. But today doing the same, in the serial monitor start doing wierd stuff, first corrupt data (text), and then stop sending data (text).
I didn´t change anything i don´t know what happen here.

Are you using TX0/RX0 to connect the arduino's together? The example needs them connecting by SDA/SCL.

If it use to work and now it doesn't using the same code, you probably have a wiring issue.

weedpharma:
Have you confirmed that the serial monitor is set for the correct baud rate etc?

Weedpharma

Thanks for your reply.
Yes i try at different baud rates, the curious thing is sometimes in 57600 baude rate send some corrupt data, but still not working.

Riva:
Are you using TX0/RX0 to connect the arduino's together? The example needs them connecting by SDA/SCL.

Thanks for your reply.
Yes i´m using TX0/RX0 to connect the arduinos and it works fin until yesterday.
I try with SDA/SCL with different wires following you suggestion and still not working

zoomkat:
If it use to work and now it doesn't using the same code, you probably have a wiring issue.

Thanks for yot reply.
Well i already try with 10 different wires and still not working.

Ok, then lets start differently.
Do you know that the arduino is running? Blinking led, or serial monitor posts to your computer?
Can you still load a sketch?
Aren't TX0 and RX0 the ones used to communicate to the computer? Are you disconnecting the computer?

Thanks for your reply.

The arduino is running, RX led is blinking, the serial monitor is not posting in my computer, i write on the serial monitor and press enter to send and the text dissapear and in the othen computer do not appear.
I use TX0 and RX0 to communicate each other and works fine, but now stop working and i have to add SCL/SDA. Is very strange that.

Oronte:
The arduino is running, RX led is blinking, the serial monitor is not posting in my computer, i write on the serial monitor and press enter to send and the text dissapear and in the othen computer do not appear.
I use TX0 and RX0 to communicate each other and works fine, but now stop working and i have to add SCL/SDA. Is very strange that.

With the code you posted in your initial post this can not happen. First the sketch does not read serial data and then send it. It just sends some text with an incrementing number on the end.
It is also using Wire library to send this (I2C) data so will be using the SCL/SDA pins and not the RX0/TX0 pins.

The corresponding receive sketch does use serial to print out ant data it receives over I2C.

"the serial monitor is not posting in my computer, i write on the serial monitor and press enter to send and the text dissapear"
Are you trying to use TX0 and RX0 to have communications between 3 computers? Isn't TX0 and RX0 the ones going to your laptop/computer?

I think that the RX led could blink even if the arduino is not running. Can you add code to "blink a led". That would tell us if the arduino is still running or not.

Riva:
With the code you posted in your initial post this can not happen. First the sketch does not read serial data and then send it. It just sends some text with an incrementing number on the end.
It is also using Wire library to send this (I2C) data so will be using the SCL/SDA pins and not the RX0/TX0 pins.

The corresponding receive sketch does use serial to print out ant data it receives over I2C.

Thanks for your reply

Well is strange that because i was running that sketch in those arduinos and worked.

I try already use the SCL/SDA with that sketch and it does not work, now is only working with SCL/SDA/TX0/RX0 connected i have no clue why.
What i need is to communicate my 2 arduino mega 2560 each one connected to a different computer and write text in the serial monitor and receive in the other just with 2 wires do you have any ideas?

Thanks for your relpy.
What i´m doing is this
Connecting an arduino megas 2560 to a computer via USB cable, and then with 2 wires connected to RX0/TX0 connect the second arduino:

Computer "A" Arduino "A" Arduino "B" Computer "B"
Hello TX0 ------------- RX0 Hello
RX0 ------------- TX0

But Now i have to connect 4 wires

Computer "A" Arduino "A" Arduino "B" Computer "B"
Hello TX0 ------------- RX0 Hello
RX0 ------------- TX0
SDA --------------- SDA
SCL ----------------- SCL
But i don´t know why suddenly just stop working with 2 wires and now just works with 4 wires

The arduino is running, RX led is blinking, the serial monitor is not posting in my computer, i write on the serial monitor and press enter to send and the text dissapear and in the othen computer do not appear.

Back to basics, the below simple serial code echos what is sent from the serial monitor back to the serial monitor.

//zoomkat 6-29-14 Simple serial echo test
//type or paste text in serial monitor and send

String readString;

void setup() {
  Serial.begin(9600);
  Serial.println("Simple serial echo test"); // so I can keep track of what is loaded
}

void loop() {

  while (Serial.available()) {
    char c = Serial.read();  //gets one byte from serial buffer
    readString += c; //makes the String readString
    delay(2);  //slow looping to allow buffer to fill with next character
  }

  if (readString.length() >0) {
    Serial.println(readString);  //so you can see the captured String 
    readString="";
  } 
}

Oronte:
...with 2 wires connected to RX0/TX0 connect the second arduino:

Computer "A" Arduino "A" Arduino "B" Computer "B"
Hello TX0 ------------- RX0 Hello
RX0 ------------- TX0

Note, serial communication is not done with 2 wires. It is done with 3.
TX ----- RX
RX ----- TX
GND ----- GND

Do you have the pullup resistors on both the I2C lines, about 4.7k ?

dmjlambert:
Note, serial communication is not done with 2 wires. It is done with 3.
TX ----- RX
RX ----- TX
GND ----- GND

Thanks for your reply
Well now i have just 3 wires connected!
Thanks
I really don´t know why before works fine with just 2 wires connected. I can´t explain that but i´m sure it was working. But as you say with GND connected it´s working fine.
Do you know why could that happen? Why before works with 2 wires?

Thanks for your reply
Ni´m not using any resistor, just 2 wires directly from one arduino to another

"Do you know why could that happen? Why before works with 2 wires? "
If you were using the same power supply, or two power supplies that used common grounds. that could happen.
I suspect you changed the power supply setup, and then it required the ground to be connected.