Pages: [1]   Go Down
Author Topic: Testing code using wire between TX and RX of 2 arduinos, causing issues maybe?  (Read 387 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 1
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Code:
//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
Code:
//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();
  }
}
Logged

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 361
Posts: 17294
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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
Logged

Offline Offline
Newbie
*
Karma: 1
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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!
Logged

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 361
Posts: 17294
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.  smiley-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.  smiley-wink

Lefty
« Last Edit: December 04, 2012, 07:41:42 pm by retrolefty » Logged

Pages: [1]   Go Up
Jump to: