Serial Communication Problem

Hello

I am very new for Arduino programming. I try to make a program for comparison of two different parameters(phone_sender and phone_reg_temp). Before the comparison part, I tried to get phone_sender and phone_reg clearly. To investigate what the response of arduino for every step, I added Serial.print() lines. I want to get same number of character from serial monitor. Until the last step, I get decent results but in the last step, Arduino combines phone_reg_temp and phone_sender. Also, it puts two squares at the end of phone_sender. Could you tell me what I should do to obtain the last step like in the following?


1
phone_reg_temp
+444444444444
phone_sender
1234567890123


My Board is Arduino Uno R3.
Baud rates of serial monitor and Serial.begin are same(9600).

Thank you in advance for taking the time and reading the message.

SCRIPT

char phone_reg[5][13] = {"+444444444444", "+111111111111", "+999999999999", "+777777777777", "0000000000000"};
char phone_reg_temp[13] ;
char phone_sender[13] ;

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

void loop() {

int j = 0;

while (Serial.available() > 0) {

delay(50);
Serial.println(Serial.available());
phone_sender[j] = Serial.read();

delay(500);
phone_reg_temp[j] = phone_reg[0][j];
j++;

delay(500);
Serial.println("phone_reg_temp");
Serial.println(phone_reg_temp);

delay(500);
Serial.println("phone_sender");
Serial.println(phone_sender);
}
}

SERIAL MONITOR
INPUT VALUE: 1234567890123

13
phone_reg_temp
+
phone_sender
1
12
phone_reg_temp
+4
phone_sender
12
11
phone_reg_temp
+44
phone_sender
123
10
phone_reg_temp
+444
phone_sender
1234
9
phone_reg_temp
+4444
phone_sender
12345
8
phone_reg_temp
+44444
phone_sender
123456
7
phone_reg_temp
+444444
phone_sender
1234567
6
phone_reg_temp
+4444444
phone_sender
12345678
5
phone_reg_temp
+44444444
phone_sender
123456789
4
phone_reg_temp
+444444444
phone_sender
1234567890
3
phone_reg_temp
+4444444444
phone_sender
12345678901
2
phone_reg_temp
+44444444444
phone_sender
123456789012
1
phone_reg_temp
+4444444444441234567890123
phone_sender
1234567890123

Hello

I am very new for Arduino programming. I try to make a program for comparison of two different parameters(phone_sender and phone_reg_temp). Before the comparison part, I tried to get phone_sender and phone_reg clearly. To investigate what the response of arduino for every step, I added Serial.print() lines. I want to get same number of character from serial monitor. Until the last step, I get decent results but in the last step, Arduino combines phone_reg_temp and phone_sender. Also, it puts two squares at the end of phone_sender. Could you tell me what I should do to obtain the last step like in the following?

1
phone_reg_temp
+444444444444
phone_sender
1234567890123

My Board is Arduino Uno R3.
Baud rates of serial monitor and Serial.begin are same(9600).

Thank you in advance for taking the time and reading the message.

SCRIPT

char phone_reg[5][13] = {"+444444444444", "+111111111111", "+999999999999", "+777777777777", "0000000000000"};
char phone_reg_temp[13] ;
char phone_sender[13] ;

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

void loop() {

int j = 0;

while (Serial.available() > 0) {

delay(50);
Serial.println(Serial.available());
phone_sender[j] = Serial.read();

delay(500);
phone_reg_temp[j] = phone_reg[0][j];
j++;

delay(500);
Serial.println("phone_reg_temp");
Serial.println(phone_reg_temp);

delay(500);
Serial.println("phone_sender");
Serial.println(phone_sender);
}
}

SERIAL MONITOR
INPUT VALUE: 1234567890123

13
phone_reg_temp
+
phone_sender
1
12
phone_reg_temp
+4
phone_sender
12
11
phone_reg_temp
+44
phone_sender
123
10
phone_reg_temp
+444
phone_sender
1234
9
phone_reg_temp
+4444
phone_sender
12345
8
phone_reg_temp
+44444
phone_sender
123456
7
phone_reg_temp
+444444
phone_sender
1234567
6
phone_reg_temp
+4444444
phone_sender
12345678
5
phone_reg_temp
+44444444
phone_sender
123456789
4
phone_reg_temp
+444444444
phone_sender
1234567890
3
phone_reg_temp
+4444444444
phone_sender
12345678901
2
phone_reg_temp
+44444444444
phone_sender
123456789012
1
phone_reg_temp
+4444444444441234567890123
phone_sender
1234567890123

