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();
}
}