Using UART communication system, Easy chat program error.

Hello, I’m studying arduino from my school at South Korea, And I’d like make a chat program.
Simple and just distinguish other component.
The conditions are here.

  1. by using 2 ardunios, using uart communication.
  2. Using softwareserial.h
  3. when one arduino send message to other arduino, LED OFF. in other times, LED ON.
  4. Two arduino’s GND should be connected.
  5. Distinguish each component. ex) A: hi B: yeah
  6. There should be line change. when one component send msg or receive msg.

Here is my code.

component A)

#include <SoftwareSerial.h>

SoftwareSerial mySerial(10,11);

int text;
int outtext;

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

delay(1000); //for stabilizing

}

void loop() {

digitalWrite(13,HIGH);
if(mySerial.available()){
outtext= mySerial.read();
Serial.write(outtext);
digitalWrite(13,LOW);
delay(100);
}

if (Serial.available())
{
Serial.print("A: ");
mySerial.print("A: ");
while(Serial.available())
{
text = Serial.read();
if(text != ‘\n’){
mySerial.write(text);
Serial.write(text);
digitalWrite(13,LOW);
delay(100);
}
}
mySerial.println();
Serial.println();
}

}

Component B)

#include <SoftwareSerial.h>

SoftwareSerial mySerial(10,11);

int text;
int outtext;

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

delay(1000); //for stabilizing
}

void loop() {

digitalWrite(13,HIGH);
if(mySerial.available()){
outtext= mySerial.read();
Serial.write(outtext);
digitalWrite(13,LOW);
delay(200);
}

if (Serial.available())
{
Serial.print("B : ");
mySerial.print("B : ");
while(Serial.available())
{
text = Serial.read();
if(text != ‘\n’){
mySerial.write(text);
Serial.write(text);
digitalWrite(13,LOW);
delay(200);
}
}
mySerial.println();
Serial.println();
}

}

There is no problem if I change the delay time at component B to 200. (on that code)
However, when I set delay 100 at B, (same as A component). There are problem that they can’t distinguish. A:~~ A:~~~ appear. not B appear.
please help me sir…! thank you.
Also I’d like to know why this problem happen.

1. Use code tags (</>) to post your codes in the post page. If you would use code tags (</>), your codes would appear like this: (copy your codes from the IDE; click on </> icon of this post page and then press Cntrl-V; you are OK!)

#include <SoftwareSerial.h>

SoftwareSerial mySerial(10,11); 

int text;
int outtext;

2. Write clearly the sequence events that are going to take place between your two Arduinos.
(1) Two Arduinos (UNO and UNO or UNO and NANO or NANO and NANO or what?) are connected by sofyware UART Port (10, 11). UNO-1 is the Sender and UNO-2 is the Receiver.

(2) What message do want send from the InputBox of the Serial Monitor (SM) of UNO-1 to UNO-2? Once UNO-2 receives this message, the built-in LED (L) of UNO-2 will become ON. Is it like this? What terminating character do you want to add at the end of the message? The Serial Monitor has three options: (Newline = 0x0A('\n'), carriage return = 0x0D('\r'), Both NL & CR = 0x0D follwed by 0x0A) and these options are selected by the 'Line ending tab' of the SM (see Fig-1). Let us assume that you want to send this message from UNO-1: "A: " (A: space) followed by Newline character.
SerialMonitor.png
Figure-1: Serial Monitor of IDE of Arduino

(3) When you send this message: "B: " (B: space) followed by Newline character from UNO-2 to UNO-1, the UNO-1 receives the message and bring its built-in LED (L) into ON-state.

(4) UNO-1 Codes (follow these codes and correct the logical errors of your code)

#include <SoftwareSerial.h>
SoftwareSerial mySerial(10, 11);
char myStr[10]="";

void setup()
{
  Serial.begin(9600);
  mySerial.begin(9600);
  pinMode(13, OUTPUT);
  digitalWrite(13, LOW);    //L is OFF
  //delay(1000); //for stabilizing
}

void loop()
{
  //-----------------------------------
    byte n = Serial.available();
    if (n != 0)
    {
      char x = Serial.read();
      Serial.print(x);
      mySerial.print(x);
    }
  
  //----------------------------------
    byte n1 = mySerial.available();
    if (n1 != 0)
    {
      mySerial.readBytesUntil('\n', myStr, 10);
      int x = strcmp(myStr, "B: ");
      if (x == 0)
      {
        digitalWrite(13, HIGH);   //L is ON
        delay(1000);   //ON for 1-sec
        digitalWrite(13, LOW);
      }
    }
  //----------------------------------------
}

((5) UNO-2 Codes (follow these codes and correct the logical errors of your code)

#include <SoftwareSerial.h>
SoftwareSerial mySerial(10, 11);
char myStr[10] = "";

void setup()
{
  Serial.begin(9600);
  mySerial.begin(9600);
  pinMode(13, OUTPUT);
  digitalWrite(13, LOW);    //L is OFF
  //delay(1000); //for stabilizing
}

void loop()
{
  //-----------------------------------
  byte n = Serial.available();
  if (n != 0)
  {
    char x = Serial.read();
    Serial.print(x);
    mySerial.print(x);
  }
  //----------------------------------
  byte n1 = mySerial.available();
  if (n1 != 0)
  {
    mySerial.readBytesUntil('\n', myStr, 10);
    int x = strcmp(myStr, "A: ");
    if (x == 0)
    {
      digitalWrite(13, HIGH);   //L is ON
      delay(1000);   //On for 1sec
      digitalWrite(13, LOW);
    }
  }
  //----------------------------------------
}

SerialMonitor.png

I really appreciate your help. I corrected my code right before.
And thanks for your tip for me. I’ll follow your tips after this time.
Thanks you. Have a nice day :slight_smile: