Pages: [1]   Go Down
Author Topic: Arduino 2 Arduino serial communication problem  (Read 426 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 5
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello,

I have a Arduino UNO that I am trying to have communicate with an Arduino Diecimila over serial, and I can't figure out what I'm doing wrong.  The Uno is the sending unit, and this is it's sketch:

int ledPin = 13;
int sendingByte = 0;

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

void loop() {
  sendingByte = 1;

  Serial.write(sendingByte);
  //Serial.println(sendingByte);
  digitalWrite(ledPin, HIGH);
  delay(2000);

  sendingByte = 2;
  Serial.write(sendingByte);
 // Serial.println(sendingByte);
  digitalWrite(ledPin, LOW);
  delay(2000);

  sendingByte = 0;
}

The receiving unit is the Diecimila, and it's sketch is this:

int incomingByte = 0;
int ledPin = 13;

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

void loop() {
  if (Serial.available() > 0) {

    incomingByte = Serial.read();

    if(incomingByte == 1){
      digitalWrite(ledPin, HIGH);
    }
    if (incomingByte == 2){
      digitalWrite(ledPin, LOW);
    }  
  }
}

The Uno seems to be transmitting just fine, with the LED on pin 13 lighting up accordingly. I also ran the serial monitor on the Uno and it is sending 1 and 2 alternately.  I see the TX and RX lighting up on the Diecimila when the UNO transmits, but the LED connected with pin 13 just flickers momentarily.  It should be going HIGH or LOW accordingly.  The UNO digital pin 1 is connected to the Diecimila digital pin 0, and vise versa.  There is one line connecting their common GND.

What am I not understanding?   Thanks
Logged

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

Try changing the type of variable being sent. The name of the variable should give you a clue as to the correct type.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 5
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks PaulS.  Here is my rewritten code for those interested:

Sending unit -

int ledPin = 13;

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

void loop() {
  Serial.print(1);
  digitalWrite(ledPin, HIGH);
  delay(2000);
 
  Serial.print(2);
  digitalWrite(ledPin, LOW);
  delay(2000);
}

Receiving unit -

int incomingByte = 0;
int ledPin = 13;

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

void loop() {
  if (Serial.available() > 0) {
    //read incoming byte - subtract incoming ASCII value from 0 ASCII value to get char sent
    incomingByte = Serial.read() - '0';
   
    if(incomingByte == 1){
      digitalWrite(ledPin, HIGH);
    }
    if (incomingByte == 2){
      digitalWrite(ledPin, LOW);
    }   
  }
}
Logged

Pages: [1]   Go Up
Jump to: