Loading...
Pages: [1]   Go Down
Author Topic: Software Serial Library  (Read 212 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi all,

    I'm new to Arduino. Recently, I was playing around with serial communication between 2 Arduino Uno. Basically, I have 2 arduino Unos, each rigged with a multicolor LED and initially setup to flash a different color. The idea is that if the 2 arduino boards are connected through the pins designated by the software serial port, their colors would sync. The strange thing is that the code seems to work prefectly fine when I attached (powered) the 2 boards via USB cable, but when I switch both (or either) over to v9 batteries, they do not seem to communicate any more (Each arduino just flashes their own color). Anybody got an idea why this is so? Thanks!

Here's the code snippet:
Code:
...
SoftwareSerial neighbor(2,3);
int my_color = 0;

void setup() {
  my_color = 0;    // uploaded to arduino board 1
  // my_color = 5; // uploaded to arduino board 2
  // Serial.begin(9600);
  neighbor.begin(4800);
}

void loop() {
  if (neighbor.available()) {
      int received_color = neighbor.read();
      int new_color = (my_color + received_color) / 2;
      // Serial.print("Received color suggestion: ");
      // Serial.print(received_color);
      // Serial.print("\n");
      set_color(new_color);
      neighbor.write(my_color);
  } else {
      // Serial.print("No suggest from neighbor, using my own color: ");
      // Serial.print(my_color);
      // Serial.print("\n");
      set_color(my_color);
  }
  delay(500);
}

void set_color(int color_value) {
   ...
   // Uninteresting bits... just tell LED to change color
}
Logged

Australia
Offline Offline
Sr. Member
****
Karma: 7
Posts: 317
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Have you got the grounds of the two boards connected?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello Lemming,

    Thanks for your reply! Oh my... the answer is no. Could you elaborate how I can do that and why it might be the cause of the problem? Sorry if those sound like lame questions, but I'm really new at this.. =P
Logged

nr Bundaberg, Australia
Offline Offline
Tesla Member
***
Karma: 70
Posts: 6803
Scattered showers my arse -- Noah, 2348BC.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Just connect the GND of one to the GND of the other.

Without a reference point (ie the GND) the voltage levels seen by each Arduino can be any old crap, probably similar to the pins just floating.

When you were using USB the GNDs where connected via the USB cables. With the batteries there is no connection.
______
Rob
« Last Edit: March 08, 2012, 04:24:28 am by Graynomad » Logged

Rob Gray aka the GRAYnomad http://www.robgray.com

Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks Graynomad and Lemming!

I realise that this problem I face is not really a programming question after all. My apologies to all, for misplacing the question in this topic.. =P
Logged

nr Bundaberg, Australia
Offline Offline
Tesla Member
***
Karma: 70
Posts: 6803
Scattered showers my arse -- Noah, 2348BC.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

That' s OK, often a problem manifest itself in a different area, or at least seems to.

______
Rob
Logged

Rob Gray aka the GRAYnomad http://www.robgray.com

Pages: [1]   Go Up
Print
 
Jump to: