Go Down

Topic: Arduino Duemilanove trying to talk to an Arduino Mega.... (Read 461 times) previous topic - next topic

MrKovacic

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: [Select]



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


void loop(){

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


Arduino Mega code:
Code: [Select]



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?

pylon

In your if statements

Code: [Select]
  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: [Select]

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

MrKovacic

That did it! Thank you very much!!

Also I learned something new!

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy