how to communicate between 2 arduinos using serial

I have to arduinos, a mega and uno. What I am trying to do is take the input from a computer sent to the mega through serial and send the same message out through one of the mega's other serial ports to the uno. For some reason I am having trouble with this, I have the wiring done I just can't get the code to work

can't get the code to work

Check line 42, it's always line 42.

If line 42 is OK post your code, at present we have nothing to comment on.


Rob

there isn't much code.

on the mega it is just reading the value of the serial and writing it to another one of the serial port and on the uno if there is any serial value above 1 it turn an led on

Look you are getting something wrong. It is either the code or the wiring. You have shown us neither. So how can anyone help you find out what you are doing wrong?

something as easy as this won't even work

mega

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

void loop(){
 Serial.print("a");
delay(2); 
  
}

uno

int val = 0;

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

void loop(){
  while(true){
  val = Serial.read()a;
  
  if (val > 0){
   digitalWrite(13, HIGH);
   delay(2000);
   digitalWrite(13, LOW); 
     }
  delay(2);
  val = 0;
  
}
}
Serial.read()a;

Eh?


Rob

still nothing

I don't even get a TX light on the mega

pitmanst:
I don’t even get a TX light on the mega

You won’t see any TX or RX lights doing this because these lights are controlled by the host PC not by the serial data.

You are using the default serial port on both Arduinos, these are connected to the USB / serial chip so you can’t use them to communicated between two arduinos.
Use serial port 1 on the Mega, and a software serial on the Uno. And learn how to use the Serial.read function.

Don’t forget to connect the RX of one to the TX of the other and connect the grounds together.

What I am trying to do is take the input from a computer sent to the mega through serial and send the same message out through one of the mega's other serial ports to the uno. For some reason I am having trouble with this, I have the wiring done I just can't get the code to work

void loop(){
  while(true){
    val = Serial.read();

    if (val > 0){
      digitalWrite(13, HIGH);
      delay(2000);
      digitalWrite(13, LOW); 
    }
    delay(2);
    val = 0;

  }
}

In this code, what other port?

 if (val > 0){
      digitalWrite(13, HIGH);
      delay(2000);
      digitalWrite(13, LOW); 
    }
    delay(2);

Even assuming everything else was correct, you are turning pin 13 on for 2 seconds and off for 2 milliseconds. You won't be able to spot that. It will look on all the time.

pitmanst: I have to arduinos, a mega and uno. What I am trying to do is take the input from a computer sent to the mega through serial and send the same message out through one of the mega's other serial ports to the uno. For some reason I am having trouble with this, I have the wiring done I just can't get the code to work

To communicate from the mega to the uno, connect the mega default tx pin to the uno rx pin, and connect the grounds between the mega and the uno. Then use simple serial code on both that will echo what is sent from the serial monitor back to the serial monitor. what is sent to the mega should appear on the uno's serial monitor.