how to send and receive a character between 2 arduinos

Hi im trying to send a character and receive it on another Arduino and check if that character came. How would you go about doing this? Ill post my code. Here is the code, I wanted to send something over like an 'a' or 'b' but whenever i try i get these weird serial readings , any help would be greatly appreciated.

//sender

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

void loop(){
   if(Serial.available() > 0){
      char character = 'a';
      Serial.print(character);
   }
}
//receiver

char msg;

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

void loop(){
  if(Serial.available()){
    msg = Serial.read();
    switch(msg){
      case 'a':
        Serial.println("An A was received");
        break;
        default:
        Serial.println("Nothing was received");
    }
  }
}

How you are taking the information in the other arduino¿?

are you use module RF?

Well, Ill explain to you exactly what i am doing. I have 2 arduinos hooked up via Rx, Tx and 2 separate computers to see what kind of output im getting. Im pretty much just telling a arduino to send some information to the other arduino and then the arduino that receives the information checks to see whether the character equals what was sent.

You have to connect GND also.

void loop(){
   if(Serial.available() > 0){
      char character = 'a';
      Serial.print(character);

This will only send something if it's recieved something first.
It would be better to use a one serial port for communicating between the devices, and one for the pc's

Read more about softwareserial here https://www.arduino.cc/en/Reference.SoftwareSerial

Also, more details about serial communication here Serial - Arduino Reference

yeah but how would power be distributed to the arduino not connected to the pc? Wouldnt the power limit the character being sent if it isnt on? Sorry im pretty new to this thing, i want to try new things.

You power each arduino independently, and just connect GND together the same way you did with TX and RX.

Alright makes sense. How about actually sending a character though? Which part of my code is incorrect and how can that be fixed ??

Have a look at Serial Input Basics

...R

Alright that makes sense, but what about actually sending a character ? Would my code satisfy that without the if serial available ? Or no ?

connect a LED to the recievers pin 13, (or use the built in if you're using an uno)
Then upload these 2 sketches (example taken from here)

//sender

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

void loop() {
  Serial.print('H');
  delay(1000);
  Serial.print('L');
  delay(1000);
}
//reciever

 int led = 13; 
int incomingByte;     

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

void loop() {
  
  if (Serial.available() > 0) {  
    incomingByte = Serial.read(); 
    if (incomingByte == 'H') {
      digitalWrite(led, HIGH);
    }
    if (incomingByte == 'L') {
      digitalWrite(led, LOW);
    }
  }
}

The LEd should turn on and off every second. It it does, you know that you're connection is working and you can start with more complex communication.

Thanks a lot, I test it out and see if its working

hell0world:
Alright that makes sense, but what about actually sending a character ? Would my code satisfy that without the if serial available ? Or no ?

Without seeing the code you are wondering about I can't answer that.

Normally you only need to check Serial.available() when receiving data, it has no relevance for sending data.

However if you only want to send data when the other Arduino requests it (a common requirement) you will need two-way communication. For example ArduinoA won't send data to ArduinoB until it receives an 'S' from ArduinoB.

...R

now im getting these weird values in the serial monitor. Im not getting any letters, What could be the problem? I hooked up Rx to Tx and Tx to Rx and connected the grounds together, One of the arduinos also is powered by battery. So what could be my error, please help. Thank you all.

@hell0world, poat your code if you want help. Without it we are blind.
And please use the code button </> so your code looks like this and is easy to copy to a text editor

...R

Did you test with the code i provided?