I'm trying to get two Arduino Uno's to talk to each other using the TX/RX pins. Eventually I'm going to be using RF modules but to test things out I've run a wire from the TX pin on one board over to the RX pin on the other. When both boards are hooked up to the pc everything works fine. I press a button on the transmit board an LED lights up on the receiver. If I unplug the wire between the ports it stops working as it should.
The problem I'm having is when I run them both on battery power, the TX light does not blink on the transmitting board and the LED on the receiver board isn't lighting. Is there something special I need to do to enable the ports when running on battery power?
This is the code I'm using:
// This says whether you are building the transmitter or reciever.
// Only one of these should be defined.
#define TRANSMITTER
//#define RECEIVER
#define NETWORK_SIG_SIZE 3
#define VAL_SIZE 2
#define CHECKSUM_SIZE 1
#define PACKET_SIZE (NETWORK_SIG_SIZE + VAL_SIZE + CHECKSUM_SIZE)
#define NET_ADDR 5
const byte g_network_sig[NETWORK_SIG_SIZE] = {0x8F, 0xAA, NET_ADDR}; // Few bytes used to initiate a transfer
#define BUTTON_PIN 8
#define LED_PIN 13
// Button hardware is setup so the button goes LOW when pressed
#define BUTTON_PRESSED LOW
#define BUTTON_NOT_PRESSED HIGH
void setup()
{
pinMode(BUTTON_PIN, INPUT);
pinMode(LED_PIN, OUTPUT);
digitalWrite(LED_PIN, LOW);
Serial.begin(1200);
}
#ifdef TRANSMITTER
void loop()
{
static int prev_button = BUTTON_NOT_PRESSED; // Previous button press value
int cur_button; // Current button press value
cur_button = digitalRead(BUTTON_PIN);
if ((prev_button == BUTTON_NOT_PRESSED) && (cur_button == BUTTON_PRESSED))
{
digitalWrite(LED_PIN, HIGH);
writeUInt(271); // Put any number you want to send here (71 is just a test)
delay(1000);
digitalWrite(LED_PIN, LOW);
}
delay(50); // Debounce button
prev_button = cur_button;
}
#endif //TRANSMITTER
#ifdef RECEIVER
void loop()
{
boolean light_led = false;
if (readUInt(true) == 271) // Check to see if we got the 71 test number
{
light_led = true;
}
if (light_led)
{
digitalWrite(LED_PIN, HIGH);
delay(1000);
digitalWrite(LED_PIN, LOW);
}
}
#endif //RECEIVER
// Sends an unsigned int over the RF network
void writeUInt(unsigned int val)
{
byte checksum = (val/256) ^ (val&0xFF);
Serial.write(0xF0); // This gets reciever in sync with transmitter
Serial.write(g_network_sig, NETWORK_SIG_SIZE);
Serial.write((byte*)&val, VAL_SIZE);
Serial.write(checksum); //CHECKSUM_SIZE
}
// Receives an unsigned int over the RF network
unsigned int readUInt(bool wait)
{
int pos = 0; // Position in the network signature
unsigned int val; // Value of the unsigned int
byte c = 0; // Current byte
if((Serial.available() < PACKET_SIZE) && (wait == false))
{
return 0xFFFF;
}
while(pos < NETWORK_SIG_SIZE)
{
while(Serial.available() == 0); // Wait until something is avalible
c = Serial.read();
if (c == g_network_sig[pos])
{
if (pos == NETWORK_SIG_SIZE-1)
{
byte checksum;
while(Serial.available() < VAL_SIZE + CHECKSUM_SIZE); // Wait until something is avalible
val = Serial.read();
val += ((unsigned int)Serial.read())*256;
checksum = Serial.read();
if (checksum != ((val/256) ^ (val&0xFF)))
{
// Checksum failed
pos = -1;
}
}
++pos;
}
else if (c == g_network_sig[0])
{
pos = 1;
}
else
{
pos = 0;
if (!wait)
{
return 0xFFFF;
}
}
}
return val;
}