Hi,
Welcome to the forum.

Please read the post at the start of any forum , entitled “How to use this Forum”.
OR
http://forum.arduino.cc/index.php/topic,148850.0.html.
Then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Thanks… Tom… :slight_smile:

When posting code please use the code button </> so your code looks like this. Also use the AutoFormat tool to indent your cod for easier reading

char phone_reg[5][13] = {"+444444444444", "+111111111111", "+999999999999", "+777777777777", "0000000000000"};
char phone_reg_temp[13] ;
char phone_sender[13] ;

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

void loop() {

    int j = 0;

    while (Serial.available() > 0) {
        
        delay(50);
        Serial.println(Serial.available());
        phone_sender[j] = Serial.read();

        delay(500);
        phone_reg_temp[j] = phone_reg[0][j];
        j++;
        
        delay(500);
        Serial.println("phone_reg_temp");
        Serial.println(phone_reg_temp);

        delay(500);
        Serial.println("phone_sender");
        Serial.println(phone_sender);
    }
}

I can’t figure out what you are expecting your program to do. What are you sending to it? What is this piece of code intended to do

phone_reg_temp[j] = phone_reg[0][j];

My wild guess is that you are writing to memory beyond the bouds of the array

This code

       Serial.println("phone_reg_temp");
        Serial.println(phone_reg_temp);

would be better this way

       Serial.print("phone_reg_temp   ");
        Serial.println(phone_reg_temp);

Have a look at the examples in Serial Input Basics - simple reliable non-blocking ways to receive data. There is also a parse example to illustrate how to extract numbers from the received text.

Note also how the business of receiving data is separated from the business of processing the data - that makes programs much easier to manage.

…R

Duplicate topics moved and merged

Cross-posting is against the rules of the forum. The reason is that duplicate posts can waste the time of the people trying to help. Someone might spend 15 minutes (or more) writing a detailed answer on this topic, without knowing that someone else already did the same in the other topic.

Repeated cross-posting will result in a timeout from the forum.

In the future, please take some time to pick the forum board that best suits the topic of your question and then only post once to that forum board. This is basic forum etiquette, as explained in the sticky "How to use this forum - please read." post you will find at the top of every forum board. It contains a lot of other useful information. Please read it.

Thanks in advance for your cooperation.

i think this is closer to want you’re trying to do which i think is enter a single digit index and retrieve a phone number string from an array.

#define N_REG  5
const char *phone_reg [N_REG] = {
    "+444444444444",
    "+111111111111",
    "+999999999999",
    "+777777777777",
    "0000000000000"
};

char phone_reg_temp [13] ;
char phone_sender   [13] ;

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

void
loop (void) {
    if (! Serial.available ())
        return;

    int  n = Serial.read() - '0';
    if (N_REG <= n)  {
        Serial.print   ("index out of range: ");
        Serial.println (n);
        return;
    }

    const char *p = phone_reg [n];
    Serial.println (p);
}

there’s a few issues with your code

this looks like you’re trying to copy the phone # string from one string array to another. but it would only copy one character for each serial input. And there’s no need to

        phone_reg_temp[j] = phone_reg[0][j];
        j++;

instead of defining the phone numbers a arrays of characters, you can define then as arrays of strings.

#define N_REG  5
const char *phone_reg [N_REG] = {
    "+444444444444",
    "+111111111111",
    "+999999999999",
    "+777777777777",
    "0000000000000"
};

and then just copy the string ptr using the index of the array

    const char *p = phone_reg [n];

i’m not really sure what the best way is to read a multi-digit decimal value from the serial interface, but this is one approach for reading a single ascii digit and converting it to an integer

int  n = Serial.read() - '0';

and you should test that the index is in range

    if (N_REG <= n)  {
        Serial.print   ("index out of range: ");
        Serial.println (n);
        return;
    }
char phone_reg[5][13] = {"+444444444444", "+111111111111", "+999999999999", "+777777777777", "0000000000000"};
char phone_reg_temp[13] ;
char phone_sender[13] ;

All these char arrays should be sized at [14] to accomodate the null terminator. With the " " declaration, they are 13 characters pluss a null ('\0'). The null tells the Serial.print() where to stop. You won't see the trailing garbage.

phone_reg_temp
+4444444444441234567890123
phone_sender
1234567890123