Pages: [1] 2   Go Down
Author Topic: Serial Communication Between 2 Arudino Unos  (Read 480 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I am an extreme newbie and need some help. I am trying to have two Uno boards speak to each other via serial communication. I think I have the first board programmed correctly, but having trouble with the 2nd one. Basically I have a proximity sensor on Uno #1 that controls a LED on/off. I would also like to transmit data via serial to a 2nd Uno board that has a LED as well and turn that on/off with the proximity sensor connected to Uno #1. Below is my code, any ideas what I am doing wrong?

Uno #1:

const int SENSOR = 0;
const int LED = 13; // LED connected to digital pin 13

int val = 0;

void setup() {

pinMode(LED, OUTPUT);
pinMode(SENSOR, INPUT);

Serial.begin(9600);
}

void loop() {
Serial.println(val);
delay(100);

val = analogRead(0);

if (val > 400) {
  Serial.write(0x01);
  digitalWrite(LED, HIGH);
} else {
  Serial.write(0x00);
  digitalWrite(LED, LOW);
}
}

Uno #2:

const int SENSOR = 0;
const int LED = 13; // LED is connected to digital pin 13

int serialdata = 0;

void setup() {
  pinMode(LED, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  serialdata = Serial.read();
  Serial.println(serialdata);
  delay(100);
 
  if (serialdata = 0x01) {
    digitalWrite(LED, HIGH);
} else {
    digitalWrite(LED, LOW);
  }
}
Logged

California
Offline Offline
Faraday Member
**
Karma: 82
Posts: 3123
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

= is assignment, == is used for testing equality.
Logged

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

That makes sense, but I still can't seem to get these 2 working. I have the Rx connected to Tx from each hardware serial, and a wire connected between a ground on each Arduino as well. Is my code too simple? What am I missing here?

Ardutino 1 (Send)
Code:
const int SENSOR = 0;
const int LED = 13; // LED connected to digital pin 13

int val = 0;

void setup() {

pinMode(LED, OUTPUT);
pinMode(SENSOR, INPUT);

Serial.begin(9600);
}

void loop() {
Serial.println(val);
delay(100);

val = analogRead(0);

if (val > 400) {
  Serial.write(0x01);
  digitalWrite(LED, HIGH);
} else {
  Serial.write(0x00);
  digitalWrite(LED, LOW);
}
}

Arduino 2 (Receive)
Code:
const int LED = 13; // LED is connected to digital pin 13

int serialdata = 0;

void setup() 4{
  pinMode(LED, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  serialdata = Serial.read();
  Serial.println(serialdata);
  delay(100);
 
  if (serialdata == 0x01) {
    digitalWrite(LED, HIGH);
} else {
    digitalWrite(LED, LOW);
  }
}
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 549
Posts: 46107
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
void setup() 4{
  pinMode(LED, OUTPUT);
  Serial.begin(9600);
}
Does this even compile?
Logged

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

No it doesn't, sorry I was taking a screenshot on my mac and press the wrong key which put a 4 into the code by mistake. Here is the corrected code.

Arduino 1 (Send)
Code:
const int SENSOR = 0;
const int LED = 13; // LED connected to digital pin 13

int val = 0;

void setup() {

pinMode(LED, OUTPUT);
pinMode(SENSOR, INPUT);

Serial.begin(9600);
}

void loop() {
Serial.println(val);
delay(100);

val = analogRead(0);

if (val > 400) {
  Serial.write(0x01);
  digitalWrite(LED, HIGH);
} else {
  Serial.write(0x00);
  digitalWrite(LED, LOW);
}
}

Arduino 2 (Receive)
Code:
const int LED = 13; // LED is connected to digital pin 13

int serialdata = 0;

void setup() {
  pinMode(LED, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  serialdata = Serial.read();
  Serial.println(serialdata);
  delay(100);
 
  if (serialdata == 0x01) {
    digitalWrite(LED, HIGH);
} else {
    digitalWrite(LED, LOW);
  }
}
Logged

California
Offline Offline
Faraday Member
**
Karma: 82
Posts: 3123
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
void loop() {
  serialdata = Serial.read();
  Serial.println(serialdata);
  delay(100);
  
  if (serialdata == 0x01) {
    digitalWrite(LED, HIGH);
} else {
    digitalWrite(LED, LOW);
  }
}

Serial.read() isn't blocking, so it will return -1 when there is not data. As you know -1 is not equal to 1, so I wouldn't expect the LED to be lit for more than 100 ms. You should be using Serial.available() to make sure there is data to read before taking action on it.
« Last Edit: January 23, 2013, 11:29:26 am by Arrch » Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 549
Posts: 46107
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

This might seem like a silly question, but, why are you tying up the hardware serial port exchanging data? Use two other pins, and SoftwareSerial. Then, you can use the hardware serial port for debugging.

Also, describing exactly what the problem is would be helpful. As it is, say val is 430. You are sending '4', '3', '0', <CR>, and <LF>, and then, after a short pause, 0x01 from one Arduino to another.

The '4', '3', '0', <CR>, and <LF> will turn the LED off on the receiver. The 0x01 will turn the LED on. But, the next set of data will immediately follow that, and turn the LED off again.
Logged

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

I'll try the Serial.available() and see what happens. But to explain further the project...

I have a proximity sensor on Arduino 1, if the reading from the sensor (val) is greater than 400, it turns on the LED on Arduino 1. I want to hook up Arduino 2 so that it's LED will turn on at the same time as the LED on Arduino 1. Here is a picture of my setup (with power unplugged).

Logged

New Jersey
Online Online
Faraday Member
**
Karma: 49
Posts: 3420
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

A little judicious use of serial.available should sort matters out. I'd be inclined to send something more readable than 0 and 1 though, perhaps 'H' and 'L'. That way, you can test each module independently using the IDE's serial terminal.
Logged

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

I'll try that, good idea. I finally got it to work using the hardware ports, however there is about a 6 second lag between the two Arduinos. What is that about? Is there a fix?
Logged

UK
Offline Offline
Shannon Member
****
Karma: 184
Posts: 11173
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

What is that about?

I suppose it's something your code is doing.
Logged

I only provide help via the forum - please do not contact me for private consultancy.

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

I think it may be something my code is not doing, like setting a faster send/receive rate. I tried messing with baud, but didn't see any improvement. Any suggestions or is it just the limitation of the hardware?
Logged

California
Offline Offline
Faraday Member
**
Karma: 82
Posts: 3123
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

A 6 second delay is an eternity for the Arduino, so there is likely something wrong with the code. You haven't posted updated code, so it's hard to help.
Logged

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

A 6 second delay is an eternity for the Arduino...

Thank you for confirming that, below is my updated code I am currently working with:

Arduino 1 (Send)
Code:
const int SENSOR = 0;
const int LED = 13; // LED connected to digital pin 13

int val = 0;

void setup() {

pinMode(LED, OUTPUT);
pinMode(SENSOR, INPUT);

Serial.begin(9600);
}

void loop() {

val = analogRead(0);

if (val > 400) {
  Serial.write('H');
  digitalWrite(LED, HIGH);
} else {
  Serial.write('L');
  digitalWrite(LED, LOW);
}
}

Arduino 2 (Receive)
Code:
const int LED = 13; // LED is connected to digital pin 13

int serialdata = 0;

void setup() {
  pinMode(LED, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  if (Serial.available() > 0) {
  serialdata = Serial.read();
  if (serialdata == 'H') {
    digitalWrite(LED, HIGH);
} else {
    digitalWrite(LED, LOW); 
  Serial.println(serialdata);
}
  }
}
Logged

California
Offline Offline
Faraday Member
**
Karma: 82
Posts: 3123
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Try this (else if):

Code:
void loop() {
  if (Serial.available() > 0) {
  serialdata = Serial.read();
  if (serialdata == 'H') {
    digitalWrite(LED, HIGH);
} else if (serialdata == 'L') {
    digitalWrite(LED, LOW);  
  Serial.println(serialdata);
}
  }
}

Also, proper indentation goes a long way in people helping you with your code. On a smaller program, it's not going to make a huge difference, but it's good practice for larger projects. To indent properly, place each curly brace on it's own line and use the Tools > Auto Format option.
« Last Edit: January 23, 2013, 03:29:28 pm by Arrch » Logged

Pages: [1] 2   Go Up
Jump to: