Pages: [1]   Go Down
Author Topic: Arduino Duemilanove trying to talk to an Arduino Mega....  (Read 404 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 46
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

This should be a simple one for anyone but me...

What I'm trying to do:
Send data from the Due to the Mega via Serial, and show that the data is moving by having the Due send a $ chr to turn an LED on the Mega on, then 2 seconds later, send a # chr to turn it off.  LED is on Pin 8 of the Mega.

The problem:
This setup works, for the most part. It seems it is missing communication because the LED does not always toggle on, or off after 2 seconds. Sometimes it does, sometimes it doesn't.

Hardware:
Arduino Due Pin 0 (RX) TO Mega pin 1 (TX)
Arduino Due Pin 1 (TX) TO Mega pin 0 (RX)
Arduino Due GND to Mega GND


Arduino Due code:
Code:


void setup(){
Serial.begin(115200);
}


void loop(){

Serial.print("$");
delay(2000);
Serial.print('#');
delay(2000);
}

Arduino Mega code:
Code:


void setup(){

 Serial.begin(115200);

  pinMode(8, OUTPUT);
}


void loop(){
  delay(1);
  if(Serial.read() == '$'){
    delay(1);
digitalWrite(8, HIGH);

    }
    delay(1);
  if(Serial.read() == '#'){
    delay(1);
digitalWrite(8, LOW);
delay(1);
    }
    delay(1);
}

Any thoughts?
Logged

Switzerland
Offline Offline
Faraday Member
**
Karma: 112
Posts: 5286
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

In your if statements

Code:
  if(Serial.read() == '$'){

you read a character from the serial buffer (without checking if there is one available BTW) and consume it. If the sent '#' is read in the first if, the second if will not see it. Same the other way around. So read the character and then check it:

Code:
if (Serial.available()) {
  byte c = Serial.read();
  if (c == '$') {
    // do your stuff
  } else if (c == '#') {
    // do other stuff
  }
}
Logged

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

That did it! Thank you very much!!

Also I learned something new!
Logged

Pages: [1]   Go Up
Jump to: