Testing code using wire between TX and RX of 2 arduinos, causing issues maybe?

Hi, Im currently testing some code that will be used via Xbee once its working. Currently just sending data from an arduino mega to an arduino uno by connecting the TX of the mega with the RX of the UNO. Basically when a button connected to the mega is pressed it increments a variable, and sends it to the UNO, where it is displayed on a 4 digit 7 segment display. When both units are connected to each other, and the computer via USB, everything works fine. When they are both connected together, but only one of them is connected to the computer via USB, and the other is powered from a battery everything stops working. I will post the code for both units below, and hopefully someone can help me out. Also I can't disconnect both from the computer at the moment as I only have one battery connector, until I buy another one.

Code for sending unit the Mega

//sender
const int buttonPin = 2;     // the number of the pushbutton pin
const int ledPin =  13;      // the number of the LED pin
int buttonState = 0;         // variable for reading the pushbutton status
int count = 0;
char istr[4];

void setup() {
  Serial.begin(19200);
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);      
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT); 
}

void loop(){
  buttonState = digitalRead(buttonPin);
  if (buttonState == HIGH) { 
    digitalWrite(ledPin, HIGH);  
    Serial.print((itoa(++count, istr, 10)));
    delay(250);
  } 
  else {
    digitalWrite(ledPin, LOW); 
  }
}

Code for the receiving unit, the UNO

//reciever
#include <Wire.h>
#include "Adafruit_LEDBackpack.h"
#include "Adafruit_GFX.h"

Adafruit_7segment matrix = Adafruit_7segment();

int count = 0;
char X_buffer[5];

void setup() {
  Serial.begin(19200);    
  matrix.begin(0x70);
}

void loop(){

if (Serial.available() >0){
      delay(5);    
      int i=0;  
    
      while(i<4){
        X_buffer[i] = Serial.read();
        i++;
      }
      
    count = atoi(X_buffer);
    matrix.print(count, DEC);
    matrix.writeDisplay();
  } 
}

but only one of them is connected to the computer via USB, and the other is powered from a battery everything stops working.

Most likely you failed to know that you would need to run a common ground wire connecting the two arduinos if they are not pugged into the same PC (which would supply a common ground without a need for a seperate ground wire).

Lefty

Yep that was the issue, was just coming back to delete the post once I had figured it out, but I guess you were faster at pointing it out than I was at spotting it! Thanks!

Gloompirate:
Yep that was the issue, was just coming back to delete the post once I had figured it out, but I guess you were faster at pointing it out than I was at spotting it! Thanks!

That's what they all say. :wink:

By the way it's never a good idea to delete postings, as many beginners search through many many posting looking for trouble symptoms they are having and looking for possible causes and solutions. Your problem even solved could be helpful for someone else down the road. Besides one of our roles here is to make sure we fill up as many site hard drives as we can and make arduino spend some of their profits in a way that benefits us directly. :wink:

Lefty